diff --git a/flowauth/backend/flowauth/models.py b/flowauth/backend/flowauth/models.py index 063e382a88..ece3cb1b41 100644 --- a/flowauth/backend/flowauth/models.py +++ b/flowauth/backend/flowauth/models.py @@ -440,11 +440,9 @@ class Scope(db.Model): For example, the scope permitting daily locations at admin 3 would be daily_location:admin3 """ - # OK, here's the heart of it. - # Each role has a collection of these referred to. - id = db.Column(db.Integer, primary_key=True, autoincrement=True) scope = db.Column(db.String) + enabled = db.Column(db.Boolean, default=True) server_id = db.Column(db.Integer, db.ForeignKey("server.id")) diff --git a/flowauth/backend/flowauth/servers.py b/flowauth/backend/flowauth/servers.py index fd926bbb2f..a4ab37976f 100644 --- a/flowauth/backend/flowauth/servers.py +++ b/flowauth/backend/flowauth/servers.py @@ -100,7 +100,31 @@ def list_scopes(server_id): Returns the list of available scopes on a server """ server = Server.query.filter_by(id=server_id).first_or_404() - return jsonify({scope.id: scope.scope for scope in server.scopes}) + return jsonify({scope.scope: scope.enabled for scope in server.scopes}) + + +@blueprint.route("/servers//scopes", methods=["PATCH"]) +@login_required +@admin_permission.require(http_exception=401) +def edit_scope_activation(server_id): + """ + Bulk activates/deactivates scopes on a server + Expects a json of the form {scope_string:True/False} + + """ + server = Server.query.filter_by(id=server_id).first_or_404() + json = request.get_json() + scopes_to_edit = ( + db.session.query(Scope) + .join(Server) + .filter(Scope.server_id == server_id) + .filter(Scope.scope.in_(json.keys())) + ) + for scope in scopes_to_edit: + scope.enabled = json[scope.scope] + db.session.add(scope) + db.session.commit() + return list_scopes(server_id) @blueprint.route("/servers//time_limits") diff --git a/flowauth/backend/tests/test_server_admin.py b/flowauth/backend/tests/test_server_admin.py index fbfa7d69f1..6696183baf 100644 --- a/flowauth/backend/tests/test_server_admin.py +++ b/flowauth/backend/tests/test_server_admin.py @@ -210,7 +210,22 @@ def test_list_scopes(client, auth, test_scopes, test_servers, test_admin): ) assert response.status_code == 200 assert response.json == { - "1": "get_result", - "3": "run", - "4": "dummy_query:admin_level_1", + "get_result": True, + "run": True, + "dummy_query:admin_level_1": True, + } + + +def test_enabled_scopes(client, auth, test_scopes, test_servers, test_admin): + uid, uname, password = test_admin + response, csrf_cookie = auth.login(uname, password) + json = {"dummy_query:admin_level_1": False} + response = client.patch( + "/admin/servers/1/scopes", json=json, headers={"X-CSRF-Token": csrf_cookie} + ) + assert response.status_code == 200 + assert response.json == { + "get_result": True, + "run": True, + "dummy_query:admin_level_1": False, }