2022-09-06 19:43:15 +02:00
|
|
|
--liquibase formatted sql
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-OBJECT:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-09-13 13:27:52 +02:00
|
|
|
call generateRelatedRbacObject('hs_office_contact');
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|
2022-09-13 10:58:54 +02:00
|
|
|
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-09-16 15:25:58 +02:00
|
|
|
call generateRbacRoleDescriptors('hsOfficeContact', 'hs_office_contact');
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-ROLES-CREATION:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates the roles and their assignments for a new contact for the AFTER INSERT TRIGGER.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 09:24:19 +02:00
|
|
|
create or replace function createRbacRolesForHsOfficeContact()
|
2022-09-06 19:43:15 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
declare
|
2022-09-07 14:04:45 +02:00
|
|
|
ownerRole uuid;
|
2022-09-09 17:43:43 +02:00
|
|
|
adminRole uuid;
|
2022-09-06 19:43:15 +02:00
|
|
|
begin
|
|
|
|
if TG_OP <> 'INSERT' then
|
|
|
|
raise exception 'invalid usage of TRIGGER AFTER INSERT';
|
|
|
|
end if;
|
|
|
|
|
2022-09-07 14:04:45 +02:00
|
|
|
-- the owner role with full access for the creator assigned to the current user
|
2022-09-09 17:43:43 +02:00
|
|
|
ownerRole := createRole(
|
2022-09-13 13:27:52 +02:00
|
|
|
hsOfficeContactOwner(NEW),
|
2022-09-06 19:43:15 +02:00
|
|
|
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['*']),
|
|
|
|
beneathRole(globalAdmin()),
|
|
|
|
withoutSubRoles(),
|
2022-09-07 10:25:36 +02:00
|
|
|
withUser(currentUser()), -- TODO.spec: Who is owner of a new contact?
|
2022-09-06 19:43:15 +02:00
|
|
|
grantedByRole(globalAdmin())
|
|
|
|
);
|
|
|
|
|
2022-09-09 17:43:43 +02:00
|
|
|
-- the tenant role for those related users who can view the data
|
|
|
|
adminRole := createRole(
|
2022-09-13 13:27:52 +02:00
|
|
|
hsOfficeContactAdmin(NEW),
|
2022-09-09 17:43:43 +02:00
|
|
|
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['edit']),
|
|
|
|
beneathRole(ownerRole)
|
|
|
|
);
|
|
|
|
|
2022-09-06 19:43:15 +02:00
|
|
|
-- the tenant role for those related users who can view the data
|
|
|
|
perform createRole(
|
2022-09-13 13:27:52 +02:00
|
|
|
hsOfficeContactTenant(NEW),
|
2022-09-06 19:43:15 +02:00
|
|
|
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['view']),
|
2022-09-09 17:43:43 +02:00
|
|
|
beneathRole(adminRole)
|
2022-09-06 19:43:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
An AFTER INSERT TRIGGER which creates the role structure for a new customer.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 09:24:19 +02:00
|
|
|
create trigger createRbacRolesForHsOfficeContact_Trigger
|
2022-09-06 19:43:15 +02:00
|
|
|
after insert
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_contact
|
2022-09-06 19:43:15 +02:00
|
|
|
for each row
|
2022-09-14 09:24:19 +02:00
|
|
|
execute procedure createRbacRolesForHsOfficeContact();
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2022-09-16 16:14:39 +02:00
|
|
|
call generateRbacIdentityView('hs_office_contact', $idName$
|
|
|
|
target.label
|
|
|
|
$idName$);
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Creates a view to the contact main table with row-level limitation
|
|
|
|
based on the 'view' permission of the current user or assumed roles.
|
|
|
|
*/
|
|
|
|
set session session authorization default;
|
2022-09-13 13:27:52 +02:00
|
|
|
drop view if exists hs_office_contact_rv;
|
|
|
|
create or replace view hs_office_contact_rv as
|
2022-09-06 19:43:15 +02:00
|
|
|
select target.*
|
2022-09-13 13:27:52 +02:00
|
|
|
from hs_office_contact as target
|
|
|
|
where target.uuid in (select queryAccessibleObjectUuidsOfSubjectIds('view', 'hs_office_contact', currentSubjectsUuids()));
|
|
|
|
grant all privileges on hs_office_contact_rv to restricted;
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-INSTEAD-OF-INSERT-TRIGGER:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2022-09-13 13:27:52 +02:00
|
|
|
Instead of insert trigger function for hs_office_contact_rv.
|
2022-09-06 19:43:15 +02:00
|
|
|
*/
|
2022-09-14 12:16:44 +02:00
|
|
|
create or replace function insertHsOfficeContact()
|
2022-09-06 19:43:15 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
declare
|
2022-09-13 13:27:52 +02:00
|
|
|
newUser hs_office_contact;
|
2022-09-06 19:43:15 +02:00
|
|
|
begin
|
|
|
|
insert
|
2022-09-13 13:27:52 +02:00
|
|
|
into hs_office_contact
|
2022-09-06 19:43:15 +02:00
|
|
|
values (new.*)
|
|
|
|
returning * into newUser;
|
|
|
|
return newUser;
|
|
|
|
end;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
/*
|
2022-09-13 13:27:52 +02:00
|
|
|
Creates an instead of insert trigger for the hs_office_contact_rv view.
|
2022-09-06 19:43:15 +02:00
|
|
|
*/
|
2022-09-14 12:16:44 +02:00
|
|
|
create trigger insertHsOfficeContact_Trigger
|
2022-09-06 19:43:15 +02:00
|
|
|
instead of insert
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_contact_rv
|
2022-09-06 19:43:15 +02:00
|
|
|
for each row
|
2022-09-14 12:16:44 +02:00
|
|
|
execute function insertHsOfficeContact();
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-INSTEAD-OF-DELETE-TRIGGER:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2022-09-13 13:27:52 +02:00
|
|
|
Instead of delete trigger function for hs_office_contact_rv.
|
2022-09-12 16:27:17 +02:00
|
|
|
|
|
|
|
Checks if the current subject (user / assumed role) has the permission to delete the row.
|
2022-09-06 19:43:15 +02:00
|
|
|
*/
|
2022-09-14 12:16:44 +02:00
|
|
|
create or replace function deleteHsOfficeContact()
|
2022-09-06 19:43:15 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
begin
|
2022-09-09 17:43:43 +02:00
|
|
|
if hasGlobalRoleGranted(currentUserUuid()) or
|
2022-09-13 13:27:52 +02:00
|
|
|
old.uuid in (select queryAccessibleObjectUuidsOfSubjectIds('delete', 'hs_office_contact', currentSubjectsUuids())) then
|
|
|
|
delete from hs_office_contact c where c.uuid = old.uuid;
|
2022-09-06 19:43:15 +02:00
|
|
|
return old;
|
|
|
|
end if;
|
|
|
|
raise exception '[403] User % not allowed to delete contact uuid %', currentUser(), old.uuid;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
2022-09-13 13:27:52 +02:00
|
|
|
Creates an instead of delete trigger for the hs_office_contact_rv view.
|
2022-09-06 19:43:15 +02:00
|
|
|
*/
|
2022-09-14 12:16:44 +02:00
|
|
|
create trigger deleteHsOfficeContact_Trigger
|
2022-09-06 19:43:15 +02:00
|
|
|
instead of delete
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_contact_rv
|
2022-09-06 19:43:15 +02:00
|
|
|
for each row
|
2022-09-14 12:16:44 +02:00
|
|
|
execute function deleteHsOfficeContact();
|
2022-09-06 19:43:15 +02:00
|
|
|
--/
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-contact-rbac-NEW-CONTACT:1 endDelimiter:--//
|
2022-09-06 19:43:15 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
/*
|
2022-09-07 14:04:45 +02:00
|
|
|
Creates a global permission for new-contact and assigns it to the hostsharing admins role.
|
2022-09-06 19:43:15 +02:00
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
|
|
|
addCustomerPermissions uuid[];
|
|
|
|
globalObjectUuid uuid;
|
|
|
|
globalAdminRoleUuid uuid ;
|
|
|
|
begin
|
2022-09-07 14:04:45 +02:00
|
|
|
call defineContext('granting global new-contact permission to global admin role', null, null, null);
|
2022-09-06 19:43:15 +02:00
|
|
|
|
|
|
|
globalAdminRoleUuid := findRoleId(globalAdmin());
|
|
|
|
globalObjectUuid := (select uuid from global);
|
2022-09-07 14:04:45 +02:00
|
|
|
addCustomerPermissions := createPermissions(globalObjectUuid, array ['new-contact']);
|
2022-09-06 19:43:15 +02:00
|
|
|
call grantPermissionsToRole(globalAdminRoleUuid, addCustomerPermissions);
|
|
|
|
end;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Used by the trigger to prevent the add-customer to current user respectively assumed roles.
|
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function addHsOfficeContactNotAllowedForCurrentSubjects()
|
2022-09-06 19:43:15 +02:00
|
|
|
returns trigger
|
|
|
|
language PLPGSQL
|
|
|
|
as $$
|
|
|
|
begin
|
2022-09-07 14:04:45 +02:00
|
|
|
raise exception '[403] new-contact not permitted for %',
|
2022-09-06 19:43:15 +02:00
|
|
|
array_to_string(currentSubjects(), ';', 'null');
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Checks if the user or assumed roles are allowed to create a new customer.
|
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create trigger hs_office_contact_insert_trigger
|
2022-09-06 19:43:15 +02:00
|
|
|
before insert
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_contact
|
2022-09-06 19:43:15 +02:00
|
|
|
for each row
|
|
|
|
-- TODO.spec: who is allowed to create new contacts
|
|
|
|
when ( not hasAssumedRole() )
|
2022-09-13 13:27:52 +02:00
|
|
|
execute procedure addHsOfficeContactNotAllowedForCurrentSubjects();
|
2022-09-06 19:43:15 +02:00
|
|
|
--//
|
|
|
|
|