2022-09-26 10:57:22 +02:00
|
|
|
--liquibase formatted sql
|
2024-03-25 05:57:58 +01:00
|
|
|
-- This code generated was by RbacViewPostgresGenerator, do not amend manually.
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2022-09-26 10:57:22 +02:00
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-OBJECT:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-22 17:23:17 +01:00
|
|
|
call generateRelatedRbacObject('hs_office_relation');
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-22 17:23:17 +01:00
|
|
|
call generateRbacRoleDescriptors('hsOfficeRelation', 'hs_office_relation');
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-insert-trigger:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
2024-03-15 16:39:37 +01:00
|
|
|
Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
|
2022-09-26 10:57:22 +02:00
|
|
|
*/
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create or replace procedure buildRbacSystemForHsOfficeRelation(
|
|
|
|
NEW hs_office_relation
|
2024-03-15 16:39:37 +01:00
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
|
|
|
declare
|
|
|
|
newHolderPerson hs_office_person;
|
|
|
|
newAnchorPerson hs_office_person;
|
|
|
|
newContact hs_office_contact;
|
|
|
|
|
|
|
|
begin
|
|
|
|
call enterTriggerForObjectUuid(NEW.uuid);
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = NEW.holderUuid INTO newHolderPerson;
|
|
|
|
assert newHolderPerson.uuid is not null, format('newHolderPerson must not be null for NEW.holderUuid = %s', NEW.holderUuid);
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = NEW.anchorUuid INTO newAnchorPerson;
|
|
|
|
assert newAnchorPerson.uuid is not null, format('newAnchorPerson must not be null for NEW.anchorUuid = %s', NEW.anchorUuid);
|
2024-03-15 16:39:37 +01:00
|
|
|
|
|
|
|
select * from hs_office_contact as c where c.uuid = NEW.contactUuid INTO newContact;
|
|
|
|
assert newContact.uuid is not null, format('newContact must not be null for NEW.contactUuid = %s', NEW.contactUuid);
|
|
|
|
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-03-22 17:23:17 +01:00
|
|
|
hsOfficeRelationOwner(NEW),
|
2024-03-15 16:39:37 +01:00
|
|
|
permissions => array['DELETE'],
|
|
|
|
incomingSuperRoles => array[globalAdmin()],
|
|
|
|
userUuids => array[currentUserUuid()]
|
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-03-22 17:23:17 +01:00
|
|
|
hsOfficeRelationAdmin(NEW),
|
2024-03-15 16:39:37 +01:00
|
|
|
permissions => array['UPDATE'],
|
|
|
|
incomingSuperRoles => array[
|
2024-03-25 06:08:36 +01:00
|
|
|
hsOfficePersonAdmin(newAnchorPerson),
|
|
|
|
hsOfficeRelationOwner(NEW)]
|
2024-03-15 16:39:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-03-22 17:23:17 +01:00
|
|
|
hsOfficeRelationAgent(NEW),
|
2024-03-15 16:39:37 +01:00
|
|
|
incomingSuperRoles => array[
|
2024-03-25 05:57:58 +01:00
|
|
|
hsOfficeRelationAdmin(NEW),
|
|
|
|
hsOfficePersonAdmin(newHolderPerson)]
|
2024-03-15 16:39:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
2024-03-22 17:23:17 +01:00
|
|
|
hsOfficeRelationTenant(NEW),
|
2024-03-15 16:39:37 +01:00
|
|
|
permissions => array['SELECT'],
|
|
|
|
incomingSuperRoles => array[
|
2024-03-25 06:08:36 +01:00
|
|
|
hsOfficeRelationAgent(NEW),
|
2024-03-25 05:57:58 +01:00
|
|
|
hsOfficePersonAdmin(newHolderPerson),
|
2024-03-25 06:08:36 +01:00
|
|
|
hsOfficeContactAdmin(newContact)],
|
2024-03-15 16:39:37 +01:00
|
|
|
outgoingSubRoles => array[
|
2024-03-25 05:57:58 +01:00
|
|
|
hsOfficePersonReferrer(newAnchorPerson),
|
2024-03-25 06:08:36 +01:00
|
|
|
hsOfficeContactReferrer(newContact),
|
|
|
|
hsOfficePersonReferrer(newHolderPerson)]
|
2024-03-15 16:39:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
call leaveTriggerForObjectUuid(NEW.uuid);
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
2024-03-22 17:23:17 +01:00
|
|
|
AFTER INSERT TRIGGER to create the role+grant structure for a new hs_office_relation row.
|
2024-03-15 16:39:37 +01:00
|
|
|
*/
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create or replace function insertTriggerForHsOfficeRelation_tf()
|
2022-09-26 10:57:22 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
2024-03-15 16:39:37 +01:00
|
|
|
begin
|
2024-03-22 17:23:17 +01:00
|
|
|
call buildRbacSystemForHsOfficeRelation(NEW);
|
2024-03-15 16:39:37 +01:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create trigger insertTriggerForHsOfficeRelation_tg
|
|
|
|
after insert on hs_office_relation
|
2024-03-15 16:39:37 +01:00
|
|
|
for each row
|
2024-03-22 17:23:17 +01:00
|
|
|
execute procedure insertTriggerForHsOfficeRelation_tf();
|
2024-03-15 16:39:37 +01:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-update-trigger:1 endDelimiter:--//
|
2024-03-15 16:39:37 +01:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Called from the AFTER UPDATE TRIGGER to re-wire the grants.
|
|
|
|
*/
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create or replace procedure updateRbacRulesForHsOfficeRelation(
|
|
|
|
OLD hs_office_relation,
|
|
|
|
NEW hs_office_relation
|
2024-03-15 16:39:37 +01:00
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
2022-09-26 10:57:22 +02:00
|
|
|
declare
|
2024-03-15 16:39:37 +01:00
|
|
|
oldHolderPerson hs_office_person;
|
|
|
|
newHolderPerson hs_office_person;
|
|
|
|
oldAnchorPerson hs_office_person;
|
|
|
|
newAnchorPerson hs_office_person;
|
|
|
|
oldContact hs_office_contact;
|
|
|
|
newContact hs_office_contact;
|
|
|
|
|
2022-09-26 10:57:22 +02:00
|
|
|
begin
|
2024-02-24 09:04:07 +01:00
|
|
|
call enterTriggerForObjectUuid(NEW.uuid);
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = OLD.holderUuid INTO oldHolderPerson;
|
|
|
|
assert oldHolderPerson.uuid is not null, format('oldHolderPerson must not be null for OLD.holderUuid = %s', OLD.holderUuid);
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = NEW.holderUuid INTO newHolderPerson;
|
|
|
|
assert newHolderPerson.uuid is not null, format('newHolderPerson must not be null for NEW.holderUuid = %s', NEW.holderUuid);
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = OLD.anchorUuid INTO oldAnchorPerson;
|
|
|
|
assert oldAnchorPerson.uuid is not null, format('oldAnchorPerson must not be null for OLD.anchorUuid = %s', OLD.anchorUuid);
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = NEW.anchorUuid INTO newAnchorPerson;
|
|
|
|
assert newAnchorPerson.uuid is not null, format('newAnchorPerson must not be null for NEW.anchorUuid = %s', NEW.anchorUuid);
|
2024-03-15 16:39:37 +01:00
|
|
|
|
|
|
|
select * from hs_office_contact as c where c.uuid = OLD.contactUuid INTO oldContact;
|
|
|
|
assert oldContact.uuid is not null, format('oldContact must not be null for OLD.contactUuid = %s', OLD.contactUuid);
|
|
|
|
|
|
|
|
select * from hs_office_contact as c where c.uuid = NEW.contactUuid INTO newContact;
|
|
|
|
assert newContact.uuid is not null, format('newContact must not be null for NEW.contactUuid = %s', NEW.contactUuid);
|
|
|
|
|
|
|
|
|
|
|
|
if NEW.contactUuid <> OLD.contactUuid then
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
call revokeRoleFromRole(hsOfficeRelationTenant(OLD), hsOfficeContactAdmin(oldContact));
|
|
|
|
call grantRoleToRole(hsOfficeRelationTenant(NEW), hsOfficeContactAdmin(newContact));
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
call revokeRoleFromRole(hsOfficeContactReferrer(oldContact), hsOfficeRelationTenant(OLD));
|
|
|
|
call grantRoleToRole(hsOfficeContactReferrer(newContact), hsOfficeRelationTenant(NEW));
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2022-09-26 10:57:22 +02:00
|
|
|
end if;
|
|
|
|
|
2024-02-24 09:04:07 +01:00
|
|
|
call leaveTriggerForObjectUuid(NEW.uuid);
|
2022-09-26 10:57:22 +02:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
2024-03-22 17:23:17 +01:00
|
|
|
AFTER INSERT TRIGGER to re-wire the grant structure for a new hs_office_relation row.
|
2022-09-26 10:57:22 +02:00
|
|
|
*/
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create or replace function updateTriggerForHsOfficeRelation_tf()
|
2024-03-15 16:39:37 +01:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2024-03-22 17:23:17 +01:00
|
|
|
call updateRbacRulesForHsOfficeRelation(OLD, NEW);
|
2024-03-15 16:39:37 +01:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create trigger updateTriggerForHsOfficeRelation_tg
|
|
|
|
after update on hs_office_relation
|
2022-09-26 10:57:22 +02:00
|
|
|
for each row
|
2024-03-22 17:23:17 +01:00
|
|
|
execute procedure updateTriggerForHsOfficeRelation_tf();
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
2024-03-15 16:39:37 +01:00
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-INSERT:1 endDelimiter:--//
|
2024-03-15 16:39:37 +01:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-03-15 18:41:02 +01:00
|
|
|
/*
|
2024-03-22 17:23:17 +01:00
|
|
|
Creates INSERT INTO hs_office_relation permissions for the related hs_office_person rows.
|
2024-03-15 18:41:02 +01:00
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
|
|
|
row hs_office_person;
|
|
|
|
permissionUuid uuid;
|
|
|
|
roleUuid uuid;
|
|
|
|
begin
|
2024-03-22 17:23:17 +01:00
|
|
|
call defineContext('create INSERT INTO hs_office_relation permissions for the related hs_office_person rows');
|
2024-03-15 18:41:02 +01:00
|
|
|
|
|
|
|
FOR row IN SELECT * FROM hs_office_person
|
|
|
|
LOOP
|
|
|
|
roleUuid := findRoleId(hsOfficePersonAdmin(row));
|
2024-03-22 17:23:17 +01:00
|
|
|
permissionUuid := createPermission(row.uuid, 'INSERT', 'hs_office_relation');
|
2024-03-15 18:41:02 +01:00
|
|
|
call grantPermissionToRole(permissionUuid, roleUuid);
|
|
|
|
END LOOP;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
/**
|
2024-03-22 17:23:17 +01:00
|
|
|
Adds hs_office_relation INSERT permission to specified role of new hs_office_person rows.
|
2024-03-15 18:41:02 +01:00
|
|
|
*/
|
2024-03-22 17:23:17 +01:00
|
|
|
create or replace function hs_office_relation_hs_office_person_insert_tf()
|
2024-03-15 18:41:02 +01:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
|
|
|
call grantPermissionToRole(
|
2024-03-22 17:23:17 +01:00
|
|
|
createPermission(NEW.uuid, 'INSERT', 'hs_office_relation'),
|
2024-03-15 18:41:02 +01:00
|
|
|
hsOfficePersonAdmin(NEW));
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
2024-03-25 05:57:58 +01:00
|
|
|
-- z_... is to put it at the end of after insert triggers, to make sure the roles exist
|
2024-03-22 17:23:17 +01:00
|
|
|
create trigger z_hs_office_relation_hs_office_person_insert_tg
|
2024-03-15 18:41:02 +01:00
|
|
|
after insert on hs_office_person
|
|
|
|
for each row
|
2024-03-22 17:23:17 +01:00
|
|
|
execute procedure hs_office_relation_hs_office_person_insert_tf();
|
2024-03-15 18:41:02 +01:00
|
|
|
|
|
|
|
/**
|
2024-03-25 05:57:58 +01:00
|
|
|
Checks if the user or assumed roles are allowed to insert a row to hs_office_relation,
|
2024-03-25 06:08:36 +01:00
|
|
|
where the check is performed by an indirect role.
|
2024-03-25 05:57:58 +01:00
|
|
|
|
2024-03-25 06:08:36 +01:00
|
|
|
An indirect role is a role FIXME.
|
2024-03-15 18:41:02 +01:00
|
|
|
*/
|
2024-03-22 17:23:17 +01:00
|
|
|
create or replace function hs_office_relation_insert_permission_missing_tf()
|
2024-03-15 18:41:02 +01:00
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
begin
|
2024-03-25 06:08:36 +01:00
|
|
|
if ( not hasInsertPermission(
|
|
|
|
( SELECT anchorPerson.uuid FROM
|
|
|
|
|
|
|
|
(select * from hs_office_person as p where p.uuid = NEW.anchorUuid) AS anchorPerson
|
|
|
|
|
|
|
|
), 'INSERT', 'hs_office_relation') ) then
|
|
|
|
raise exception
|
|
|
|
'[403] insert into hs_office_relation not allowed for current subjects % (%)',
|
|
|
|
currentSubjects(), currentSubjectsUuids();
|
|
|
|
end if;
|
|
|
|
return NEW;
|
2024-03-15 18:41:02 +01:00
|
|
|
end; $$;
|
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
create trigger hs_office_relation_insert_permission_check_tg
|
|
|
|
before insert on hs_office_relation
|
2024-03-15 16:39:37 +01:00
|
|
|
for each row
|
2024-03-22 17:23:17 +01:00
|
|
|
execute procedure hs_office_relation_insert_permission_missing_tf();
|
2024-03-15 16:39:37 +01:00
|
|
|
--//
|
|
|
|
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2024-03-22 17:23:17 +01:00
|
|
|
call generateRbacIdentityViewFromProjection('hs_office_relation', $idName$
|
|
|
|
(select idName from hs_office_person_iv p where p.uuid = anchorUuid)
|
|
|
|
|| '-with-' || target.type || '-'
|
|
|
|
|| (select idName from hs_office_person_iv p where p.uuid = holderUuid)
|
2024-03-15 16:39:37 +01:00
|
|
|
|
2022-09-26 10:57:22 +02:00
|
|
|
$idName$);
|
|
|
|
--//
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-22 17:23:17 +01:00
|
|
|
--changeset hs-office-relation-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-22 17:23:17 +01:00
|
|
|
call generateRbacRestrictedView('hs_office_relation',
|
2024-03-15 16:39:37 +01:00
|
|
|
$orderBy$
|
2024-03-22 17:23:17 +01:00
|
|
|
(select idName from hs_office_person_iv p where p.uuid = target.holderUuid)
|
2024-03-15 16:39:37 +01:00
|
|
|
$orderBy$,
|
|
|
|
$updates$
|
2024-03-25 05:57:58 +01:00
|
|
|
contactUuid = new.contactUuid
|
2022-09-26 10:57:22 +02:00
|
|
|
$updates$);
|
|
|
|
--//
|
|
|
|
|