Michael Hoennig
2022-10-18 cb47d526ac559dff5ffd2d0b591ea36221ca455d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
--liquibase formatted sql
 
-- ============================================================================
--changeset hs-office-coopSharesTransaction-rbac-OBJECT:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call generateRelatedRbacObject('hs_office_coopSharesTransaction');
--//
 
 
-- ============================================================================
--changeset hs-office-coopSharesTransaction-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call generateRbacRoleDescriptors('hsOfficeCoopSharesTransaction', 'hs_office_coopSharesTransaction');
--//
 
 
-- ============================================================================
--changeset hs-office-coopSharesTransaction-rbac-ROLES-CREATION:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
 
/*
    Creates and updates the permissions for coopSharesTransaction entities.
 */
 
create or replace function hsOfficeCoopSharesTransactionRbacRolesTrigger()
    returns trigger
    language plpgsql
    strict as $$
declare
    newHsOfficeMembership      hs_office_membership;
begin
 
    select * from hs_office_membership as p where p.uuid = NEW.membershipUuid into newHsOfficeMembership;
 
    if TG_OP = 'INSERT' then
 
        -- Each coopSharesTransaction entity belong exactly to one membership entity
        -- and it makes little sense just to delegate coopSharesTransaction roles.
        -- Therefore, we do not create coopSharesTransaction roles at all,
        -- but instead just assign extra permissions to existing membership-roles.
 
        -- coopsharestransactions cannot be edited nor deleted, just created+viewed
        call grantPermissionsToRole(
                getRoleId(hsOfficeMembershipTenant(newHsOfficeMembership), 'fail'),
                createPermissions(NEW.uuid, array ['view'])
            );
 
    else
        raise exception 'invalid usage of TRIGGER';
    end if;
 
    return NEW;
end; $$;
 
/*
    An AFTER INSERT TRIGGER which creates the role structure for a new customer.
 */
create trigger createRbacRolesForHsOfficeCoopSharesTransaction_Trigger
    after insert
    on hs_office_coopSharesTransaction
    for each row
execute procedure hsOfficeCoopSharesTransactionRbacRolesTrigger();
--//
 
 
-- ============================================================================
--changeset hs-office-coopSharesTransaction-rbac-IDENTITY-VIEW:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call generateRbacIdentityView('hs_office_coopSharesTransaction', 
    idNameExpression => 'target.reference');
--//
 
 
-- ============================================================================
--changeset hs-office-coopSharesTransaction-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call generateRbacRestrictedView('hs_office_coopSharesTransaction', orderby => 'target.reference');
--//
 
 
-- ============================================================================
--changeset hs-office-coopSharesTransaction-rbac-NEW-CoopSharesTransaction:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
/*
    Creates a global permission for new-coopSharesTransaction and assigns it to the hostsharing admins role.
 */
do language plpgsql $$
    declare
        addCustomerPermissions uuid[];
        globalObjectUuid       uuid;
        globalAdminRoleUuid    uuid ;
    begin
        call defineContext('granting global new-coopSharesTransaction permission to global admin role', null, null, null);
 
        globalAdminRoleUuid := findRoleId(globalAdmin());
        globalObjectUuid := (select uuid from global);
        addCustomerPermissions := createPermissions(globalObjectUuid, array ['new-coopsharestransaction']);
        call grantPermissionsToRole(globalAdminRoleUuid, addCustomerPermissions);
    end;
$$;
 
/**
    Used by the trigger to prevent the add-customer to current user respectively assumed roles.
 */
create or replace function addHsOfficeCoopSharesTransactionNotAllowedForCurrentSubjects()
    returns trigger
    language PLPGSQL
as $$
begin
    raise exception '[403] new-coopsharestransaction 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_coopSharesTransaction_insert_trigger
    before insert
    on hs_office_coopSharesTransaction
    for each row
    when ( not hasAssumedRole() )
execute procedure addHsOfficeCoopSharesTransactionNotAllowedForCurrentSubjects();
--//