2022-09-26 10:57:22 +02:00
|
|
|
--liquibase formatted sql
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-13 15:01:24 +01:00
|
|
|
--changeset hs-office-relation-rbac-OBJECT:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-13 15:01:24 +01:00
|
|
|
call generateRelatedRbacObject('hs_office_relation');
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-13 15:01:24 +01:00
|
|
|
--changeset hs-office-relation-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-13 15:01:24 +01:00
|
|
|
call generateRbacRoleDescriptors('hsOfficeRelation', 'hs_office_relation');
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-13 15:01:24 +01:00
|
|
|
--changeset hs-office-relation-rbac-ROLES-CREATION:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
2024-03-13 15:01:24 +01:00
|
|
|
Creates and updates the roles and their assignments for relation entities.
|
2022-09-26 10:57:22 +02:00
|
|
|
*/
|
|
|
|
|
2024-03-13 15:01:24 +01:00
|
|
|
create or replace function hsOfficeRelationRbacRolesTrigger()
|
2022-09-26 10:57:22 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
declare
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficeRelationTenant RbacRoleDescriptor;
|
|
|
|
newAnchor hs_office_person;
|
|
|
|
newHolder hs_office_person;
|
2022-09-26 10:57:22 +02:00
|
|
|
oldContact hs_office_contact;
|
|
|
|
newContact hs_office_contact;
|
|
|
|
begin
|
2024-02-24 09:04:07 +01:00
|
|
|
call enterTriggerForObjectUuid(NEW.uuid);
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficeRelationTenant := hsOfficeRelationTenant(NEW);
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2024-03-13 15:01:24 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = NEW.anchorUuid into newAnchor;
|
|
|
|
select * from hs_office_person as p where p.uuid = NEW.holderUuid into newHolder;
|
2022-09-26 10:57:22 +02:00
|
|
|
select * from hs_office_contact as c where c.uuid = NEW.contactUuid into newContact;
|
|
|
|
|
|
|
|
if TG_OP = 'INSERT' then
|
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficeRelationOwner(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['DELETE'],
|
2022-10-12 15:48:56 +02:00
|
|
|
incomingSuperRoles => array[
|
2022-09-26 10:57:22 +02:00
|
|
|
globalAdmin(),
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficePersonAdmin(newAnchor)]
|
2022-09-26 10:57:22 +02:00
|
|
|
);
|
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficeRelationAdmin(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['UPDATE'],
|
2024-03-13 15:01:24 +01:00
|
|
|
incomingSuperRoles => array[hsOfficeRelationOwner(NEW)]
|
2022-09-26 10:57:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
-- the tenant role for those related users who can view the data
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficeRelationTenant,
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['SELECT'],
|
2022-10-12 15:48:56 +02:00
|
|
|
incomingSuperRoles => array[
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficeRelationAdmin(NEW),
|
|
|
|
hsOfficePersonAdmin(newAnchor),
|
|
|
|
hsOfficePersonAdmin(newHolder),
|
2022-10-12 15:48:56 +02:00
|
|
|
hsOfficeContactAdmin(newContact)],
|
|
|
|
outgoingSubRoles => array[
|
2024-03-13 15:01:24 +01:00
|
|
|
hsOfficePersonTenant(newAnchor),
|
|
|
|
hsOfficePersonTenant(newHolder),
|
2022-10-12 15:48:56 +02:00
|
|
|
hsOfficeContactTenant(newContact)]
|
2022-09-26 10:57:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
-- anchor and holder admin roles need each others tenant role
|
2024-03-13 15:01:24 +01:00
|
|
|
-- to be able to see the joined relation
|
2022-10-12 15:48:56 +02:00
|
|
|
-- TODO: this can probably be avoided through agent+guest roles
|
2024-03-13 15:01:24 +01:00
|
|
|
call grantRoleToRole(hsOfficePersonTenant(newAnchor), hsOfficePersonAdmin(newHolder));
|
|
|
|
call grantRoleToRole(hsOfficePersonTenant(newHolder), hsOfficePersonAdmin(newAnchor));
|
|
|
|
call grantRoleToRoleIfNotNull(hsOfficePersonTenant(newHolder), hsOfficeContactAdmin(newContact));
|
2022-09-26 10:57:22 +02:00
|
|
|
|
|
|
|
elsif TG_OP = 'UPDATE' then
|
|
|
|
|
|
|
|
if OLD.contactUuid <> NEW.contactUuid then
|
|
|
|
-- nothing but the contact can be updated,
|
2024-03-13 15:01:24 +01:00
|
|
|
-- in other cases, a new relation needs to be created and the old updated
|
2022-09-26 10:57:22 +02:00
|
|
|
|
|
|
|
select * from hs_office_contact as c where c.uuid = OLD.contactUuid into oldContact;
|
|
|
|
|
2024-03-13 15:01:24 +01:00
|
|
|
call revokeRoleFromRole( hsOfficeRelationTenant, hsOfficeContactAdmin(oldContact) );
|
|
|
|
call grantRoleToRole( hsOfficeRelationTenant, hsOfficeContactAdmin(newContact) );
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2024-03-13 15:01:24 +01:00
|
|
|
call revokeRoleFromRole( hsOfficeContactTenant(oldContact), hsOfficeRelationTenant );
|
|
|
|
call grantRoleToRole( hsOfficeContactTenant(newContact), hsOfficeRelationTenant );
|
2022-09-26 10:57:22 +02:00
|
|
|
end if;
|
|
|
|
else
|
|
|
|
raise exception 'invalid usage of TRIGGER';
|
|
|
|
end if;
|
|
|
|
|
2024-02-24 09:04:07 +01:00
|
|
|
call leaveTriggerForObjectUuid(NEW.uuid);
|
2022-09-26 10:57:22 +02:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
An AFTER INSERT TRIGGER which creates the role structure for a new customer.
|
|
|
|
*/
|
2024-03-13 15:01:24 +01:00
|
|
|
create trigger createRbacRolesForHsOfficeRelation_Trigger
|
2022-09-26 10:57:22 +02:00
|
|
|
after insert
|
2024-03-13 15:01:24 +01:00
|
|
|
on hs_office_relation
|
2022-09-26 10:57:22 +02:00
|
|
|
for each row
|
2024-03-13 15:01:24 +01:00
|
|
|
execute procedure hsOfficeRelationRbacRolesTrigger();
|
2022-09-26 10:57:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
An AFTER UPDATE TRIGGER which updates the role structure of a customer.
|
|
|
|
*/
|
2024-03-13 15:01:24 +01:00
|
|
|
create trigger updateRbacRolesForHsOfficeRelation_Trigger
|
2022-09-26 10:57:22 +02:00
|
|
|
after update
|
2024-03-13 15:01:24 +01:00
|
|
|
on hs_office_relation
|
2022-09-26 10:57:22 +02:00
|
|
|
for each row
|
2024-03-13 15:01:24 +01:00
|
|
|
execute procedure hsOfficeRelationRbacRolesTrigger();
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-13 15:01:24 +01:00
|
|
|
--changeset hs-office-relation-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-13 15:01:24 +01:00
|
|
|
call generateRbacIdentityViewFromProjection('hs_office_relation', $idName$
|
|
|
|
(select idName from hs_office_person_iv p where p.uuid = target.anchorUuid)
|
|
|
|
|| '-with-' || target.type || '-' ||
|
|
|
|
(select idName from hs_office_person_iv p where p.uuid = target.holderUuid)
|
2022-09-26 10:57:22 +02:00
|
|
|
$idName$);
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-13 15:01:24 +01:00
|
|
|
--changeset hs-office-relation-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-13 15:01:24 +01:00
|
|
|
call generateRbacRestrictedView('hs_office_relation',
|
|
|
|
'(select idName from hs_office_person_iv p where p.uuid = target.holderUuid)',
|
2022-09-26 10:57:22 +02:00
|
|
|
$updates$
|
|
|
|
contactUuid = new.contactUuid
|
|
|
|
$updates$);
|
|
|
|
--//
|
|
|
|
|
|
|
|
-- TODO: exception if one tries to amend any other column
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-03-13 15:01:24 +01:00
|
|
|
--changeset hs-office-relation-rbac-NEW-RELATHIONSHIP:1 endDelimiter:--//
|
2022-09-26 10:57:22 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
/*
|
2024-03-13 15:01:24 +01:00
|
|
|
Creates a global permission for new-relation and assigns it to the hostsharing admins role.
|
2022-09-26 10:57:22 +02:00
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
|
|
|
addCustomerPermissions uuid[];
|
|
|
|
globalObjectUuid uuid;
|
|
|
|
globalAdminRoleUuid uuid ;
|
|
|
|
begin
|
2024-03-13 15:01:24 +01:00
|
|
|
call defineContext('granting global new-relation permission to global admin role', null, null, null);
|
2022-09-26 10:57:22 +02:00
|
|
|
|
|
|
|
globalAdminRoleUuid := findRoleId(globalAdmin());
|
|
|
|
globalObjectUuid := (select uuid from global);
|
2024-03-13 15:01:24 +01:00
|
|
|
addCustomerPermissions := createPermissions(globalObjectUuid, array ['new-relation']);
|
2022-09-26 10:57:22 +02:00
|
|
|
call grantPermissionsToRole(globalAdminRoleUuid, addCustomerPermissions);
|
|
|
|
end;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used by the trigger to prevent the add-customer to current user respectively assumed roles.
|
|
|
|
*/
|
2024-03-13 15:01:24 +01:00
|
|
|
create or replace function addHsOfficeRelationNotAllowedForCurrentSubjects()
|
2022-09-26 10:57:22 +02:00
|
|
|
returns trigger
|
|
|
|
language PLPGSQL
|
|
|
|
as $$
|
|
|
|
begin
|
2024-03-13 15:01:24 +01:00
|
|
|
raise exception '[403] new-relation not permitted for %',
|
2022-09-26 10:57:22 +02:00
|
|
|
array_to_string(currentSubjects(), ';', 'null');
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Checks if the user or assumed roles are allowed to create a new customer.
|
|
|
|
*/
|
2024-03-13 15:01:24 +01:00
|
|
|
create trigger hs_office_relation_insert_trigger
|
2022-09-26 10:57:22 +02:00
|
|
|
before insert
|
2024-03-13 15:01:24 +01:00
|
|
|
on hs_office_relation
|
2022-09-26 10:57:22 +02:00
|
|
|
for each row
|
2024-03-13 15:01:24 +01:00
|
|
|
-- TODO.spec: who is allowed to create new relations
|
2022-09-26 10:57:22 +02:00
|
|
|
when ( not hasAssumedRole() )
|
2024-03-13 15:01:24 +01:00
|
|
|
execute procedure addHsOfficeRelationNotAllowedForCurrentSubjects();
|
2022-09-26 10:57:22 +02:00
|
|
|
--//
|
|
|
|
|