Skip to content

Commit

Permalink
Added edit_scope_activation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Thingus committed May 31, 2022
1 parent e24c40f commit 5bb6136
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
4 changes: 1 addition & 3 deletions flowauth/backend/flowauth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))


Expand Down
26 changes: 25 additions & 1 deletion flowauth/backend/flowauth/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<server_id>/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/<server_id>/time_limits")
Expand Down
21 changes: 18 additions & 3 deletions flowauth/backend/tests/test_server_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 5bb6136

Please sign in to comment.