Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ALS-5612] Create stored procedure to create a user #154

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pic-sure-auth-db/db/sql/V4__ADD_CREATE_USER_STORED_PROCEDURE.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
USE `auth`;

DROP PROCEDURE IF EXISTS CreateUserWithRole;
DELIMITER //
CREATE PROCEDURE CreateUserWithRole (
IN user_email VARCHAR(255),
IN connection_id VARCHAR(255),
IN role_name VARCHAR(255)
)
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;

-- If the user does not exist, create a new user entry
IF @userUUID IS NULL THEN
-- Generate a new UUID for the user
SET @userUUID = UNHEX(REPLACE(UUID(), '-', ''));
-- Retrieve the UUID for the connection
SELECT @connectionUUID := uuid 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, NULL, CURRENT_TIMESTAMP, @connectionUUID, user_email, 0, NULL, 1, NULL);
Gcolon021 marked this conversation as resolved.
Show resolved Hide resolved
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//
DELIMITER ;
Loading