about summary refs log tree commit diff
path: root/gn3/auth/authorisation/resources/models.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-03-18 11:35:36 +0300
committerFrederick Muriuki Muriithi2023-03-18 11:35:36 +0300
commitf7b27947495b4dc928f6c257286bcb6a7112dbed (patch)
tree7fd12449a99ed759b139980fa7733eab27e460c4 /gn3/auth/authorisation/resources/models.py
parent30900b963c043939caa4492aca6d130843e048d0 (diff)
downloadgenenetwork3-f7b27947495b4dc928f6c257286bcb6a7112dbed.tar.gz
oauth2: resources: toggle whether a resource is public or not
Diffstat (limited to 'gn3/auth/authorisation/resources/models.py')
-rw-r--r--gn3/auth/authorisation/resources/models.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/gn3/auth/authorisation/resources/models.py b/gn3/auth/authorisation/resources/models.py
index 4049fae..afda5e8 100644
--- a/gn3/auth/authorisation/resources/models.py
+++ b/gn3/auth/authorisation/resources/models.py
@@ -529,3 +529,30 @@ def unassign_resource_user(
                 f"The user '{user.name}'({user.email}) had the "
                 f"'{role.role.role_name}' role on resource with ID "
                 f"'{resource.resource_id}' taken away.")}
+
+def save_resource(
+        conn: db.DbConnection, user: User, resource: Resource) -> Resource:
+    """Update an existing resource."""
+    resource_id = resource.resource_id
+    authorised = authorised_for(
+        conn, user, ("group:resource:edit-resource",), (resource_id,))
+    if authorised[resource_id]:
+        with db.cursor(conn) as cursor:
+            params = {**dictify(resource), "public": 1 if resource.public else 0}
+            print(f"THE PARAMS: {params}")
+            cursor.execute(
+                "UPDATE resources SET "
+                "resource_name=:resource_name, "
+                "public=:public "
+                "WHERE group_id=:group_id "
+                "AND resource_id=:resource_id",
+                {
+                    "resource_name": resource.resource_name,
+                    "public": 1 if resource.public else 0,
+                    "group_id": str(resource.group.group_id),
+                    "resource_id": str(resource.resource_id)
+                })
+            return resource
+
+    raise AuthorisationError(
+        "You do not have the appropriate privileges to edit this resource.")