diff options
author | Frederick Muriuki Muriithi | 2023-03-18 09:14:43 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-03-18 09:15:15 +0300 |
commit | 30900b963c043939caa4492aca6d130843e048d0 (patch) | |
tree | c20dd229151d3db6202fcee754524b64a7fb1700 | |
parent | a1a376db4848a3ccd74c9c384efacea4579ce165 (diff) | |
download | genenetwork3-30900b963c043939caa4492aca6d130843e048d0.tar.gz |
auth: resources: Disallow duplicate resource names.
-rw-r--r-- | gn3/auth/authorisation/resources/views.py | 22 | ||||
-rw-r--r-- | migrations/auth/20221110_01_WtZ1I-create-resources-table.py | 2 |
2 files changed, 17 insertions, 7 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") diff --git a/migrations/auth/20221110_01_WtZ1I-create-resources-table.py b/migrations/auth/20221110_01_WtZ1I-create-resources-table.py index 0b769b8..abc8895 100644 --- a/migrations/auth/20221110_01_WtZ1I-create-resources-table.py +++ b/migrations/auth/20221110_01_WtZ1I-create-resources-table.py @@ -12,7 +12,7 @@ steps = [ CREATE TABLE IF NOT EXISTS resources( group_id TEXT NOT NULL, resource_id TEXT NOT NULL, - resource_name TEXT NOT NULL, + resource_name TEXT NOT NULL UNIQUE, resource_category_id TEXT NOT NULL, PRIMARY KEY(group_id, resource_id), FOREIGN KEY(group_id) REFERENCES groups(group_id) |