2024-04-16 11:21:34 +02:00
|
|
|
--liquibase formatted sql
|
|
|
|
-- This code generated was by RbacViewPostgresGenerator, do not amend manually.
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-booking-item-rbac-OBJECT:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
call generateRelatedRbacObject('hs_booking_item');
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-booking-item-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
call generateRbacRoleDescriptors('hsBookingItem', 'hs_booking_item');
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-booking-item-rbac-insert-trigger:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
|
|
|
|
*/
|
|
|
|
|
|
|
|
create or replace procedure buildRbacSystemForHsBookingItem(
|
|
|
|
NEW hs_booking_item
|
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
|
|
|
declare
|
|
|
|
newDebitor hs_office_debitor;
|
|
|
|
newDebitorRel hs_office_relation;
|
|
|
|
|
|
|
|
begin
|
|
|
|
call enterTriggerForObjectUuid(NEW.uuid);
|
|
|
|
|
|
|
|
SELECT * FROM hs_office_debitor WHERE uuid = NEW.debitorUuid INTO newDebitor;
|
|
|
|
assert newDebitor.uuid is not null, format('newDebitor must not be null for NEW.debitorUuid = %s', NEW.debitorUuid);
|
|
|
|
|
|
|
|
SELECT debitorRel.*
|
|
|
|
FROM hs_office_relation debitorRel
|
|
|
|
JOIN hs_office_debitor debitor ON debitor.debitorRelUuid = debitorRel.uuid
|
|
|
|
WHERE debitor.uuid = NEW.debitorUuid
|
|
|
|
INTO newDebitorRel;
|
|
|
|
assert newDebitorRel.uuid is not null, format('newDebitorRel must not be null for NEW.debitorUuid = %s', NEW.debitorUuid);
|
|
|
|
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
|
|
|
hsBookingItemOWNER(NEW),
|
|
|
|
incomingSuperRoles => array[hsOfficeRelationAGENT(newDebitorRel)]
|
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
|
|
|
hsBookingItemADMIN(NEW),
|
2024-04-19 10:06:57 +02:00
|
|
|
permissions => array['UPDATE'],
|
|
|
|
incomingSuperRoles => array[
|
|
|
|
hsBookingItemOWNER(NEW),
|
|
|
|
hsOfficeRelationAGENT(newDebitorRel)]
|
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
|
|
|
hsBookingItemAGENT(NEW),
|
|
|
|
incomingSuperRoles => array[hsBookingItemADMIN(NEW)]
|
2024-04-16 11:21:34 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
perform createRoleWithGrants(
|
|
|
|
hsBookingItemTENANT(NEW),
|
|
|
|
permissions => array['SELECT'],
|
2024-04-19 10:06:57 +02:00
|
|
|
incomingSuperRoles => array[hsBookingItemAGENT(NEW)],
|
2024-04-16 11:21:34 +02:00
|
|
|
outgoingSubRoles => array[hsOfficeRelationTENANT(newDebitorRel)]
|
|
|
|
);
|
|
|
|
|
|
|
|
call grantPermissionToRole(createPermission(NEW.uuid, 'DELETE'), globalAdmin());
|
|
|
|
|
|
|
|
call leaveTriggerForObjectUuid(NEW.uuid);
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
AFTER INSERT TRIGGER to create the role+grant structure for a new hs_booking_item row.
|
|
|
|
*/
|
|
|
|
|
|
|
|
create or replace function insertTriggerForHsBookingItem_tf()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
|
|
|
call buildRbacSystemForHsBookingItem(NEW);
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
create trigger insertTriggerForHsBookingItem_tg
|
|
|
|
after insert on hs_booking_item
|
|
|
|
for each row
|
|
|
|
execute procedure insertTriggerForHsBookingItem_tf();
|
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2024-04-29 11:43:49 +02:00
|
|
|
--changeset hs-booking-item-rbac-GRANTING-INSERT-PERMISSION:1 endDelimiter:--//
|
2024-04-16 11:21:34 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
-- granting INSERT permission to hs_office_relation ----------------------------
|
|
|
|
|
2024-04-16 11:21:34 +02:00
|
|
|
/*
|
2024-04-29 11:43:49 +02:00
|
|
|
Grants INSERT INTO hs_booking_item permissions to specified role of pre-existing hs_office_relation rows.
|
2024-04-16 11:21:34 +02:00
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
|
|
|
row hs_office_relation;
|
|
|
|
begin
|
2024-04-29 11:43:49 +02:00
|
|
|
call defineContext('create INSERT INTO hs_booking_item permissions for pre-exising hs_office_relation rows');
|
2024-04-16 11:21:34 +02:00
|
|
|
|
|
|
|
FOR row IN SELECT * FROM hs_office_relation
|
2024-04-29 11:43:49 +02:00
|
|
|
WHERE type = 'DEBITOR'
|
2024-04-16 11:21:34 +02:00
|
|
|
LOOP
|
|
|
|
call grantPermissionToRole(
|
2024-04-29 11:43:49 +02:00
|
|
|
createPermission(row.uuid, 'INSERT', 'hs_booking_item'),
|
|
|
|
hsOfficeRelationADMIN(row));
|
2024-04-16 11:21:34 +02:00
|
|
|
END LOOP;
|
2024-04-29 11:43:49 +02:00
|
|
|
end;
|
2024-04-16 11:21:34 +02:00
|
|
|
$$;
|
|
|
|
|
|
|
|
/**
|
2024-04-29 11:43:49 +02:00
|
|
|
Grants hs_booking_item INSERT permission to specified role of new hs_office_relation rows.
|
2024-04-16 11:21:34 +02:00
|
|
|
*/
|
2024-04-29 11:43:49 +02:00
|
|
|
create or replace function new_hs_booking_item_grants_insert_to_hs_office_relation_tf()
|
2024-04-16 11:21:34 +02:00
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
2024-04-23 10:42:24 +02:00
|
|
|
if NEW.type = 'DEBITOR' then
|
2024-04-29 11:43:49 +02:00
|
|
|
call grantPermissionToRole(
|
2024-04-16 11:21:34 +02:00
|
|
|
createPermission(NEW.uuid, 'INSERT', 'hs_booking_item'),
|
|
|
|
hsOfficeRelationADMIN(NEW));
|
2024-04-29 11:43:49 +02:00
|
|
|
end if;
|
2024-04-16 11:21:34 +02:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
-- z_... is to put it at the end of after insert triggers, to make sure the roles exist
|
2024-04-29 11:43:49 +02:00
|
|
|
create trigger z_new_hs_booking_item_grants_insert_to_hs_office_relation_tg
|
2024-04-16 11:21:34 +02:00
|
|
|
after insert on hs_office_relation
|
|
|
|
for each row
|
2024-04-29 11:43:49 +02:00
|
|
|
execute procedure new_hs_booking_item_grants_insert_to_hs_office_relation_tf();
|
2024-04-16 11:21:34 +02:00
|
|
|
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs_booking_item-rbac-CHECKING-INSERT-PERMISSION:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
Checks if the user respectively the assumed roles are allowed to insert a row to hs_booking_item.
|
2024-04-16 11:21:34 +02:00
|
|
|
*/
|
|
|
|
create or replace function hs_booking_item_insert_permission_check_tf()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
declare
|
2024-04-29 11:43:49 +02:00
|
|
|
superObjectUuid uuid;
|
2024-04-16 11:21:34 +02:00
|
|
|
begin
|
2024-04-29 11:43:49 +02:00
|
|
|
-- check INSERT permission via indirect foreign key: NEW.debitorUuid
|
|
|
|
superObjectUuid := (SELECT debitorRel.uuid
|
|
|
|
FROM hs_office_relation debitorRel
|
|
|
|
JOIN hs_office_debitor debitor ON debitor.debitorRelUuid = debitorRel.uuid
|
|
|
|
WHERE debitor.uuid = NEW.debitorUuid
|
|
|
|
);
|
|
|
|
assert superObjectUuid is not null, 'object uuid fetched depending on hs_booking_item.debitorUuid must not be null, also check fetchSql in RBAC DSL';
|
|
|
|
if hasInsertPermission(superObjectUuid, 'hs_booking_item') then
|
|
|
|
return NEW;
|
2024-04-16 11:21:34 +02:00
|
|
|
end if;
|
2024-04-29 11:43:49 +02:00
|
|
|
|
|
|
|
raise exception '[403] insert into hs_booking_item not allowed for current subjects % (%)',
|
|
|
|
currentSubjects(), currentSubjectsUuids();
|
2024-04-16 11:21:34 +02:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
create trigger hs_booking_item_insert_permission_check_tg
|
|
|
|
before insert on hs_booking_item
|
|
|
|
for each row
|
|
|
|
execute procedure hs_booking_item_insert_permission_check_tf();
|
|
|
|
--//
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2024-04-16 11:21:34 +02:00
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-booking-item-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
call generateRbacIdentityViewFromQuery('hs_booking_item',
|
|
|
|
$idName$
|
|
|
|
SELECT bookingItem.uuid as uuid, debitorIV.idName || '-' || cleanIdentifier(bookingItem.caption) as idName
|
2024-04-19 10:06:57 +02:00
|
|
|
FROM hs_booking_item bookingItem
|
|
|
|
JOIN hs_office_debitor_iv debitorIV ON debitorIV.uuid = bookingItem.debitorUuid
|
2024-04-29 11:43:49 +02:00
|
|
|
$idName$);
|
2024-04-16 11:21:34 +02:00
|
|
|
--//
|
|
|
|
|
2024-04-29 11:43:49 +02:00
|
|
|
|
2024-04-16 11:21:34 +02:00
|
|
|
-- ============================================================================
|
|
|
|
--changeset hs-booking-item-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
call generateRbacRestrictedView('hs_booking_item',
|
|
|
|
$orderBy$
|
|
|
|
validity
|
|
|
|
$orderBy$,
|
|
|
|
$updates$
|
|
|
|
version = new.version,
|
2024-04-19 10:06:57 +02:00
|
|
|
caption = new.caption,
|
2024-04-16 11:21:34 +02:00
|
|
|
validity = new.validity,
|
|
|
|
resources = new.resources
|
|
|
|
$updates$);
|
|
|
|
--//
|
|
|
|
|