2022-10-13 13:35:50 +02:00
|
|
|
--liquibase formatted sql
|
2024-03-28 12:15:13 +01:00
|
|
|
-- This code generated was by RbacViewPostgresGenerator, do not amend manually.
|
|
|
|
|
2022-10-13 13:35:50 +02:00
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacObjectGenerator:hs-office-sepamandate-rbac-OBJECT endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-09-18 10:28:21 +02:00
|
|
|
call rbac.generateRelatedRbacObject('hs_office.sepamandate');
|
2022-10-13 13:35:50 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacRoleDescriptorsGenerator:hs-office-sepamandate-rbac-ROLE-DESCRIPTORS endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-09-18 15:58:59 +02:00
|
|
|
call rbac.generateRbacRoleDescriptors('hs_office.sepamandate');
|
2022-10-13 13:35:50 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RolesGrantsAndPermissionsGenerator:hs-office-sepamandate-rbac-insert-trigger endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
2024-03-28 12:15:13 +01:00
|
|
|
Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
|
|
|
|
*/
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create or replace procedure hs_office.sepamandate_build_rbac_system(
|
|
|
|
NEW hs_office.sepamandate
|
2024-03-28 12:15:13 +01:00
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
|
|
|
declare
|
2024-09-18 10:28:21 +02:00
|
|
|
newBankAccount hs_office.bankaccount;
|
|
|
|
newDebitorRel hs_office.relation;
|
2024-03-28 12:15:13 +01:00
|
|
|
|
|
|
|
begin
|
2024-09-16 15:36:37 +02:00
|
|
|
call rbac.enterTriggerForObjectUuid(NEW.uuid);
|
2024-03-28 12:15:13 +01:00
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
SELECT * FROM hs_office.bankaccount WHERE uuid = NEW.bankAccountUuid INTO newBankAccount;
|
2024-03-28 12:15:13 +01:00
|
|
|
assert newBankAccount.uuid is not null, format('newBankAccount must not be null for NEW.bankAccountUuid = %s', NEW.bankAccountUuid);
|
|
|
|
|
|
|
|
SELECT debitorRel.*
|
2024-09-18 10:28:21 +02:00
|
|
|
FROM hs_office.relation debitorRel
|
|
|
|
JOIN hs_office.debitor debitor ON debitor.debitorRelUuid = debitorRel.uuid
|
2024-03-28 12:15:13 +01:00
|
|
|
WHERE debitor.uuid = NEW.debitorUuid
|
|
|
|
INTO newDebitorRel;
|
|
|
|
assert newDebitorRel.uuid is not null, format('newDebitorRel must not be null for NEW.debitorUuid = %s', NEW.debitorUuid);
|
|
|
|
|
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.sepamandate_OWNER(NEW),
|
2024-03-28 12:15:13 +01:00
|
|
|
permissions => array['DELETE'],
|
2024-09-19 13:24:08 +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
|
|
|
);
|
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.sepamandate_ADMIN(NEW),
|
2024-03-28 12:15:13 +01:00
|
|
|
permissions => array['UPDATE'],
|
2024-09-19 12:14:13 +02:00
|
|
|
incomingSuperRoles => array[hs_office.sepamandate_OWNER(NEW)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.sepamandate_AGENT(NEW),
|
|
|
|
incomingSuperRoles => array[hs_office.sepamandate_ADMIN(NEW)],
|
2024-03-28 12:15:13 +01:00
|
|
|
outgoingSubRoles => array[
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.bankaccount_REFERRER(newBankAccount),
|
|
|
|
hs_office.relation_AGENT(newDebitorRel)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
perform rbac.defineRoleWithGrants(
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.sepamandate_REFERRER(NEW),
|
2024-03-28 12:15:13 +01:00
|
|
|
permissions => array['SELECT'],
|
|
|
|
incomingSuperRoles => array[
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.bankaccount_ADMIN(newBankAccount),
|
|
|
|
hs_office.relation_AGENT(newDebitorRel),
|
|
|
|
hs_office.sepamandate_AGENT(NEW)],
|
|
|
|
outgoingSubRoles => array[hs_office.relation_TENANT(newDebitorRel)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
2024-09-16 15:36:37 +02:00
|
|
|
call rbac.leaveTriggerForObjectUuid(NEW.uuid);
|
2024-03-28 12:15:13 +01:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
2024-09-18 10:28:21 +02:00
|
|
|
AFTER INSERT TRIGGER to create the role+grant structure for a new hs_office.sepamandate row.
|
2022-10-13 13:35:50 +02:00
|
|
|
*/
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create or replace function hs_office.sepamandate_build_rbac_system_after_insert_tf()
|
2022-10-13 13:35:50 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2024-09-18 10:28:21 +02:00
|
|
|
call hs_office.sepamandate_build_rbac_system(NEW);
|
2024-03-28 12:15:13 +01:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create trigger build_rbac_system_after_insert_tg
|
|
|
|
after insert on hs_office.sepamandate
|
2024-03-28 12:15:13 +01:00
|
|
|
for each row
|
2024-09-18 10:28:21 +02:00
|
|
|
execute procedure hs_office.sepamandate_build_rbac_system_after_insert_tf();
|
2024-03-28 12:15:13 +01:00
|
|
|
--//
|
2022-10-13 13:35:50 +02:00
|
|
|
|
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset InsertTriggerGenerator:hs-office-sepamandate-rbac-GRANTING-INSERT-PERMISSION endDelimiter:--//
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
-- granting INSERT permission to hs_office.relation ----------------------------
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
/*
|
2024-09-18 10:28:21 +02:00
|
|
|
Grants INSERT INTO hs_office.sepamandate permissions to specified role of pre-existing hs_office.relation rows.
|
2024-03-28 12:15:13 +01:00
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
2024-09-18 10:28:21 +02:00
|
|
|
row hs_office.relation;
|
2024-03-28 12:15:13 +01:00
|
|
|
begin
|
2024-09-18 10:28:21 +02:00
|
|
|
call base.defineContext('create INSERT INTO hs_office.sepamandate permissions for pre-exising hs_office.relation rows');
|
2024-03-28 12:15:13 +01:00
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
FOR row IN SELECT * FROM hs_office.relation
|
2024-04-29 11:43:49 +02:00
|
|
|
WHERE type = 'DEBITOR'
|
2024-03-28 12:15:13 +01:00
|
|
|
LOOP
|
2024-09-16 15:36:37 +02:00
|
|
|
call rbac.grantPermissionToRole(
|
2024-09-18 10:28:21 +02:00
|
|
|
rbac.createPermission(row.uuid, 'INSERT', 'hs_office.sepamandate'),
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.relation_ADMIN(row));
|
2024-03-28 12:15:13 +01:00
|
|
|
END LOOP;
|
2024-04-29 11:43:49 +02:00
|
|
|
end;
|
2024-03-28 12:15:13 +01:00
|
|
|
$$;
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
/**
|
2024-09-18 10:28:21 +02:00
|
|
|
Grants hs_office.sepamandate INSERT permission to specified role of new relation rows.
|
2024-03-28 12:15:13 +01:00
|
|
|
*/
|
2024-09-18 10:28:21 +02:00
|
|
|
create or replace function hs_office.new_sepamandate_grants_insert_to_relation_tf()
|
2024-03-28 12:15:13 +01:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2024-04-23 10:42:24 +02:00
|
|
|
if NEW.type = 'DEBITOR' then
|
2024-09-16 15:36:37 +02:00
|
|
|
call rbac.grantPermissionToRole(
|
2024-09-18 10:28:21 +02:00
|
|
|
rbac.createPermission(NEW.uuid, 'INSERT', 'hs_office.sepamandate'),
|
2024-09-19 12:14:13 +02:00
|
|
|
hs_office.relation_ADMIN(NEW));
|
2024-04-29 11:43:49 +02:00
|
|
|
end if;
|
2024-03-28 12:15:13 +01:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
-- z_... is to put it at the end of after insert triggers, to make sure the roles exist
|
2024-09-18 10:28:21 +02:00
|
|
|
create trigger z_new_sepamandate_grants_after_insert_tg
|
|
|
|
after insert on hs_office.relation
|
2024-03-28 12:15:13 +01:00
|
|
|
for each row
|
2024-09-18 10:28:21 +02:00
|
|
|
execute procedure hs_office.new_sepamandate_grants_insert_to_relation_tf();
|
2022-10-13 13:35:50 +02:00
|
|
|
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
-- ============================================================================
|
2024-09-17 14:21:43 +02:00
|
|
|
--changeset InsertTriggerGenerator:hs-office-sepamandate-rbac-CHECKING-INSERT-PERMISSION endDelimiter:--//
|
2024-04-29 11:43:49 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2024-09-18 10:28:21 +02:00
|
|
|
Checks if the user respectively the assumed roles are allowed to insert a row to hs_office.sepamandate.
|
2024-03-28 12:15:13 +01:00
|
|
|
*/
|
2024-09-18 10:28:21 +02:00
|
|
|
create or replace function hs_office.sepamandate_insert_permission_check_tf()
|
2024-03-28 12:15:13 +01:00
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
declare
|
2024-04-29 11:43:49 +02:00
|
|
|
superObjectUuid uuid;
|
2024-03-28 12:15:13 +01:00
|
|
|
begin
|
2024-04-29 11:43:49 +02:00
|
|
|
-- check INSERT permission via indirect foreign key: NEW.debitorUuid
|
|
|
|
superObjectUuid := (SELECT debitorRel.uuid
|
2024-09-18 10:28:21 +02:00
|
|
|
FROM hs_office.relation debitorRel
|
|
|
|
JOIN hs_office.debitor debitor ON debitor.debitorRelUuid = debitorRel.uuid
|
2024-04-29 11:43:49 +02:00
|
|
|
WHERE debitor.uuid = NEW.debitorUuid
|
|
|
|
);
|
2024-09-18 10:28:21 +02:00
|
|
|
assert superObjectUuid is not null, 'object uuid fetched depending on hs_office.sepamandate.debitorUuid must not be null, also check fetchSql in RBAC DSL';
|
|
|
|
if rbac.hasInsertPermission(superObjectUuid, 'hs_office.sepamandate') then
|
2024-04-29 11:43:49 +02:00
|
|
|
return NEW;
|
2022-10-13 13:35:50 +02:00
|
|
|
end if;
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
raise exception '[403] insert into hs_office.sepamandate values(%) not allowed for current subjects % (%)',
|
2024-09-16 15:36:37 +02:00
|
|
|
NEW, base.currentSubjects(), rbac.currentSubjectOrAssumedRolesUuids();
|
2022-10-13 13:35:50 +02:00
|
|
|
end; $$;
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
create trigger sepamandate_insert_permission_check_tg
|
|
|
|
before insert on hs_office.sepamandate
|
2022-10-13 13:35:50 +02:00
|
|
|
for each row
|
2024-09-18 10:28:21 +02:00
|
|
|
execute procedure hs_office.sepamandate_insert_permission_check_tf();
|
2022-10-13 13:35:50 +02:00
|
|
|
--//
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacIdentityViewGenerator:hs-office-sepamandate-rbac-IDENTITY-VIEW endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-09-18 10:28:21 +02:00
|
|
|
call rbac.generateRbacIdentityViewFromQuery('hs_office.sepamandate',
|
2024-04-29 11:43:49 +02:00
|
|
|
$idName$
|
|
|
|
select sm.uuid as uuid, ba.iban || '-' || sm.validity as idName
|
2024-09-18 10:28:21 +02:00
|
|
|
from hs_office.sepamandate sm
|
|
|
|
join hs_office.bankaccount ba on ba.uuid = sm.bankAccountUuid
|
2024-04-29 11:43:49 +02:00
|
|
|
$idName$);
|
2024-03-28 12:15:13 +01:00
|
|
|
--//
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ============================================================================
|
2024-09-16 15:36:37 +02:00
|
|
|
--changeset RbacRestrictedViewGenerator:hs-office-sepamandate-rbac-RESTRICTED-VIEW endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-09-18 10:28:21 +02:00
|
|
|
call rbac.generateRbacRestrictedView('hs_office.sepamandate',
|
2024-03-28 12:15:13 +01:00
|
|
|
$orderBy$
|
|
|
|
validity
|
|
|
|
$orderBy$,
|
|
|
|
$updates$
|
2022-10-29 11:42:03 +02:00
|
|
|
reference = new.reference,
|
2022-10-28 13:44:48 +02:00
|
|
|
agreement = new.agreement,
|
2022-10-13 13:35:50 +02:00
|
|
|
validity = new.validity
|
|
|
|
$updates$);
|
|
|
|
--//
|
|
|
|
|