2022-09-26 10:57:22 +02:00
|
|
|
--liquibase formatted sql
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-office-relationship-rbac-OBJECT:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
call generateRelatedRbacObject('hs_office_relationship');
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-office-relationship-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
call generateRbacRoleDescriptors('hsOfficeRelationship', 'hs_office_relationship');
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-office-relationship-rbac-ROLES-CREATION:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates and updates the roles and their assignments for relationship entities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
create or replace function hsOfficeRelationshipRbacRolesTrigger()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
declare
|
2024-03-12 08:31:50 +01:00
|
|
|
newAnchorPerson hs_office_person;
|
|
|
|
newHolderPerson 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-12 08:31:50 +01:00
|
|
|
select * from hs_office_person as p where p.uuid = NEW.relAnchorUuid into newAnchorPerson;
|
|
|
|
select * from hs_office_person as p where p.uuid = NEW.relHolderUuid into newHolderPerson;
|
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
|
|
|
|
|
2024-03-12 08:31:50 +01:00
|
|
|
-- cannot be generated using `tools/generate` because there are multiple grants to the same entity type
|
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2022-09-26 10:57:22 +02:00
|
|
|
hsOfficeRelationshipOwner(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['DELETE'],
|
2022-10-12 15:48:56 +02:00
|
|
|
incomingSuperRoles => array[
|
2024-03-12 08:31:50 +01:00
|
|
|
globalAdmin()
|
|
|
|
]
|
|
|
|
);
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2022-09-26 10:57:22 +02:00
|
|
|
hsOfficeRelationshipAdmin(NEW),
|
2024-03-11 12:30:43 +01:00
|
|
|
permissions => array['UPDATE'],
|
2024-03-12 08:31:50 +01:00
|
|
|
incomingSuperRoles => array[
|
|
|
|
hsOfficeRelationshipOwner(NEW),
|
|
|
|
hsOfficePersonAdmin(newAnchorPerson)
|
|
|
|
]
|
|
|
|
);
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-12 08:31:50 +01:00
|
|
|
hsOfficeRelationshipAgent(NEW),
|
2022-10-12 15:48:56 +02:00
|
|
|
incomingSuperRoles => array[
|
|
|
|
hsOfficeRelationshipAdmin(NEW),
|
2024-03-12 08:31:50 +01:00
|
|
|
hsOfficePersonAdmin(newHolderPerson),
|
|
|
|
hsOfficeContactAdmin(newContact)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
|
|
|
hsOfficeRelationshipTenant(NEW),
|
|
|
|
permissions => array['SELECT'],
|
|
|
|
incomingSuperRoles => array[
|
|
|
|
hsOfficeRelationshipAgent(NEW)
|
|
|
|
],
|
2022-10-12 15:48:56 +02:00
|
|
|
outgoingSubRoles => array[
|
2024-03-12 08:31:50 +01:00
|
|
|
hsOfficePersonReferrer(newAnchorPerson),
|
|
|
|
hsOfficePersonReferrer(newHolderPerson),
|
|
|
|
hsOfficeContactReferrer(newContact)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( NEW.relType = 'REPRESENTATIVE' ) then
|
|
|
|
call grantRoleToRole(hsOfficePersonAdmin(newHolderPerson), hsOfficePersonAdmin(newAnchorPerson));
|
|
|
|
end if;
|
2022-09-26 10:57:22 +02:00
|
|
|
|
|
|
|
elsif TG_OP = 'UPDATE' then
|
|
|
|
|
|
|
|
if OLD.contactUuid <> NEW.contactUuid then
|
2024-03-12 08:31:50 +01:00
|
|
|
-- only the contact can be updated,
|
2022-09-26 10:57:22 +02:00
|
|
|
-- in other cases, a new relationship needs to be created and the old updated
|
|
|
|
|
|
|
|
select * from hs_office_contact as c where c.uuid = OLD.contactUuid into oldContact;
|
|
|
|
|
2024-03-12 08:31:50 +01:00
|
|
|
call revokeRoleFromRole( hsOfficeContactReferrer(oldContact), hsOfficeRelationshipTenant(NEW) );
|
|
|
|
call grantRoleToRole( hsOfficeContactReferrer(newContact), hsOfficeRelationshipTenant(NEW) );
|
2022-09-26 10:57:22 +02:00
|
|
|
|
2024-03-12 08:31:50 +01:00
|
|
|
call revokeRoleFromRole( hsOfficeRelationshipAgent(NEW), hsOfficeContactAdmin(oldContact) );
|
|
|
|
call grantRoleToRole( hsOfficeRelationshipAgent(NEW), hsOfficeContactAdmin(newContact) );
|
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.
|
|
|
|
*/
|
|
|
|
create trigger createRbacRolesForHsOfficeRelationship_Trigger
|
|
|
|
after insert
|
|
|
|
on hs_office_relationship
|
|
|
|
for each row
|
|
|
|
execute procedure hsOfficeRelationshipRbacRolesTrigger();
|
|
|
|
|
|
|
|
/*
|
|
|
|
An AFTER UPDATE TRIGGER which updates the role structure of a customer.
|
|
|
|
*/
|
|
|
|
create trigger updateRbacRolesForHsOfficeRelationship_Trigger
|
|
|
|
after update
|
|
|
|
on hs_office_relationship
|
|
|
|
for each row
|
|
|
|
execute procedure hsOfficeRelationshipRbacRolesTrigger();
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-office-relationship-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-11 12:30:43 +01:00
|
|
|
call generateRbacIdentityViewFromProjection('hs_office_relationship', $idName$
|
2022-09-26 10:57:22 +02:00
|
|
|
(select idName from hs_office_person_iv p where p.uuid = target.relAnchorUuid)
|
|
|
|
|| '-with-' || target.relType || '-' ||
|
|
|
|
(select idName from hs_office_person_iv p where p.uuid = target.relHolderUuid)
|
|
|
|
$idName$);
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-office-relationship-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
call generateRbacRestrictedView('hs_office_relationship',
|
2024-03-12 08:31:50 +01:00
|
|
|
'(select idName from hs_office_person_iv p where p.uuid = target.relHolderUuid)',
|
|
|
|
$updates$
|
2022-09-26 10:57:22 +02:00
|
|
|
contactUuid = new.contactUuid
|
|
|
|
$updates$);
|
|
|
|
--//
|
|
|
|
|
|
|
|
-- TODO: exception if one tries to amend any other column
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-office-relationship-rbac-NEW-RELATHIONSHIP:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Creates a global permission for new-relationship and assigns it to the hostsharing admins role.
|
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
|
|
|
addCustomerPermissions uuid[];
|
|
|
|
globalObjectUuid uuid;
|
|
|
|
globalAdminRoleUuid uuid ;
|
|
|
|
begin
|
|
|
|
call defineContext('granting global new-relationship permission to global admin role', null, null, null);
|
|
|
|
|
|
|
|
globalAdminRoleUuid := findRoleId(globalAdmin());
|
|
|
|
globalObjectUuid := (select uuid from global);
|
|
|
|
addCustomerPermissions := createPermissions(globalObjectUuid, array ['new-relationship']);
|
|
|
|
call grantPermissionsToRole(globalAdminRoleUuid, addCustomerPermissions);
|
|
|
|
end;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used by the trigger to prevent the add-customer to current user respectively assumed roles.
|
|
|
|
*/
|
|
|
|
create or replace function addHsOfficeRelationshipNotAllowedForCurrentSubjects()
|
|
|
|
returns trigger
|
|
|
|
language PLPGSQL
|
|
|
|
as $$
|
|
|
|
begin
|
|
|
|
raise exception '[403] new-relationship not permitted for %',
|
|
|
|
array_to_string(currentSubjects(), ';', 'null');
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Checks if the user or assumed roles are allowed to create a new customer.
|
|
|
|
*/
|
|
|
|
create trigger hs_office_relationship_insert_trigger
|
|
|
|
before insert
|
|
|
|
on hs_office_relationship
|
|
|
|
for each row
|
|
|
|
-- TODO.spec: who is allowed to create new relationships
|
|
|
|
when ( not hasAssumedRole() )
|
|
|
|
execute procedure addHsOfficeRelationshipNotAllowedForCurrentSubjects();
|
|
|
|
--//
|
|
|
|
|