Compare commits

..

2 Commits

Author SHA1 Message Date
Michael Hoennig
77aba67af9 basis.currentUser(), basis.assumedRoles() 2024-09-13 09:01:15 +02:00
Michael Hoennig
a7037c4306 basis.tx_history_txid(), basis.tx_operation, basis.tx_create_historicization(...) 2024-09-13 08:56:27 +02:00
10 changed files with 29 additions and 29 deletions

View File

@ -110,7 +110,7 @@ end; $$;
/*
Returns the current user as defined by `basis.defineContext(...)`.
*/
create or replace function currentUser()
create or replace function basis.currentUser()
returns varchar(63)
stable -- leakproof
language plpgsql as $$
@ -134,7 +134,7 @@ end; $$;
Returns assumed role names as set in `hsadminng.assumedRoles`
or empty array, if not set.
*/
create or replace function assumedRoles()
create or replace function basis.assumedRoles()
returns varchar(1023)[]
stable -- leakproof
language plpgsql as $$
@ -213,11 +213,11 @@ create or replace function currentSubjects()
declare
assumedRoles varchar(1023)[];
begin
assumedRoles := assumedRoles();
assumedRoles := basis.assumedRoles();
if array_length(assumedRoles, 1) > 0 then
return assumedRoles;
else
return array [currentUser()]::varchar(1023)[];
return array [basis.currentUser()]::varchar(1023)[];
end if;
end; $$;
@ -226,7 +226,7 @@ create or replace function hasAssumedRole()
stable -- leakproof
language plpgsql as $$
begin
return array_length(assumedRoles(), 1) > 0;
return array_length(basis.assumedRoles(), 1) > 0;
end; $$;
--//

View File

@ -84,7 +84,7 @@ begin
insert
into basis.tx_context (txId, txTimestamp, currentUser, assumedRoles, currentTask, currentRequest)
values ( curTxId, now(),
currentUser(), assumedRoles(), curTask, basis.currentRequest())
basis.currentUser(), basis.assumedRoles(), curTask, basis.currentRequest())
on conflict do nothing;
case tg_op

View File

@ -3,7 +3,7 @@
-- ============================================================================
--changeset hs-global-historization-tx-history-txid:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
create or replace function tx_history_txid()
create or replace function basis.tx_history_txid()
returns xid8 stable
language plpgsql as $$
declare
@ -12,18 +12,18 @@ declare
historicalTxId xid8;
historicalTimestamp timestamp;
begin
select coalesce(current_setting('hsadminng.tx_history_txid', true), '') into historicalTxIdSetting;
select coalesce(current_setting('hsadminng.basis.tx_history_txid', true), '') into historicalTxIdSetting;
select coalesce(current_setting('hsadminng.tx_history_timestamp', true), '') into historicalTimestampSetting;
if historicalTxIdSetting > '' and historicalTimestampSetting > '' then
raise exception 'either hsadminng.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are set: (%, %)',
raise exception 'either hsadminng.basis.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are set: (%, %)',
historicalTxIdSetting, historicalTimestampSetting;
end if;
if historicalTxIdSetting = '' and historicalTimestampSetting = '' then
raise exception 'either hsadminng.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are unset or empty: (%, %)',
raise exception 'either hsadminng.basis.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are unset or empty: (%, %)',
historicalTxIdSetting, historicalTimestampSetting;
end if;
-- just for debugging / making sure the function is only called once per query
-- raise notice 'tx_history_txid() called with: (%, %)', historicalTxIdSetting, historicalTimestampSetting;
-- raise notice 'basis.tx_history_txid() called with: (%, %)', historicalTxIdSetting, historicalTimestampSetting;
if historicalTxIdSetting is null or historicalTxIdSetting = '' then
select historicalTimestampSetting::timestamp into historicalTimestamp;
@ -40,7 +40,7 @@ end; $$;
--changeset hs-global-historization-tx-historicize-tf:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
create type "tx_operation" as enum ('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE');
create type basis.tx_operation as enum ('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE');
create or replace function tx_historicize_tf()
returns trigger
@ -95,7 +95,7 @@ end; $$;
-- ----------------------------------------------------------------------------
create or replace procedure tx_create_historicization(baseTable varchar)
create or replace procedure basis.tx_create_historicization(baseTable varchar)
language plpgsql as $$
declare
createHistTableSql varchar;
@ -111,7 +111,7 @@ begin
'CREATE TABLE ' || baseTable || '_ex (' ||
' version_id serial PRIMARY KEY,' ||
' txid xid8 NOT NULL REFERENCES basis.tx_context(txid),' ||
' trigger_op tx_operation NOT NULL,' ||
' trigger_op basis.tx_operation NOT NULL,' ||
' alive boolean not null,' ||
' LIKE ' || baseTable ||
' EXCLUDING CONSTRAINTS' ||
@ -132,7 +132,7 @@ begin
'CREATE OR REPLACE VIEW %1$s AS' ||
'(' ||
-- make sure the function is only called once, not for every matching row in basis.tx_context
' WITH txh AS (SELECT tx_history_txid() AS txid) ' ||
' WITH txh AS (SELECT basis.tx_history_txid() AS txid) ' ||
' SELECT %2$s' ||
' FROM %3$s' ||
' WHERE alive = TRUE' ||

View File

@ -12,8 +12,8 @@ declare
currentSubjectsUuids uuid[];
begin
-- exactly one role must be assumed, not none not more than one
if cardinality(assumedRoles()) <> 1 then
raise exception '[400] Granting roles to user is only possible if exactly one role is assumed, given: %', assumedRoles();
if cardinality(basis.assumedRoles()) <> 1 then
raise exception '[400] Granting roles to user is only possible if exactly one role is assumed, given: %', basis.assumedRoles();
end if;
currentSubjectsUuids := currentSubjectsUuids();

View File

@ -66,10 +66,10 @@ begin
and r.roleType = roleTypeToAssume
into roleUuidToAssume;
if roleUuidToAssume is null then
raise exception '[403] role % does not exist or is not accessible for user %', roleName, currentUser();
raise exception '[403] role % does not exist or is not accessible for user %', roleName, basis.currentUser();
end if;
if not isGranted(currentUserUuid, roleUuidToAssume) then
raise exception '[403] user % has no permission to assume role %', currentUser(), roleName;
raise exception '[403] user % has no permission to assume role %', basis.currentUser(), roleName;
end if;
roleIdsToAssume := roleIdsToAssume || roleUuidToAssume;
end loop;
@ -132,7 +132,7 @@ begin
currentUserUuid := null;
end;
if (currentUserUuid is null or currentUserUuid = '') then
currentUserName := currentUser();
currentUserName := basis.currentUser();
if (length(currentUserName) > 0) then
raise exception '[401] currentUserUuid cannot be determined, unknown user name "%"', currentUserName;
else
@ -166,7 +166,7 @@ begin
currentSubjectsUuids := null;
end;
if (currentSubjectsUuids is null or length(currentSubjectsUuids) = 0 ) then
currentUserName := currentUser();
currentUserName := basis.currentUser();
if (length(currentUserName) > 0) then
raise exception '[401] currentSubjectsUuids (%) cannot be determined, unknown user name "%"', currentSubjectsUuids, currentUserName;
else

View File

@ -241,7 +241,7 @@ create or replace view RbacUser_rv as
union
select users.*
from RbacUser as users
where cardinality(assumedRoles()) = 0 and
where cardinality(basis.assumedRoles()) = 0 and
(currentUserUuid() = users.uuid or hasGlobalRoleGranted(currentUserUuid()))
) as unordered
@ -303,7 +303,7 @@ begin
delete from RbacUser where uuid = old.uuid;
return old;
end if;
raise exception '[403] User % not allowed to delete user uuid %', currentUser(), old.uuid;
raise exception '[403] User % not allowed to delete user uuid %', basis.currentUser(), old.uuid;
end; $$;
/*
@ -354,7 +354,7 @@ begin
currentUserUuid := currentUserUuid();
if hasGlobalRoleGranted(targetUserUuid) and not hasGlobalRoleGranted(currentUserUuid) then
raise exception '[403] permissions of user "%" are not accessible to user "%"', targetUserUuid, currentUser();
raise exception '[403] permissions of user "%" are not accessible to user "%"', targetUserUuid, basis.currentUser();
end if;
return query select

View File

@ -25,5 +25,5 @@ call basis.create_journal('hs_booking_project');
-- ============================================================================
--changeset hs-booking-project-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call tx_create_historicization('hs_booking_project');
call basis.tx_create_historicization('hs_booking_project');
--//

View File

@ -42,6 +42,6 @@ call basis.create_journal('hs_booking_item');
-- ============================================================================
--changeset hs-booking-item-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call tx_create_historicization('hs_booking_item');
call basis.tx_create_historicization('hs_booking_item');
--//

View File

@ -173,7 +173,7 @@ call basis.create_journal('hs_hosting_asset');
-- ============================================================================
--changeset hs-hosting-asset-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call tx_create_historicization('hs_hosting_asset');
call basis.tx_create_historicization('hs_hosting_asset');
--//

View File

@ -52,7 +52,7 @@ public abstract class ContextBasedTest {
protected void historicalContext(final Long txId) {
// set local cannot be used with query parameters
em.createNativeQuery("""
set local hsadminng.tx_history_txid to ':txid';
set local hsadminng.basis.tx_history_txid to ':txid';
""".replace(":txid", txId.toString())).executeUpdate();
em.createNativeQuery("""
set local hsadminng.tx_history_timestamp to '';
@ -66,7 +66,7 @@ public abstract class ContextBasedTest {
set local hsadminng.tx_history_timestamp to ':timestamp';
""".replace(":timestamp", txTimestamp.toString())).executeUpdate();
em.createNativeQuery("""
set local hsadminng.tx_history_txid to '';
set local hsadminng.basis.tx_history_txid to '';
""").executeUpdate();
}