Michael Hoennig
2022-09-16 d63e3f31e926a53cbfdfb8accda77472ea66e2cc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
--liquibase formatted sql
 
-- ============================================================================
--changeset hs-office-contact-rbac-OBJECT:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call generateRelatedRbacObject('hs_office_contact');
--//
 
 
-- ============================================================================
--changeset hs-office-contact-rbac-ROLE-DESCRIPTORS:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
call generateRbacRoleDescriptors('hsOfficeContact', 'hs_office_contact');
--//
 
 
-- ============================================================================
--changeset hs-office-contact-rbac-ROLES-CREATION:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
 
/*
    Creates the roles and their assignments for a new contact for the AFTER INSERT TRIGGER.
 */
 
create or replace function createRbacRolesForHsOfficeContact()
    returns trigger
    language plpgsql
    strict as $$
declare
    ownerRole uuid;
    adminRole uuid;
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
    ownerRole := createRole(
        hsOfficeContactOwner(NEW),
        grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['*']),
        beneathRole(globalAdmin()),
        withoutSubRoles(),
        withUser(currentUser()), -- TODO.spec: Who is owner of a new contact?
        grantedByRole(globalAdmin())
        );
 
    -- the tenant role for those related users who can view the data
    adminRole := createRole(
            hsOfficeContactAdmin(NEW),
            grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['edit']),
            beneathRole(ownerRole)
        );
 
    -- the tenant role for those related users who can view the data
    perform createRole(
        hsOfficeContactTenant(NEW),
        grantingPermissions(forObjectUuid => NEW.uuid, permitOps => array ['view']),
        beneathRole(adminRole)
        );
 
    return NEW;
end; $$;
 
/*
    An AFTER INSERT TRIGGER which creates the role structure for a new customer.
 */
 
create trigger createRbacRolesForHsOfficeContact_Trigger
    after insert
    on hs_office_contact
    for each row
execute procedure createRbacRolesForHsOfficeContact();
--//
 
 
-- ============================================================================
--changeset hs-office-contact-rbac-IDENTITY-VIEW:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
 
/*
    Creates a view to the contact main table which maps the identifying name
    (in this case, the prefix) to the objectUuid.
 */
create or replace view hs_office_contact_iv as
select target.uuid, cleanIdentifier(target.label) as idName
    from hs_office_contact as target;
-- TODO.spec: Is it ok that everybody has access to this information?
grant all privileges on hs_office_contact_iv to restricted;
 
/*
    Returns the objectUuid for a given identifying name (in this case the prefix).
 */
create or replace function hs_office_contactUuidByIdName(idName varchar)
    returns uuid
    language sql
    strict as $$
select uuid from hs_office_contact_iv iv where iv.idName = hs_office_contactUuidByIdName.idName;
$$;
 
/*
    Returns the identifying name for a given objectUuid (in this case the label).
 */
create or replace function hs_office_contactIdNameByUuid(uuid uuid)
    returns varchar
    language sql
    strict as $$
select idName from hs_office_contact_iv iv where iv.uuid = hs_office_contactIdNameByUuid.uuid;
$$;
--//
 
 
-- ============================================================================
--changeset hs-office-contact-rbac-RESTRICTED-VIEW:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
/*
    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;
drop view if exists hs_office_contact_rv;
create or replace view hs_office_contact_rv as
select target.*
    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;
--//
 
 
-- ============================================================================
--changeset hs-office-contact-rbac-INSTEAD-OF-INSERT-TRIGGER:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
 
/**
    Instead of insert trigger function for hs_office_contact_rv.
 */
create or replace function insertHsOfficeContact()
    returns trigger
    language plpgsql as $$
declare
    newUser hs_office_contact;
begin
    insert
        into hs_office_contact
        values (new.*)
        returning * into newUser;
    return newUser;
end;
$$;
 
/*
    Creates an instead of insert trigger for the hs_office_contact_rv view.
 */
create trigger insertHsOfficeContact_Trigger
    instead of insert
    on hs_office_contact_rv
    for each row
execute function insertHsOfficeContact();
--//
 
-- ============================================================================
--changeset hs-office-contact-rbac-INSTEAD-OF-DELETE-TRIGGER:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
 
/**
    Instead of delete trigger function for hs_office_contact_rv.
 
    Checks if the current subject (user / assumed role) has the permission to delete the row.
 */
create or replace function deleteHsOfficeContact()
    returns trigger
    language plpgsql as $$
begin
    if hasGlobalRoleGranted(currentUserUuid()) or
       old.uuid in (select queryAccessibleObjectUuidsOfSubjectIds('delete', 'hs_office_contact', currentSubjectsUuids())) then
        delete from hs_office_contact c where c.uuid = old.uuid;
        return old;
    end if;
    raise exception '[403] User % not allowed to delete contact uuid %', currentUser(), old.uuid;
end; $$;
 
/*
    Creates an instead of delete trigger for the hs_office_contact_rv view.
 */
create trigger deleteHsOfficeContact_Trigger
    instead of delete
    on hs_office_contact_rv
    for each row
execute function deleteHsOfficeContact();
--/
 
-- ============================================================================
--changeset hs-office-contact-rbac-NEW-CONTACT:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
/*
    Creates a global permission for new-contact and assigns it to the hostsharing admins role.
 */
do language plpgsql $$
    declare
        addCustomerPermissions  uuid[];
        globalObjectUuid        uuid;
        globalAdminRoleUuid         uuid ;
    begin
        call defineContext('granting global new-contact permission to global admin role', null, null, null);
 
        globalAdminRoleUuid := findRoleId(globalAdmin());
        globalObjectUuid := (select uuid from global);
        addCustomerPermissions := createPermissions(globalObjectUuid, array ['new-contact']);
        call grantPermissionsToRole(globalAdminRoleUuid, addCustomerPermissions);
    end;
$$;
 
/**
    Used by the trigger to prevent the add-customer to current user respectively assumed roles.
 */
create or replace function addHsOfficeContactNotAllowedForCurrentSubjects()
    returns trigger
    language PLPGSQL
as $$
begin
    raise exception '[403] new-contact 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_contact_insert_trigger
    before insert
    on hs_office_contact
    for each row
    -- TODO.spec: who is allowed to create new contacts
    when ( not hasAssumedRole() )
execute procedure addHsOfficeContactNotAllowedForCurrentSubjects();
--//