2022-09-07 20:24:35 +02:00
|
|
|
--liquibase formatted sql
|
2024-03-28 12:15:13 +01:00
|
|
|
-- This code generated was by RbacViewPostgresGenerator, do not amend manually.
|
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacObjectGenerator:hs-office-person-rbac-OBJECT endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-09-18 10:28:21 +02:00
|
|
|
call rbac.generateRelatedRbacObject('hs_office.person');
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
2022-09-13 10:58:54 +02:00
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacRoleDescriptorsGenerator:hs-office-person-rbac-ROLE-DESCRIPTORS endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-09-23 10:52:37 +02:00
|
|
|
call rbac.generateRbacRoleDescriptors('hs_office.person');
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RolesGrantsAndPermissionsGenerator:hs-office-person-rbac-insert-trigger endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-28 12:15:13 +01:00
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
/*
|
2024-03-28 12:15:13 +01:00
|
|
|
Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
|
2022-09-07 20:24:35 +02:00
|
|
|
*/
|
2024-03-28 12:15:13 +01:00
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create or replace procedure hs_office.person_build_rbac_system(
|
|
|
|
NEW hs_office.person
|
2024-03-28 12:15:13 +01:00
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
|
|
|
declare
|
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
begin
|
2024-09-16 15:36:37 +02:00
|
|
|
call rbac.enterTriggerForObjectUuid(NEW.uuid);
|
2022-09-07 20:24:35 +02:00
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-23 10:52:37 +02:00
|
|
|
hs_office.person_OWNER(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['DELETE'],
|
2024-09-23 10:52:37 +02:00
|
|
|
incomingSuperRoles => array[rbac.global_ADMIN()],
|
2024-09-16 15:36:37 +02:00
|
|
|
subjectUuids => array[rbac.currentSubjectUuid()]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
2022-09-07 20:24:35 +02:00
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-23 10:52:37 +02:00
|
|
|
hs_office.person_ADMIN(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['UPDATE'],
|
2024-09-23 10:52:37 +02:00
|
|
|
incomingSuperRoles => array[hs_office.person_OWNER(NEW)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
2022-09-09 17:43:43 +02:00
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-23 10:52:37 +02:00
|
|
|
hs_office.person_REFERRER(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['SELECT'],
|
2024-09-23 10:52:37 +02:00
|
|
|
incomingSuperRoles => array[hs_office.person_ADMIN(NEW)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
2022-09-07 20:24:35 +02:00
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
call rbac.leaveTriggerForObjectUuid(NEW.uuid);
|
2022-09-07 20:24:35 +02:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
2024-09-18 10:28:21 +02:00
|
|
|
AFTER INSERT TRIGGER to create the role+grant structure for a new hs_office.person row.
|
2022-09-07 20:24:35 +02:00
|
|
|
*/
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create or replace function hs_office.person_build_rbac_system_after_insert_tf()
|
2024-03-28 12:15:13 +01:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2024-09-18 10:28:21 +02:00
|
|
|
call hs_office.person_build_rbac_system(NEW);
|
2024-03-28 12:15:13 +01:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create trigger build_rbac_system_after_insert_tg
|
|
|
|
after insert on hs_office.person
|
2022-09-07 20:24:35 +02:00
|
|
|
for each row
|
2024-09-18 10:28:21 +02:00
|
|
|
execute procedure hs_office.person_build_rbac_system_after_insert_tf();
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacIdentityViewGenerator:hs-office-person-rbac-IDENTITY-VIEW endDelimiter:--//
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
call rbac.generateRbacIdentityViewFromProjection('hs_office.person',
|
2024-03-28 12:15:13 +01:00
|
|
|
$idName$
|
|
|
|
concat(tradeName, familyName, givenName)
|
|
|
|
$idName$);
|
|
|
|
--//
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacRestrictedViewGenerator:hs-office-person-rbac-RESTRICTED-VIEW endDelimiter:--//
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-09-18 10:28:21 +02:00
|
|
|
call rbac.generateRbacRestrictedView('hs_office.person',
|
2024-03-28 12:15:13 +01:00
|
|
|
$orderBy$
|
|
|
|
concat(tradeName, familyName, givenName)
|
|
|
|
$orderBy$,
|
|
|
|
$updates$
|
|
|
|
personType = new.personType,
|
2024-04-08 10:14:22 +02:00
|
|
|
title = new.title,
|
|
|
|
salutation = new.salutation,
|
2024-03-28 12:15:13 +01:00
|
|
|
tradeName = new.tradeName,
|
|
|
|
givenName = new.givenName,
|
|
|
|
familyName = new.familyName
|
|
|
|
$updates$);
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
2025-01-02 10:02:47 +01:00
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset RbacRbacSystemRebuildGenerator:hs-office-person-rbac-rebuild endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- HOWTO: Rebuild RBAC-system for table hs_office.person after changing its RBAC specification.
|
|
|
|
--
|
|
|
|
-- begin transaction;
|
|
|
|
-- call base.defineContext('re-creating RBAC for table hs_office.person', null, <<insert executing global admin user here>>);
|
|
|
|
-- call hs_office.person_rebuild_rbac_system();
|
|
|
|
-- commit;
|
|
|
|
--
|
|
|
|
-- How it works:
|
|
|
|
-- 1. All grants previously created from the RBAC specification of this table will be deleted.
|
|
|
|
-- These grants are identified by `hs_office.person.grantedByTriggerOf IS NOT NULL`.
|
|
|
|
-- User-induced grants (`hs_office.person.grantedByTriggerOf IS NULL`) are NOT deleted.
|
|
|
|
-- 2. New role types will be created, but existing role types which are not specified anymore,
|
|
|
|
-- will NOT be deleted!
|
|
|
|
-- 3. All newly specified grants will be created.
|
|
|
|
--
|
|
|
|
-- IMPORTANT:
|
|
|
|
-- Make sure not to skip any previously defined role-types or you might break indirect grants!
|
|
|
|
-- E.g. If, in an updated version of the RBAC system for a table, you remove the AGENT role type
|
|
|
|
-- and now directly grant the TENANT role to the ADMIN role, all external grants to the AGENT role
|
|
|
|
-- of this table would be in a dead end.
|
|
|
|
|
|
|
|
create or replace procedure hs_office.person_rebuild_rbac_system()
|
|
|
|
language plpgsql as $$
|
|
|
|
DECLARE
|
|
|
|
DECLARE
|
|
|
|
row hs_office.person;
|
|
|
|
grantsAfter numeric;
|
|
|
|
grantsBefore numeric;
|
|
|
|
BEGIN
|
2025-01-02 12:39:18 +01:00
|
|
|
SELECT count(*) INTO grantsBefore FROM rbac.grant;
|
2025-01-02 10:02:47 +01:00
|
|
|
|
|
|
|
FOR row IN SELECT * FROM hs_office.person LOOP
|
|
|
|
-- first delete all generated grants for this row from the previously defined RBAC system
|
2025-01-02 12:39:18 +01:00
|
|
|
DELETE FROM rbac.grant g
|
2025-01-02 10:02:47 +01:00
|
|
|
WHERE g.grantedbytriggerof = row.uuid;
|
|
|
|
|
|
|
|
-- then build the grants according to the currently defined RBAC rules
|
|
|
|
CALL hs_office.person_build_rbac_system(row);
|
|
|
|
END LOOP;
|
|
|
|
|
2025-01-02 12:39:18 +01:00
|
|
|
select count(*) into grantsAfter from rbac.grant;
|
2025-01-02 10:02:47 +01:00
|
|
|
|
|
|
|
-- print how the total count of grants has changed
|
|
|
|
raise notice 'total grant count before -> after: % -> %', grantsBefore, grantsAfter;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
--//
|
|
|
|
|