2022-09-07 20:24:35 +02:00
|
|
|
--liquibase formatted sql
|
|
|
|
|
2022-09-13 10:58:54 +02:00
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-OBJECT:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-09-13 13:27:52 +02:00
|
|
|
call generateRelatedRbacObject('hs_office_person');
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
2022-09-13 10:58:54 +02:00
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function hsOfficePersonOwner(person hs_office_person)
|
2022-09-07 20:24:35 +02:00
|
|
|
returns RbacRoleDescriptor
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2022-09-13 13:27:52 +02:00
|
|
|
return roleDescriptor('hs_office_person', person.uuid, 'owner');
|
2022-09-07 20:24:35 +02:00
|
|
|
end; $$;
|
|
|
|
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function hsOfficePersonAdmin(person hs_office_person)
|
2022-09-07 20:24:35 +02:00
|
|
|
returns RbacRoleDescriptor
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2022-09-13 13:27:52 +02:00
|
|
|
return roleDescriptor('hs_office_person', person.uuid, 'admin');
|
2022-09-07 20:24:35 +02:00
|
|
|
end; $$;
|
|
|
|
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function hsOfficePersonTenant(person hs_office_person)
|
2022-09-07 20:24:35 +02:00
|
|
|
returns RbacRoleDescriptor
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2022-09-13 13:27:52 +02:00
|
|
|
return roleDescriptor('hs_office_person', person.uuid, 'tenant');
|
2022-09-07 20:24:35 +02:00
|
|
|
end; $$;
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-ROLES-CREATION:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates the roles and their assignments for a new person for the AFTER INSERT TRIGGER.
|
|
|
|
*/
|
|
|
|
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function createRbacRolesForHsOfficePerson()
|
2022-09-07 20:24:35 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
declare
|
|
|
|
ownerRole uuid;
|
2022-09-09 17:43:43 +02:00
|
|
|
adminRole uuid;
|
2022-09-07 20:24:35 +02:00
|
|
|
begin
|
|
|
|
if TG_OP <> 'INSERT' then
|
|
|
|
raise exception 'invalid usage of TRIGGER AFTER INSERT';
|
|
|
|
end if;
|
|
|
|
|
|
|
|
-- 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
|
|
|
hsOfficePersonOwner(NEW),
|
2022-09-12 16:27:17 +02:00
|
|
|
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['*']),
|
|
|
|
beneathRole(globalAdmin()),
|
|
|
|
withoutSubRoles(),
|
|
|
|
withUser(currentUser()), -- TODO.spec: Who is owner of a new person?
|
|
|
|
grantedByRole(globalAdmin())
|
2022-09-07 20:24:35 +02:00
|
|
|
);
|
|
|
|
|
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
|
|
|
hsOfficePersonAdmin(NEW),
|
2022-09-09 17:43:43 +02:00
|
|
|
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['edit']),
|
|
|
|
beneathRole(ownerRole)
|
|
|
|
);
|
|
|
|
|
2022-09-07 20:24:35 +02:00
|
|
|
-- the tenant role for those related users who can view the data
|
|
|
|
perform createRole(
|
2022-09-13 13:27:52 +02:00
|
|
|
hsOfficePersonTenant(NEW),
|
2022-09-12 16:27:17 +02:00
|
|
|
grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['view']),
|
|
|
|
beneathRole(adminRole)
|
2022-09-07 20:24:35 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
An AFTER INSERT TRIGGER which creates the role structure for a new customer.
|
|
|
|
*/
|
|
|
|
|
2022-09-13 13:27:52 +02:00
|
|
|
create trigger createRbacRolesForHsOfficePerson_Trigger
|
2022-09-07 20:24:35 +02:00
|
|
|
after insert
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_person
|
2022-09-07 20:24:35 +02:00
|
|
|
for each row
|
2022-09-13 13:27:52 +02:00
|
|
|
execute procedure createRbacRolesForHsOfficePerson();
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates a view to the person main table which maps the identifying name
|
|
|
|
(in this case, the prefix) to the objectUuid.
|
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace view hs_office_person_iv as
|
2022-09-07 20:24:35 +02:00
|
|
|
select target.uuid, cleanIdentifier(concat(target.tradeName, target.familyName, target.givenName)) as idName
|
2022-09-13 13:27:52 +02:00
|
|
|
from hs_office_person as target;
|
2022-09-07 20:24:35 +02:00
|
|
|
-- TODO.spec: Is it ok that everybody has access to this information?
|
2022-09-13 13:27:52 +02:00
|
|
|
grant all privileges on hs_office_person_iv to restricted;
|
2022-09-07 20:24:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Returns the objectUuid for a given identifying name (in this case the prefix).
|
|
|
|
*/
|
2022-09-15 13:32:01 +02:00
|
|
|
create or replace function hs_office_personUuidByIdName(idName varchar)
|
2022-09-07 20:24:35 +02:00
|
|
|
returns uuid
|
|
|
|
language sql
|
|
|
|
strict as $$
|
2022-09-15 13:32:01 +02:00
|
|
|
select uuid from hs_office_person_iv iv where iv.idName = hs_office_personUuidByIdName.idName;
|
2022-09-07 20:24:35 +02:00
|
|
|
$$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns the identifying name for a given objectUuid (in this case the label).
|
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function hs_office_personIdNameByUuid(uuid uuid)
|
2022-09-07 20:24:35 +02:00
|
|
|
returns varchar
|
|
|
|
language sql
|
|
|
|
strict as $$
|
2022-09-13 13:27:52 +02:00
|
|
|
select idName from hs_office_person_iv iv where iv.uuid = hs_office_personIdNameByUuid.uuid;
|
2022-09-07 20:24:35 +02:00
|
|
|
$$;
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Creates a view to the person 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_person_rv;
|
|
|
|
create or replace view hs_office_person_rv as
|
2022-09-07 20:24:35 +02:00
|
|
|
select target.*
|
2022-09-13 13:27:52 +02:00
|
|
|
from hs_office_person as target
|
|
|
|
where target.uuid in (select queryAccessibleObjectUuidsOfSubjectIds('view', 'hs_office_person', currentSubjectsUuids()));
|
|
|
|
grant all privileges on hs_office_person_rv to restricted;
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-INSTEAD-OF-INSERT-TRIGGER:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2022-09-13 13:27:52 +02:00
|
|
|
Instead of insert trigger function for hs_office_person_rv.
|
2022-09-07 20:24:35 +02:00
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function insertHsOfficePerson()
|
2022-09-07 20:24:35 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
declare
|
2022-09-13 13:27:52 +02:00
|
|
|
newUser hs_office_person;
|
2022-09-07 20:24:35 +02:00
|
|
|
begin
|
|
|
|
insert
|
2022-09-13 13:27:52 +02:00
|
|
|
into hs_office_person
|
2022-09-07 20:24:35 +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_person_rv view.
|
2022-09-07 20:24:35 +02:00
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create trigger insertHsOfficePerson_Trigger
|
2022-09-07 20:24:35 +02:00
|
|
|
instead of insert
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_person_rv
|
2022-09-07 20:24:35 +02:00
|
|
|
for each row
|
2022-09-13 13:27:52 +02:00
|
|
|
execute function insertHsOfficePerson();
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-INSTEAD-OF-DELETE-TRIGGER:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2022-09-13 13:27:52 +02:00
|
|
|
Instead of delete trigger function for hs_office_person_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-07 20:24:35 +02:00
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create or replace function deleteHsOfficePerson()
|
2022-09-07 20:24:35 +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_person', currentSubjectsUuids())) then
|
|
|
|
delete from hs_office_person c where c.uuid = old.uuid;
|
2022-09-07 20:24:35 +02:00
|
|
|
return old;
|
|
|
|
end if;
|
|
|
|
raise exception '[403] User % not allowed to delete person uuid %', currentUser(), old.uuid;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
2022-09-13 13:27:52 +02:00
|
|
|
Creates an instead of delete trigger for the hs_office_person_rv view.
|
2022-09-07 20:24:35 +02:00
|
|
|
*/
|
2022-09-13 13:27:52 +02:00
|
|
|
create trigger deleteHsOfficePerson_Trigger
|
2022-09-07 20:24:35 +02:00
|
|
|
instead of delete
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_person_rv
|
2022-09-07 20:24:35 +02:00
|
|
|
for each row
|
2022-09-13 13:27:52 +02:00
|
|
|
execute function deleteHsOfficePerson();
|
2022-09-07 20:24:35 +02:00
|
|
|
--/
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-13 13:27:52 +02:00
|
|
|
--changeset hs-office-person-rbac-NEW-PERSON:1 endDelimiter:--//
|
2022-09-07 20:24:35 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Creates a global permission for new-person and assigns it to the hostsharing admins role.
|
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
2022-09-12 16:27:17 +02:00
|
|
|
addCustomerPermissions uuid[];
|
|
|
|
globalObjectUuid uuid;
|
|
|
|
globalAdminRoleUuid uuid ;
|
2022-09-07 20:24:35 +02:00
|
|
|
begin
|
|
|
|
call defineContext('granting global new-person permission to global admin role', null, null, null);
|
|
|
|
|
|
|
|
globalAdminRoleUuid := findRoleId(globalAdmin());
|
|
|
|
globalObjectUuid := (select uuid from global);
|
|
|
|
addCustomerPermissions := createPermissions(globalObjectUuid, array ['new-person']);
|
|
|
|
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 addHsOfficePersonNotAllowedForCurrentSubjects()
|
2022-09-07 20:24:35 +02:00
|
|
|
returns trigger
|
|
|
|
language PLPGSQL
|
|
|
|
as $$
|
|
|
|
begin
|
|
|
|
raise exception '[403] new-person not permitted for %',
|
|
|
|
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_person_insert_trigger
|
2022-09-07 20:24:35 +02:00
|
|
|
before insert
|
2022-09-13 13:27:52 +02:00
|
|
|
on hs_office_person
|
2022-09-07 20:24:35 +02:00
|
|
|
for each row
|
|
|
|
-- TODO.spec: who is allowed to create new persons
|
|
|
|
when ( not hasAssumedRole() )
|
2022-09-13 13:27:52 +02:00
|
|
|
execute procedure addHsOfficePersonNotAllowedForCurrentSubjects();
|
2022-09-07 20:24:35 +02:00
|
|
|
--//
|
|
|
|
|