aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-03-18 09:14:43 +0300
committerFrederick Muriuki Muriithi2023-03-18 09:15:15 +0300
commit30900b963c043939caa4492aca6d130843e048d0 (patch)
treec20dd229151d3db6202fcee754524b64a7fb1700 /gn3
parenta1a376db4848a3ccd74c9c384efacea4579ce165 (diff)
downloadgenenetwork3-30900b963c043939caa4492aca6d130843e048d0.tar.gz
auth: resources: Disallow duplicate resource names.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/auth/authorisation/resources/views.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/gn3/auth/authorisation/resources/views.py b/gn3/auth/authorisation/resources/views.py
index 220181e..6f7b65c 100644
--- a/gn3/auth/authorisation/resources/views.py
+++ b/gn3/auth/authorisation/resources/views.py
@@ -1,6 +1,7 @@
"""The views/routes for the resources package"""
import uuid
import json
+import sqlite3
from functools import reduce
from flask import request, jsonify, Response, Blueprint, current_app as app
@@ -14,7 +15,7 @@ from .models import (
unlink_data_from_resource, create_resource as _create_resource)
from ..roles import Role
-from ..errors import InvalidData, AuthorisationError
+from ..errors import InvalidData, InconsistencyError, AuthorisationError
from ..groups.models import Group, GroupRole, group_role_by_id
from ... import db
@@ -43,11 +44,20 @@ def create_resource() -> Response:
resource_category_id = uuid.UUID(form.get("resource_category"))
db_uri = app.config["AUTH_DB"]
with db.connection(db_uri) as conn:
- resource = _create_resource(
- conn, resource_name, resource_category_by_id(
- conn, resource_category_id),
- the_token.user)
- return jsonify(dictify(resource))
+ try:
+ resource = _create_resource(
+ conn, resource_name, resource_category_by_id(
+ conn, resource_category_id),
+ the_token.user)
+ return jsonify(dictify(resource))
+ except sqlite3.IntegrityError as sql3ie:
+ if sql3ie.args[0] == ("UNIQUE constraint failed: "
+ "resources.resource_name"):
+ raise InconsistencyError(
+ "You cannot have duplicate resource names.") from sql3ie
+ app.logger.debug(
+ f"{type(sql3ie)=}: {sql3ie=}")
+ raise
@resources.route("/view/<uuid:resource_id>")
@require_oauth("profile group resource")