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
|
|
|
|
|
|
|
-- ============================================================================
|
2022-10-13 18:53:38 +02:00
|
|
|
--changeset hs-office-sepamandate-rbac-OBJECT:1 endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-10-13 18:53:38 +02:00
|
|
|
call generateRelatedRbacObject('hs_office_sepamandate');
|
2022-10-13 13:35:50 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-10-13 18:53:38 +02:00
|
|
|
--changeset hs-office-sepamandate-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-10-13 18:53:38 +02:00
|
|
|
call generateRbacRoleDescriptors('hsOfficeSepaMandate', 'hs_office_sepamandate');
|
2022-10-13 13:35:50 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-28 12:15:13 +01:00
|
|
|
--changeset hs-office-sepamandate-rbac-insert-trigger:1 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
create or replace procedure buildRbacSystemForHsOfficeSepaMandate(
|
|
|
|
NEW hs_office_sepamandate
|
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
|
|
|
declare
|
|
|
|
newBankAccount hs_office_bankaccount;
|
|
|
|
newDebitorRel hs_office_relation;
|
|
|
|
|
|
|
|
begin
|
2024-09-13 17:31:08 +02:00
|
|
|
call rbac.enterTriggerForObjectUuid(NEW.uuid);
|
2024-03-28 12:15:13 +01:00
|
|
|
|
|
|
|
SELECT * FROM hs_office_bankaccount WHERE uuid = NEW.bankAccountUuid INTO newBankAccount;
|
|
|
|
assert newBankAccount.uuid is not null, format('newBankAccount must not be null for NEW.bankAccountUuid = %s', NEW.bankAccountUuid);
|
|
|
|
|
|
|
|
SELECT debitorRel.*
|
|
|
|
FROM hs_office_relation debitorRel
|
|
|
|
JOIN hs_office_debitor debitor ON debitor.debitorRelUuid = debitorRel.uuid
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeSepaMandateOWNER(NEW),
|
2024-03-28 12:15:13 +01:00
|
|
|
permissions => array['DELETE'],
|
2024-04-02 12:01:37 +02:00
|
|
|
incomingSuperRoles => array[globalADMIN()],
|
2024-09-13 16:20:14 +02:00
|
|
|
subjectUuids => array[rbac.currentSubjectUuid()]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeSepaMandateADMIN(NEW),
|
2024-03-28 12:15:13 +01:00
|
|
|
permissions => array['UPDATE'],
|
2024-04-02 12:01:37 +02:00
|
|
|
incomingSuperRoles => array[hsOfficeSepaMandateOWNER(NEW)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeSepaMandateAGENT(NEW),
|
|
|
|
incomingSuperRoles => array[hsOfficeSepaMandateADMIN(NEW)],
|
2024-03-28 12:15:13 +01:00
|
|
|
outgoingSubRoles => array[
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeBankAccountREFERRER(newBankAccount),
|
|
|
|
hsOfficeRelationAGENT(newDebitorRel)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeSepaMandateREFERRER(NEW),
|
2024-03-28 12:15:13 +01:00
|
|
|
permissions => array['SELECT'],
|
|
|
|
incomingSuperRoles => array[
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeBankAccountADMIN(newBankAccount),
|
|
|
|
hsOfficeRelationAGENT(newDebitorRel),
|
|
|
|
hsOfficeSepaMandateAGENT(NEW)],
|
|
|
|
outgoingSubRoles => array[hsOfficeRelationTENANT(newDebitorRel)]
|
2024-03-28 12:15:13 +01:00
|
|
|
);
|
|
|
|
|
2024-09-13 17:31:08 +02:00
|
|
|
call rbac.leaveTriggerForObjectUuid(NEW.uuid);
|
2024-03-28 12:15:13 +01:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
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-03-28 12:15:13 +01:00
|
|
|
create or replace function insertTriggerForHsOfficeSepaMandate_tf()
|
2022-10-13 13:35:50 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2024-03-28 12:15:13 +01:00
|
|
|
call buildRbacSystemForHsOfficeSepaMandate(NEW);
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
create trigger insertTriggerForHsOfficeSepaMandate_tg
|
|
|
|
after insert on hs_office_sepamandate
|
|
|
|
for each row
|
|
|
|
execute procedure insertTriggerForHsOfficeSepaMandate_tf();
|
|
|
|
--//
|
2022-10-13 13:35:50 +02:00
|
|
|
|
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ============================================================================
|
2024-04-29 11:43:49 +02:00
|
|
|
--changeset hs-office-sepamandate-rbac-GRANTING-INSERT-PERMISSION:1 endDelimiter:--//
|
2024-03-28 12:15:13 +01:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-10-13 13:35:50 +02:00
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
-- granting INSERT permission to hs_office_relation ----------------------------
|
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
/*
|
2024-04-29 11:43:49 +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
|
|
|
|
row hs_office_relation;
|
|
|
|
begin
|
2024-09-13 08:16:22 +02:00
|
|
|
call basis.defineContext('create INSERT INTO hs_office_sepamandate permissions for pre-exising hs_office_relation rows');
|
2024-03-28 12:15:13 +01: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
|
|
|
|
call grantPermissionToRole(
|
2024-04-29 11:43:49 +02:00
|
|
|
createPermission(row.uuid, 'INSERT', 'hs_office_sepamandate'),
|
|
|
|
hsOfficeRelationADMIN(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-04-29 11:43:49 +02:00
|
|
|
Grants hs_office_sepamandate INSERT permission to specified role of new hs_office_relation rows.
|
2024-03-28 12:15:13 +01:00
|
|
|
*/
|
2024-04-29 11:43:49 +02:00
|
|
|
create or replace function new_hs_office_sepamandate_grants_insert_to_hs_office_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-04-29 11:43:49 +02:00
|
|
|
call grantPermissionToRole(
|
2024-03-28 12:15:13 +01:00
|
|
|
createPermission(NEW.uuid, 'INSERT', 'hs_office_sepamandate'),
|
2024-04-02 12:01:37 +02:00
|
|
|
hsOfficeRelationADMIN(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-04-29 11:43:49 +02:00
|
|
|
create trigger z_new_hs_office_sepamandate_grants_insert_to_hs_office_relation_tg
|
2024-03-28 12:15:13 +01:00
|
|
|
after insert on hs_office_relation
|
|
|
|
for each row
|
2024-04-29 11:43:49 +02:00
|
|
|
execute procedure new_hs_office_sepamandate_grants_insert_to_hs_office_relation_tf();
|
2022-10-13 13:35:50 +02:00
|
|
|
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs_office_sepamandate-rbac-CHECKING-INSERT-PERMISSION:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
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
|
|
|
*/
|
|
|
|
create or replace function hs_office_sepamandate_insert_permission_check_tf()
|
|
|
|
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
|
|
|
|
FROM hs_office_relation debitorRel
|
|
|
|
JOIN hs_office_debitor debitor ON debitor.debitorRelUuid = debitorRel.uuid
|
|
|
|
WHERE debitor.uuid = NEW.debitorUuid
|
|
|
|
);
|
|
|
|
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 hasInsertPermission(superObjectUuid, 'hs_office_sepamandate') then
|
|
|
|
return NEW;
|
2022-10-13 13:35:50 +02:00
|
|
|
end if;
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2024-08-05 11:48:33 +02:00
|
|
|
raise exception '[403] insert into hs_office_sepamandate values(%) not allowed for current subjects % (%)',
|
2024-09-13 12:44:56 +02:00
|
|
|
NEW, currentSubjects(), rbac.currentSubjectOrAssumedRolesUuids();
|
2022-10-13 13:35:50 +02:00
|
|
|
end; $$;
|
|
|
|
|
2024-03-28 12:15:13 +01:00
|
|
|
create trigger hs_office_sepamandate_insert_permission_check_tg
|
|
|
|
before insert on hs_office_sepamandate
|
2022-10-13 13:35:50 +02:00
|
|
|
for each row
|
2024-03-28 12:15:13 +01: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
|
|
|
-- ============================================================================
|
2022-10-13 18:53:38 +02:00
|
|
|
--changeset hs-office-sepamandate-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
call generateRbacIdentityViewFromQuery('hs_office_sepamandate',
|
|
|
|
$idName$
|
|
|
|
select sm.uuid as uuid, ba.iban || '-' || sm.validity as idName
|
2024-03-28 12:15:13 +01: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
|
|
|
-- ============================================================================
|
2022-10-13 18:53:38 +02:00
|
|
|
--changeset hs-office-sepamandate-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
2022-10-13 13:35:50 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-10-13 18:53:38 +02:00
|
|
|
call 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$);
|
|
|
|
--//
|
|
|
|
|