Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9c5234b

Browse files
Gcolon021Luke Sikina
authored and
Luke Sikina
committedOct 7, 2024
[ALS-5612] Create stored procedure to create a user (#154)
* Add a new stored procedure to enable user creation * Specific database Auth * Update CreateUserWithRole stored procedure in auth DB The stored procedure, CreateUserWithRole, in the 'auth' database has been updated to improve user creation. It now checks for existing users and roles, and generates a new UUID if needed. Additionally, it associates new users with roles if they exist. * Rename CreateUserWithRole stored procedure file * Add general metadata parameter to CreateUserWithRole procedure
1 parent 63f4779 commit 9c5234b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
USE `auth`;
2+
3+
DROP PROCEDURE IF EXISTS CreateUserWithRole;
4+
DELIMITER //
5+
CREATE PROCEDURE CreateUserWithRole (
6+
IN user_email VARCHAR(255),
7+
IN connection_id VARCHAR(255),
8+
IN role_name VARCHAR(255),
9+
IN user_general_metadata varchar(255)
10+
)
11+
BEGIN
12+
-- Attempt to retrieve the UUIDs for the user and role based on the provided information
13+
SELECT @userUUID := uuid FROM auth.user WHERE email = user_email AND connectionId = connection_id;
14+
SELECT @roleUUID := uuid FROM auth.role WHERE name = role_name;
15+
16+
-- If the user does not exist, create a new user entry
17+
IF @userUUID IS NULL THEN
18+
-- Generate a new UUID for the user
19+
SET @userUUID = UNHEX(REPLACE(UUID(), '-', ''));
20+
-- Retrieve the UUID for the connection
21+
SELECT @connectionUUID := uuid FROM auth.connection WHERE id = connection_id;
22+
-- Insert the new user record into the user table
23+
INSERT INTO auth.user (uuid, general_metadata, acceptedTOS, connectionId, email, matched, subject, is_active, long_term_token)
24+
VALUES (@userUUID, user_general_metadata, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, NULL, 1, NULL);
25+
END IF;
26+
27+
-- If the role exists, associate the user with the role
28+
IF @roleUUID IS NOT NULL THEN
29+
INSERT INTO auth.user_role (user_id, role_id) VALUES (@userUUID, @roleUUID);
30+
END IF;
31+
END//
32+
DELIMITER ;

0 commit comments

Comments
 (0)
Please sign in to comment.