Skip to content

Commit

Permalink
Introduce ENABLE_SUPERADMIN_ASSIGNMENT variable
Browse files Browse the repository at this point in the history
- for disableing / enabling admin access button in admin
  • Loading branch information
MarcelGeo committed Nov 11, 2024
1 parent f091e73 commit d618ec1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions server/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

Configuration.SERVER_TYPE = "ce"
Configuration.USER_SELF_REGISTRATION = False

application = create_app(
[
"DOCS_URL",
Expand All @@ -37,6 +38,7 @@
"GLOBAL_ADMIN",
"GLOBAL_READ",
"GLOBAL_WRITE",
"ENABLE_SUPERADMIN_ASSIGNMENT",
]
)
register_stats(application)
Expand Down
7 changes: 7 additions & 0 deletions server/mergin/auth/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,18 @@ def update_user(username): # pylint: disable=W0613,W0612
form = UserForm.from_json(request.json)
if not form.validate_on_submit():
return jsonify(form.errors), 400
if request.json.get("is_admin") is not None and not current_app.config.get(
"ENABLE_SUPERADMIN_ASSIGNMENT"
):
abort(400, "Unable to assign super admin role")

user = User.query.filter_by(username=username).first_or_404("User not found")
form.update_obj(user)

# remove inactive since flag for ban or re-activation
user.inactive_since = None
print("heeeere")

db.session.add(user)
db.session.commit()
return jsonify(UserSchema().dump(user))
Expand Down
4 changes: 4 additions & 0 deletions server/mergin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ class Configuration(object):
USER_SELF_REGISTRATION = config("USER_SELF_REGISTRATION", default=False, cast=bool)
# build hash number
BUILD_HASH = config("BUILD_HASH", default="")
# Allow changing access to admin panel
ENABLE_SUPERADMIN_ASSIGNMENT = config(
"ENABLE_SUPERADMIN_ASSIGNMENT", default=True, cast=bool
)
20 changes: 20 additions & 0 deletions server/mergin/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,25 @@ def test_api_user_profile(client):
def test_update_user(client):
login_as_admin(client)
user = User.query.filter_by(username="mergin").first()
data = {"active": True, "is_admin": True}
resp = client.patch(
url_for("/.mergin_auth_controller_update_user", username=user.username),
data=json.dumps(data),
headers=json_headers,
)
assert resp.status_code == 200
assert user.active
assert user.is_admin

client.application.config["ENABLE_SUPERADMIN_ASSIGNMENT"] = False
data = {"active": False, "is_admin": False}
resp = client.patch(
url_for("/.mergin_auth_controller_update_user", username=user.username),
data=json.dumps(data),
headers=json_headers,
)
assert resp.status_code == 400
assert user.active
data = {"active": False}
resp = client.patch(
url_for("/.mergin_auth_controller_update_user", username=user.username),
Expand All @@ -421,6 +440,7 @@ def test_update_user(client):
assert resp.status_code == 200
assert not user.active

client.application.config["ENABLE_SUPERADMIN_ASSIGNMENT"] = True
user.is_admin = False
db.session.add(user)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
>
<PButton
:severity="user?.is_admin ? 'danger' : 'warning'"
:modelValue="user?.is_admin"
:disabled="
!instanceStore.configData?.enable_superadmin_assignment
"
@click="switchAdminAccess"
:label="
!user?.is_admin
Expand Down Expand Up @@ -129,7 +131,8 @@ import {
AppContainer,
ConfirmDialogProps,
AppSettings,
AppSettingsItemConfig
AppSettingsItemConfig,
useInstanceStore
} from '@mergin/lib'
import { computed, watch } from 'vue'
import { useRoute } from 'vue-router'
Expand All @@ -140,6 +143,7 @@ import { useAdminStore } from '@/modules/admin/store'
const route = useRoute()
const adminStore = useAdminStore()
const dialogStore = useDialogStore()
const instanceStore = useInstanceStore()
const settingsItems = computed<AppSettingsItemConfig[]>(() => [
{
Expand Down Expand Up @@ -209,8 +213,7 @@ const changeStatusDialog = () => {
await adminStore.updateUser({
username: user.value.username,
data: {
active: !user.value.active,
is_admin: user.value.is_admin
active: !user.value.active
}
})
}
Expand Down
1 change: 1 addition & 0 deletions web-app/packages/lib/src/modules/instance/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface BaseConfigResponse {
global_read?: boolean
global_write?: boolean
global_admin?: boolean
enable_superadmin_assignment: boolean
}

export type ConfigResponse = BaseConfigResponse &
Expand Down

0 comments on commit d618ec1

Please sign in to comment.