From 7f244b9b98ce87e411e59b86ea26d493fdd72097 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 14 Feb 2024 09:04:58 -0500 Subject: [PATCH 1/6] Add connectionSubPrefix to user creation stored procedure --- .../db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql index 53c550fad..f194fc5b2 100644 --- a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql +++ b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql @@ -19,9 +19,12 @@ IF @userUUID IS NULL THEN SET @userUUID = UNHEX(REPLACE(UUID(), '-', '')); -- Retrieve the UUID for the connection SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; +SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; +@connectionSubPrefix := concat(@connectionSubPrefix, '|'); +@connectionSubPrefix := concat('LONG_TERM_TOKEN|', connection_id); -- Insert the new user record into the user table INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token) -VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, NULL, 1, NULL); +VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(UUID(),'-','')), 1, NULL); END IF; -- If the role exists, associate the user with the role From 900718ffe0e2752dce80076a99b9bc2512ec6603 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 14 Feb 2024 10:21:30 -0500 Subject: [PATCH 2/6] Refactor connection prefix manipulation in SQL procedure --- .../db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql index f194fc5b2..caaeca559 100644 --- a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql +++ b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql @@ -20,11 +20,11 @@ IF @userUUID IS NULL THEN -- Retrieve the UUID for the connection SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; -@connectionSubPrefix := concat(@connectionSubPrefix, '|'); -@connectionSubPrefix := concat('LONG_TERM_TOKEN|', connection_id); +SET @connectionSubPrefix := concat(@connectionSubPrefix, '|'); +SET @connectionSubPrefix := concat('LONG_TERM_TOKEN|', connection_id); -- Insert the new user record into the user table INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token) -VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(UUID(),'-','')), 1, NULL); +VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(UUID(),'-','')), 1, NULL); END IF; -- If the role exists, associate the user with the role From f75bbe7980a3381ee4004fd75ef3d8161e81452a Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 14 Feb 2024 10:34:42 -0500 Subject: [PATCH 3/6] Update user creation stored procedure The changes made address the process of user creation in the stored procedure. A new variable, @baseUUID, has been introduced for storing UUIDs during processing. Additionally, the preparation of @connectionSubPrefix has been adjusted to concatenate LONG_TERM_TOKEN with existing values instead of overriding them. --- .../db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql index caaeca559..0343a70de 100644 --- a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql +++ b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql @@ -15,16 +15,17 @@ SELECT @roleUUID := uuid FROM auth.role WHERE name = role_name; -- If the user does not exist, create a new user entry IF @userUUID IS NULL THEN + set @baseUUID = UUID(); -- Generate a new UUID for the user - SET @userUUID = UNHEX(REPLACE(UUID(), '-', '')); + SET @userUUID = UNHEX(REPLACE(@baseUUID, '-', '')); -- Retrieve the UUID for the connection SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; SET @connectionSubPrefix := concat(@connectionSubPrefix, '|'); -SET @connectionSubPrefix := concat('LONG_TERM_TOKEN|', connection_id); +SET @connectionSubPrefix := concat('LONG_TERM_TOKEN|', @connectionSubPrefix); -- Insert the new user record into the user table INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token) -VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(UUID(),'-','')), 1, NULL); +VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(@baseUUID,'-','')), 1, NULL); END IF; -- If the role exists, associate the user with the role From d7a4a7c4d02e3bd9ab7ef0856f4da451c478a5a9 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 14 Feb 2024 10:41:46 -0500 Subject: [PATCH 4/6] Remove additional concat operation --- pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql index 0343a70de..b021f546f 100644 --- a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql +++ b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql @@ -21,7 +21,6 @@ IF @userUUID IS NULL THEN -- Retrieve the UUID for the connection SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; -SET @connectionSubPrefix := concat(@connectionSubPrefix, '|'); SET @connectionSubPrefix := concat('LONG_TERM_TOKEN|', @connectionSubPrefix); -- Insert the new user record into the user table INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token) From 4f487ca69a32bd87c8937237ca65209681ae8c51 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 14 Feb 2024 11:15:50 -0500 Subject: [PATCH 5/6] Remove long term prefix --- pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql index b021f546f..0c1d27b0f 100644 --- a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql +++ b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql @@ -21,7 +21,6 @@ IF @userUUID IS NULL THEN -- Retrieve the UUID for the connection SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; -SET @connectionSubPrefix := concat('LONG_TERM_TOKEN|', @connectionSubPrefix); -- Insert the new user record into the user table INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token) VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(@baseUUID,'-','')), 1, NULL); From 3ab0806db20ba5c8292cefe43eaedbe3bcaf1ba5 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Wed, 14 Feb 2024 12:00:11 -0500 Subject: [PATCH 6/6] Add PIC-SURE User role assignment in CreateUserWithRole procedure Improved the CreateUserWithRole stored procedure in the auth-db. All new users are now automatically assigned the 'PIC-SURE User' role in addition to specific roles designated during account creation. This ensures all users have access to the base level of functionalities. --- .../V4__ADD_CREATE_USER_STORED_PROCEDURE.sql | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql index 0c1d27b0f..af57999c5 100644 --- a/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql +++ b/pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql @@ -2,7 +2,7 @@ USE `auth`; DROP PROCEDURE IF EXISTS CreateUserWithRole; DELIMITER // -CREATE PROCEDURE CreateUserWithRole ( +CREATE PROCEDURE CreateUserWithRole( IN user_email VARCHAR(255), IN connection_id VARCHAR(255), IN role_name VARCHAR(255), @@ -10,25 +10,34 @@ CREATE PROCEDURE CreateUserWithRole ( ) BEGIN -- Attempt to retrieve the UUIDs for the user and role based on the provided information -SELECT @userUUID := uuid FROM auth.user WHERE email = user_email AND connectionId = connection_id; -SELECT @roleUUID := uuid FROM auth.role WHERE name = role_name; + SELECT @userUUID := uuid FROM auth.user WHERE email = user_email AND connectionId = connection_id; + SELECT @roleUUID := uuid FROM auth.role WHERE name = role_name; + SELECT @picsureUserRoleId := uuid FROM auth.role WHERE name = 'PIC-SURE User'; -- If the user does not exist, create a new user entry -IF @userUUID IS NULL THEN + IF @userUUID IS NULL THEN set @baseUUID = UUID(); -- Generate a new UUID for the user SET @userUUID = UNHEX(REPLACE(@baseUUID, '-', '')); -- Retrieve the UUID for the connection -SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; -SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; + SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id; + SELECT @connectionSubPrefix := subPrefix FROM auth.connection WHERE id = connection_id; -- Insert the new user record into the user table -INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token) -VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, concat(@connectionSubPrefix, REPLACE(@baseUUID,'-','')), 1, NULL); -END IF; + INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, + long_term_token) + VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, + concat(@connectionSubPrefix, REPLACE(@baseUUID, '-', '')), 1, NULL); + END IF; -- If the role exists, associate the user with the role IF @roleUUID IS NOT NULL THEN INSERT INTO auth.user_role (user_id, role_id) VALUES (@userUUID, @roleUUID); -END IF; + END IF; + + -- If the role is not PIC-SURE User, associate the user with the PIC-SURE User role as well + -- All users must have the PIC-SURE User role + IF @roleUUID IS NOT NULL AND @roleUUID != @picsureUserRoleId THEN + INSERT INTO auth.user_role (user_id, role_id) VALUES (@userUUID, @picsureUserRoleId); + END IF; END// DELIMITER ; \ No newline at end of file