about summary refs log tree commit diff
path: root/gn3/auth/authorisation/resources.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/auth/authorisation/resources.py')
-rw-r--r--gn3/auth/authorisation/resources.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/gn3/auth/authorisation/resources.py b/gn3/auth/authorisation/resources.py
index d01c435..f0a4b3a 100644
--- a/gn3/auth/authorisation/resources.py
+++ b/gn3/auth/authorisation/resources.py
@@ -1,10 +1,14 @@
 """Handle the management of resources."""
-from uuid import UUID
+from uuid import UUID, uuid4
 from typing import NamedTuple
 
 from gn3.auth import db
-from .groups import Group
 from .checks import authorised_p
+from .exceptions import AuthorisationError
+from .groups import Group, authenticated_user_group
+
+class MissingGroupError(AuthorisationError):
+    """Raised for any resource operation without a group."""
 
 class ResourceCategory(NamedTuple):
     """Class representing a resource category."""
@@ -22,6 +26,18 @@ class Resource(NamedTuple):
 @authorised_p(("create-resource",),  error_message="Could not create resource")
 def create_resource(
         conn: db.DbConnection, resource_name: str,
-        resource_category: ResourceCategory):
+        resource_category: ResourceCategory) -> Resource:
     """Create a resource item."""
-    return tuple()
+    with db.cursor(conn) as cursor:
+        group = authenticated_user_group(conn).maybe(False, lambda val: val)
+        if not group:
+            raise MissingGroupError(
+                "User with no group cannot create a resource.")
+        resource = Resource(group, uuid4(), resource_name, resource_category)
+        cursor.execute(
+            ("INSERT INTO resources VALUES (?, ?, ?, ?)"),
+            (str(resource.group.group_id), str(resource.resource_id),
+             resource_name,
+             str(resource.resource_category.resource_category_id)))
+
+    return resource