2022-07-29 12:37:40 +02:00
|
|
|
--liquibase formatted sql
|
2024-03-09 09:12:29 +01:00
|
|
|
-- This code generated was by RbacViewPostgresGenerator at 2024-03-09T08:56:16.450322125.
|
2022-07-29 12:37:40 +02:00
|
|
|
|
|
|
|
-- ============================================================================
|
2022-09-16 15:25:58 +02:00
|
|
|
--changeset test-package-rbac-OBJECT:1 endDelimiter:--//
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-09-16 15:25:58 +02:00
|
|
|
call generateRelatedRbacObject('test_package');
|
2022-07-29 12:37:40 +02:00
|
|
|
--//
|
2022-07-22 13:31:37 +02:00
|
|
|
|
|
|
|
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ============================================================================
|
2022-08-31 09:42:40 +02:00
|
|
|
--changeset test-package-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2022-09-16 15:25:58 +02:00
|
|
|
call generateRbacRoleDescriptors('testPackage', 'test_package');
|
2022-07-29 12:37:40 +02:00
|
|
|
--//
|
2022-07-25 16:38:21 +02:00
|
|
|
|
|
|
|
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ============================================================================
|
2024-03-06 08:44:23 +01:00
|
|
|
--changeset test-package-rbac-insert-trigger:1 endDelimiter:--//
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-06 08:44:23 +01:00
|
|
|
|
2022-07-29 12:37:40 +02:00
|
|
|
/*
|
2024-03-08 08:53:28 +01:00
|
|
|
Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
|
2022-07-29 12:37:40 +02:00
|
|
|
*/
|
2024-03-06 08:44:23 +01:00
|
|
|
|
|
|
|
create or replace procedure buildRbacSystemForTestPackage(
|
|
|
|
NEW test_package
|
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
2022-07-29 08:46:04 +02:00
|
|
|
declare
|
2024-03-06 08:44:23 +01:00
|
|
|
newCustomer test_customer;
|
2022-07-22 13:31:37 +02:00
|
|
|
|
2024-03-06 08:44:23 +01:00
|
|
|
begin
|
2024-02-24 09:04:07 +01:00
|
|
|
call enterTriggerForObjectUuid(NEW.uuid);
|
2024-03-06 08:44:23 +01:00
|
|
|
SELECT * FROM test_customer c
|
|
|
|
WHERE c.uuid= NEW.customerUuid
|
|
|
|
into newCustomer;
|
2024-02-24 09:04:07 +01:00
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-06 08:44:23 +01:00
|
|
|
testPackageOwner(NEW),
|
|
|
|
permissions => array['DELETE', 'UPDATE'],
|
|
|
|
incomingSuperRoles => array[testCustomerAdmin(newCustomer)]
|
|
|
|
);
|
2022-07-25 16:38:21 +02:00
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-06 08:44:23 +01:00
|
|
|
testPackageAdmin(NEW),
|
2022-10-12 15:48:56 +02:00
|
|
|
incomingSuperRoles => array[testPackageOwner(NEW)]
|
2024-03-06 08:44:23 +01:00
|
|
|
);
|
2022-07-25 16:38:21 +02:00
|
|
|
|
2022-10-12 15:48:56 +02:00
|
|
|
perform createRoleWithGrants(
|
2024-03-06 08:44:23 +01:00
|
|
|
testPackageTenant(NEW),
|
|
|
|
permissions => array['SELECT'],
|
|
|
|
incomingSuperRoles => array[testPackageAdmin(NEW)],
|
|
|
|
outgoingSubRoles => array[testCustomerTenant(newCustomer)]
|
|
|
|
);
|
2022-07-22 13:31:37 +02:00
|
|
|
|
2024-02-24 09:04:07 +01:00
|
|
|
call leaveTriggerForObjectUuid(NEW.uuid);
|
2024-03-06 08:44:23 +01:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
AFTER INSERT TRIGGER to create the role+grant structure for a new test_package row.
|
|
|
|
*/
|
|
|
|
|
|
|
|
create or replace function insertTriggerForTestPackage_tf()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
|
|
|
call buildRbacSystemForTestPackage(NEW);
|
2022-07-29 08:46:04 +02:00
|
|
|
return NEW;
|
|
|
|
end; $$;
|
2022-07-22 13:31:37 +02:00
|
|
|
|
2024-03-06 08:44:23 +01:00
|
|
|
create trigger insertTriggerForTestPackage_tg
|
|
|
|
after insert on test_package
|
|
|
|
for each row
|
|
|
|
execute procedure insertTriggerForTestPackage_tf();
|
|
|
|
|
|
|
|
--//
|
|
|
|
|
|
|
|
-- ============================================================================
|
|
|
|
--changeset test-package-rbac-update-trigger:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
2022-07-29 12:37:40 +02:00
|
|
|
/*
|
2024-03-06 08:44:23 +01:00
|
|
|
Called from the AFTER UPDATE TRIGGER to re-wire the grants.
|
2022-07-29 12:37:40 +02:00
|
|
|
*/
|
|
|
|
|
2024-03-06 08:44:23 +01:00
|
|
|
create or replace procedure updateRbacRulesForTestPackage(
|
|
|
|
OLD test_package,
|
|
|
|
NEW test_package
|
|
|
|
)
|
|
|
|
language plpgsql as $$
|
|
|
|
|
|
|
|
declare
|
|
|
|
oldCustomer test_customer;
|
|
|
|
newCustomer test_customer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
call enterTriggerForObjectUuid(NEW.uuid);
|
|
|
|
|
|
|
|
SELECT * FROM test_customer c
|
|
|
|
WHERE c.uuid= OLD.customerUuid
|
|
|
|
into oldCustomer;
|
|
|
|
SELECT * FROM test_customer c
|
|
|
|
WHERE c.uuid= NEW.customerUuid
|
|
|
|
into newCustomer;
|
|
|
|
|
|
|
|
if NEW.customerUuid <> OLD.customerUuid then
|
|
|
|
|
|
|
|
call revokePermissionFromRole(findPermissionId(OLD.uuid, 'INSERT'), testCustomerAdmin(oldCustomer));
|
|
|
|
|
|
|
|
call revokeRoleFromRole(testPackageOwner(OLD), testCustomerAdmin(oldCustomer));
|
|
|
|
call grantRoleToRole(testPackageOwner(NEW), testCustomerAdmin(newCustomer));
|
|
|
|
|
|
|
|
call revokeRoleFromRole(testCustomerTenant(oldCustomer), testPackageTenant(OLD));
|
|
|
|
call grantRoleToRole(testCustomerTenant(newCustomer), testPackageTenant(NEW));
|
|
|
|
|
|
|
|
end if;
|
|
|
|
|
|
|
|
call leaveTriggerForObjectUuid(NEW.uuid);
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
/*
|
|
|
|
AFTER INSERT TRIGGER to re-wire the grant structure for a new test_package row.
|
|
|
|
*/
|
|
|
|
|
|
|
|
create or replace function updateTriggerForTestPackage_tf()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
|
|
|
call updateRbacRulesForTestPackage(OLD, NEW);
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
create trigger updateTriggerForTestPackage_tg
|
|
|
|
after update on test_package
|
2022-07-29 08:46:04 +02:00
|
|
|
for each row
|
2024-03-06 08:44:23 +01:00
|
|
|
execute procedure updateTriggerForTestPackage_tf();
|
|
|
|
|
2022-07-29 12:37:40 +02:00
|
|
|
--//
|
|
|
|
|
2024-03-06 08:44:23 +01:00
|
|
|
-- ============================================================================
|
|
|
|
--changeset test-package-rbac-INSERT:1 endDelimiter:--//
|
|
|
|
-- ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Creates INSERT INTO test_package permissions for the related test_customer rows.
|
|
|
|
*/
|
|
|
|
do language plpgsql $$
|
|
|
|
declare
|
|
|
|
row test_customer;
|
|
|
|
permissionUuid uuid;
|
|
|
|
roleUuid uuid;
|
|
|
|
begin
|
2024-03-07 14:42:25 +01:00
|
|
|
call defineContext('create INSERT INTO test_package permissions for the related test_customer rows');
|
2024-03-06 08:44:23 +01:00
|
|
|
|
|
|
|
FOR row IN SELECT * FROM test_customer
|
|
|
|
LOOP
|
|
|
|
roleUuid := findRoleId(testCustomerAdmin(row));
|
|
|
|
permissionUuid := createPermission(row.uuid, 'INSERT', 'test_package');
|
|
|
|
call grantPermissionToRole(roleUuid, permissionUuid);
|
|
|
|
END LOOP;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Adds test_package INSERT permission to specified role of new test_customer rows.
|
|
|
|
*/
|
|
|
|
create or replace function test_package_test_customer_insert_tf()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql
|
|
|
|
strict as $$
|
|
|
|
begin
|
|
|
|
call grantPermissionToRole(
|
|
|
|
testCustomerAdmin(NEW),
|
|
|
|
createPermission(NEW.uuid, 'INSERT', 'test_package'));
|
|
|
|
return NEW;
|
|
|
|
end; $$;
|
|
|
|
|
|
|
|
create trigger test_package_test_customer_insert_tg
|
|
|
|
after insert on test_customer
|
|
|
|
for each row
|
|
|
|
execute procedure test_package_test_customer_insert_tf();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Checks if the user or assumed roles are allowed to insert a row to test_package.
|
|
|
|
*/
|
|
|
|
create or replace function test_package_insert_permission_missing_tf()
|
|
|
|
returns trigger
|
|
|
|
language plpgsql as $$
|
|
|
|
begin
|
2024-03-07 18:12:33 +01:00
|
|
|
raise exception '[403] insert into test_package not allowed for current subjects % (%)',
|
2024-03-07 12:26:07 +01:00
|
|
|
currentSubjects(), currentSubjectsUuids();
|
2024-03-06 08:44:23 +01:00
|
|
|
end; $$;
|
|
|
|
|
|
|
|
create trigger test_package_insert_permission_check_tg
|
|
|
|
before insert on test_package
|
|
|
|
for each row
|
|
|
|
when ( not hasInsertPermission(NEW.customerUuid, 'INSERT', 'test_package') )
|
|
|
|
execute procedure test_package_insert_permission_missing_tf();
|
|
|
|
|
|
|
|
--//
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ============================================================================
|
2022-08-31 09:42:40 +02:00
|
|
|
--changeset test-package-rbac-IDENTITY-VIEW:1 endDelimiter:--//
|
2022-07-29 16:25:46 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-06 08:44:23 +01:00
|
|
|
call generateRbacIdentityView('test_package', $idName$
|
|
|
|
name
|
|
|
|
$idName$);
|
2022-07-29 16:25:46 +02:00
|
|
|
--//
|
|
|
|
|
|
|
|
|
|
|
|
-- ============================================================================
|
2022-08-31 09:42:40 +02:00
|
|
|
--changeset test-package-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
|
2022-07-29 12:37:40 +02:00
|
|
|
-- ----------------------------------------------------------------------------
|
2024-03-06 08:44:23 +01:00
|
|
|
call generateRbacRestrictedView('test_package',
|
|
|
|
'name',
|
2022-09-19 20:43:14 +02:00
|
|
|
$updates$
|
|
|
|
version = new.version,
|
|
|
|
customerUuid = new.customerUuid,
|
|
|
|
description = new.description
|
|
|
|
$updates$);
|
2022-07-29 12:37:40 +02:00
|
|
|
--//
|
2022-09-19 20:43:14 +02:00
|
|
|
|
|
|
|
|