-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy existing database functions to procedures folder, where they can…
… be migrated and maintained more easily. Update api_patch_system_user.ts to do case-insensitive comparisons when finding a system user.
- Loading branch information
Showing
2 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { Knex } from 'knex'; | ||
|
||
/** | ||
* Add/update function that patches a system user record. | ||
* | ||
* Steps: | ||
* 1. Attempts to find an existing system user record | ||
* - First using `p_system_user_guid` | ||
* - Second using `p_user_identifier` and `p_user_identity_source_name` | ||
* 2. If no user is found, return null | ||
* 3. If a user is found, update the system user record with the latest information passed to this function if any of | ||
* the incoming values are not the same as the existing values | ||
* | ||
* @export | ||
* @param {Knex} knex | ||
* @return {*} {Promise<void>} | ||
*/ | ||
export async function up(knex: Knex): Promise<void> { | ||
await knex.raw(`--sql | ||
set search_path=biohub; | ||
CREATE OR REPLACE FUNCTION | ||
api_patch_system_user ( | ||
p_system_user_guid character varying, | ||
p_user_identifier character varying, | ||
p_user_identity_source_name character varying, | ||
p_email character varying, | ||
p_display_name character varying, | ||
p_given_name character varying, | ||
p_family_name character varying, | ||
p_agency character varying | ||
) | ||
RETURNS integer | ||
LANGUAGE plpgsql | ||
SET client_min_messages TO 'warning' | ||
AS $$ | ||
-- ******************************************************************* | ||
-- Procedure: api_patch_system_user | ||
-- Purpose: Updates a system_user record if any of the incoming values are not the same as the existing values. | ||
-- | ||
-- MODIFICATION HISTORY | ||
-- Person Date Comments | ||
-- ---------------- ----------- -------------------------------------- | ||
-- nick.phura@quartech.com | ||
-- 2023-08-01 initial release | ||
-- ******************************************************************* | ||
DECLARE | ||
_system_user system_user%rowtype; | ||
_user_identity_source_id user_identity_source.user_identity_source_id%type; | ||
BEGIN | ||
-- Attempt to find user based on guid | ||
SELECT * INTO _system_user FROM system_user | ||
WHERE LOWER(user_guid) = LOWER(p_system_user_guid) | ||
AND record_end_date IS NULL | ||
LIMIT 1; | ||
-- Otherwise, attempt to find user based on identifier and identity source | ||
IF NOT found THEN | ||
SELECT user_identity_source_id INTO strict _user_identity_source_id FROM user_identity_source | ||
WHERE name = p_user_identity_source_name | ||
AND record_end_date IS NULL; | ||
SELECT * INTO _system_user FROM system_user | ||
WHERE user_identity_source_id = _user_identity_source_id | ||
AND LOWER(user_identifier) = LOWER(p_user_identifier) | ||
LIMIT 1; | ||
END IF; | ||
-- If no user found, return and do nothing | ||
IF NOT found THEN | ||
RETURN NULL; | ||
END IF; | ||
-- Otherwise, patch the system user record with the latest information passed to this function | ||
UPDATE system_user SET | ||
user_guid = p_system_user_guid, | ||
user_identifier = p_user_identifier, | ||
email = p_email, | ||
display_name = p_display_name, | ||
given_name = p_given_name, | ||
family_name = p_family_name, | ||
agency = p_agency | ||
WHERE | ||
system_user_id = _system_user.system_user_id | ||
AND ( | ||
user_guid != p_system_user_guid OR | ||
user_identifier != p_user_identifier OR | ||
email != p_email OR | ||
display_name != p_display_name OR | ||
given_name != p_given_name OR | ||
family_name != p_family_name OR | ||
agency != p_agency | ||
); | ||
-- Return system user id of patched record | ||
RETURN _system_user.system_user_id; | ||
EXCEPTION | ||
WHEN OTHERS THEN | ||
RAISE; | ||
END; | ||
$$; | ||
COMMENT ON FUNCTION api_patch_system_user(varchar, varchar, varchar, varchar, varchar, varchar, varchar, varchar) IS 'Updates a system_user record if any of the incoming values are not the same as the existing values.'; | ||
`); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.raw(``); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Knex } from 'knex'; | ||
|
||
/** | ||
* Add/update function to set the context for the user making a database request. Used by the auditing and journaling | ||
* triggers. | ||
* | ||
* @export | ||
* @param {Knex} knex | ||
* @return {*} {Promise<void>} | ||
*/ | ||
export async function up(knex: Knex): Promise<void> { | ||
await knex.raw(` | ||
SET search_path = 'biohub'; | ||
DROP FUNCTION IF EXISTS api_set_context; | ||
CREATE OR REPLACE FUNCTION api_set_context(p_system_user_guid system_user.user_guid%type, p_user_identity_source_name user_identity_source.name%type) RETURNS system_user.system_user_id%type | ||
language plpgsql | ||
security invoker | ||
SET client_min_messages = warning | ||
AS | ||
$$ | ||
DECLARE | ||
_system_user_id system_user.system_user_id%type; | ||
_user_identity_source_id user_identity_source.user_identity_source_id%type; | ||
BEGIN | ||
SELECT user_identity_source_id INTO strict _user_identity_source_id FROM user_identity_source | ||
WHERE LOWER(name) = LOWER(p_user_identity_source_name) | ||
AND record_end_date IS NULL; | ||
SELECT system_user_id INTO strict _system_user_id FROM system_user | ||
WHERE user_identity_source_id = _user_identity_source_id | ||
AND LOWER(user_guid) = LOWER(p_system_user_guid); | ||
CREATE TEMP TABLE IF NOT EXISTS biohub_context_temp (tag varchar(200), value varchar(200)); | ||
DELETE FROM biohub_context_temp WHERE tag = 'user_id'; | ||
INSERT INTO biohub_context_temp (tag, value) values ('user_id', _system_user_id::varchar(200)); | ||
RETURN _system_user_id; | ||
EXCEPTION | ||
WHEN OTHERS THEN | ||
RAISE; | ||
END; | ||
$$; | ||
`); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.raw(``); | ||
} |