]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/006.schema.permissions.sql
adding support for group application permissions
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 006.schema.permissions.sql
1 DROP SCHEMA permission CASCADE;
2
3 BEGIN;
4 CREATE SCHEMA permission;
5
6 CREATE TABLE permission.perm_list (
7         id              SERIAL  PRIMARY KEY,
8         code            TEXT    NOT NULL UNIQUE,
9         description     TEXT
10 );
11 CREATE INDEX perm_list_code_idx ON permission.perm_list (code);
12
13 /*
14 INSERT INTO permission.perm_list VALUES (-1, 'EVERYTHING', NULL);
15 INSERT INTO permission.perm_list VALUES (2, 'OPAC_LOGIN', NULL);
16 INSERT INTO permission.perm_list VALUES (4, 'STAFF_LOGIN', NULL);
17 INSERT INTO permission.perm_list VALUES (5, 'MR_HOLDS', NULL);
18 INSERT INTO permission.perm_list VALUES (6, 'TITLE_HOLDS', NULL);
19 INSERT INTO permission.perm_list VALUES (7, 'VOLUME_HOLDS', NULL);
20 INSERT INTO permission.perm_list VALUES (9, 'REQUEST_HOLDS', NULL);
21 INSERT INTO permission.perm_list VALUES (10, 'REQUEST_HOLDS_OVERRIDE', NULL);
22 INSERT INTO permission.perm_list VALUES (13, 'DELETE_HOLDS', NULL);
23 INSERT INTO permission.perm_list VALUES (15, 'RENEW_CIRC', NULL);
24 INSERT INTO permission.perm_list VALUES (16, 'VIEW_USER_FINES_SUMMARY', NULL);
25 INSERT INTO permission.perm_list VALUES (17, 'VIEW_USER_TRANSACTIONS', NULL);
26 INSERT INTO permission.perm_list VALUES (18, 'UPDATE_MARC', NULL);
27 INSERT INTO permission.perm_list VALUES (20, 'IMPORT_MARC', NULL);
28 INSERT INTO permission.perm_list VALUES (21, 'CREATE_VOLUME', NULL);
29 INSERT INTO permission.perm_list VALUES (22, 'UPDATE_VOLUME', NULL);
30 INSERT INTO permission.perm_list VALUES (23, 'DELETE_VOLUME', NULL);
31 INSERT INTO permission.perm_list VALUES (25, 'UPDATE_COPY', NULL);
32 INSERT INTO permission.perm_list VALUES (26, 'DELETE_COPY', NULL);
33 INSERT INTO permission.perm_list VALUES (27, 'RENEW_HOLD_OVERRIDE', NULL);
34 INSERT INTO permission.perm_list VALUES (28, 'CREATE_USER', NULL);
35 INSERT INTO permission.perm_list VALUES (29, 'UPDATE_USER', NULL);
36 INSERT INTO permission.perm_list VALUES (30, 'DELETE_USER', NULL);
37 INSERT INTO permission.perm_list VALUES (31, 'VIEW_USER', NULL);
38 INSERT INTO permission.perm_list VALUES (32, 'COPY_CHECKIN', NULL);
39 INSERT INTO permission.perm_list VALUES (33, 'CREATE_TRANSIT', NULL);
40 INSERT INTO permission.perm_list VALUES (34, 'VIEW_PERMISSION', NULL);
41 INSERT INTO permission.perm_list VALUES (35, 'CHECKIN_BYPASS_HOLD_FULFILL', NULL);
42 INSERT INTO permission.perm_list VALUES (36, 'CREATE_PAYMENT', NULL);
43 INSERT INTO permission.perm_list VALUES (37, 'SET_CIRC_LOST', NULL);
44 INSERT INTO permission.perm_list VALUES (38, 'SET_CIRC_MISSING', NULL);
45 INSERT INTO permission.perm_list VALUES (39, 'SET_CIRC_CLAIMS_RETURNED', NULL);
46 INSERT INTO permission.perm_list VALUES (41, 'CREATE_TRANSACTION', 'User may create new billable transactions');
47 INSERT INTO permission.perm_list VALUES (43, 'CREATE_BILL', 'Allows a user to create a new bill on a transaction');
48 INSERT INTO permission.perm_list VALUES (44, 'VIEW_CONTAINER', 'Allows a user to view another user''s containers (buckets)');
49 INSERT INTO permission.perm_list VALUES (45, 'CREATE_CONTAINER', 'Allows a user to create a new container for another user');
50 INSERT INTO permission.perm_list VALUES (8, 'COPY_HOLDS', 'User is allowed to place a hold on a specific copy');
51 INSERT INTO permission.perm_list VALUES (24, 'CREATE_COPY', 'User is allowed to create a new copy object');
52 INSERT INTO permission.perm_list VALUES (19, 'CREATE_ORIGINAL_MARC', 'User is allowed to create new MARC records');
53 INSERT INTO permission.perm_list VALUES (47, 'UPDATE_ORG_UNIT', 'Allows a user to change org unit settings');
54 INSERT INTO permission.perm_list VALUES (48, 'VIEW_CIRCULATIONS', 'Allows a user to see what another use has checked out');
55 INSERT INTO permission.perm_list VALUES (14, 'UPDATE_HOLD', 'Allows a user to update another user''s hold');
56 INSERT INTO permission.perm_list VALUES (11, 'VIEW_HOLD', 'Allows a user to view another user''s holds');
57 INSERT INTO permission.perm_list VALUES (42, 'VIEW_TRANSACTION', 'User may view another user''s transactions');
58 INSERT INTO permission.perm_list VALUES (49, 'DELETE_CONTAINER', 'Allows a user to delete another user container');
59 INSERT INTO permission.perm_list VALUES (50, 'CREATE_CONTAINER_ITEM', 'Create a container item for another user');
60 INSERT INTO permission.perm_list VALUES (51, 'CREATE_USER_GROUP_LINK', 'User can add other users to permission groups');
61 INSERT INTO permission.perm_list VALUES (52, 'REMOVE_USER_GROUP_LINK', 'User can remove other users from permission groups');
62 INSERT INTO permission.perm_list VALUES (53, 'VIEW_PERM_GROUPS', 'Allow user to view others'' permission groups');
63 INSERT INTO permission.perm_list VALUES (54, 'VIEW_PERMIT_CHECKOUT', 'Allows a user to determine of another user can checkout an item');
64 INSERT INTO permission.perm_list VALUES (55, 'UPDATE_BATCH_COPY', 'Allows a user to edit copies in batch');
65 INSERT INTO permission.perm_list VALUES (56, 'CREATE_PATRON_STAT_CAT', 'User may create a new patron statistical category');
66 INSERT INTO permission.perm_list VALUES (57, 'CREATE_COPY_STAT_CAT', 'User may create a copy stat cat');
67 INSERT INTO permission.perm_list VALUES (58, 'CREATE_PATRON_STAT_CAT_ENTRY', 'User may create a new patron stat cat entry');
68 INSERT INTO permission.perm_list VALUES (59, 'CREATE_COPY_STAT_CAT_ENTRY', 'User may create a new copy stat cat entry');
69 INSERT INTO permission.perm_list VALUES (60, 'UPDATE_PATRON_STAT_CAT', 'User may update a patron stat cat');
70 INSERT INTO permission.perm_list VALUES (61, 'UPDATE_COPY_STAT_CAT', 'User may update a copy stat cat');
71 INSERT INTO permission.perm_list VALUES (62, 'UPDATE_PATRON_STAT_CAT_ENTRY', 'User may update a patron stat cat entry');
72 INSERT INTO permission.perm_list VALUES (63, 'UPDATE_COPY_STAT_CAT_ENTRY', 'User may update a copy stat cat entry');
73 INSERT INTO permission.perm_list VALUES (65, 'CREATE_COPY_STAT_CAT_ENTRY_MAP', 'User may link a copy to a stat cat entry');
74 INSERT INTO permission.perm_list VALUES (64, 'CREATE_PATRON_STAT_CAT_ENTRY_MAP', 'User may link another user to a stat cat entry');
75 INSERT INTO permission.perm_list VALUES (66, 'DELETE_PATRON_STAT_CAT', 'User may delete a patron stat cat');
76 INSERT INTO permission.perm_list VALUES (67, 'DELETE_COPY_STAT_CAT', 'User may delete a copy stat cat');
77 INSERT INTO permission.perm_list VALUES (68, 'DELETE_PATRON_STAT_CAT_ENTRY', 'User may delete a patron stat cat entry');
78 INSERT INTO permission.perm_list VALUES (69, 'DELETE_COPY_STAT_CAT_ENTRY', 'User may delete a copy stat cat entry');
79 INSERT INTO permission.perm_list VALUES (70, 'DELETE_PATRON_STAT_CAT_ENTRY_MAP', 'User may delete a patron stat cat entry map');
80 INSERT INTO permission.perm_list VALUES (71, 'DELETE_COPY_STAT_CAT_ENTRY_MAP', 'User may delete a copy stat cat entry map');
81 INSERT INTO permission.perm_list VALUES (72, 'CREATE_NON_CAT_TYPE', 'Allows a user to create a new non-cataloged item type');
82 INSERT INTO permission.perm_list VALUES (73, 'UPDATE_NON_CAT_TYPE', 'Allows a user to update a non cataloged type');
83 INSERT INTO permission.perm_list VALUES (74, 'CREATE_IN_HOUSE_USE', 'Allows a user to create a new in-house-use ');
84 INSERT INTO permission.perm_list VALUES (75, 'COPY_CHECKOUT', 'Allows a user to check out a copy');
85 INSERT INTO permission.perm_list VALUES (76, 'CREATE_COPY_LOCATION', 'Allows a user to create a new copy location');
86 INSERT INTO permission.perm_list VALUES (77, 'UPDATE_COPY_LOCATION', 'Allows a user to update a copy location');
87 INSERT INTO permission.perm_list VALUES (78, 'DELETE_COPY_LOCATION', 'Allows a user to delete a copy location');
88 INSERT INTO permission.perm_list VALUES (79, 'CREATE_COPY_TRANSIT', 'Allows a user to create a transit_copy object for transiting a copy');
89 INSERT INTO permission.perm_list VALUES (80, 'COPY_TRANSIT_RECEIVE', 'Allows a user to close out a transit on a copy');
90 INSERT INTO permission.perm_list VALUES (81, 'VIEW_HOLD_PERMIT', 'Allows a user to see if another user has permission to place a hold on a given copy');
91 INSERT INTO permission.perm_list VALUES (82, 'VIEW_COPY_CHECKOUT_HISTORY', 'Allows a user to view which users have checked out a given copy');
92 INSERT INTO permission.perm_list VALUES (83, 'REMOTE_Z3950_QUERY', 'Allows a user to perform z3950 queries against remote servers');
93 INSERT INTO permission.perm_list VALUES (84, 'REGISTER_WORKSTATION', 'Allows a user to register a new workstation');
94 INSERT INTO permission.perm_list VALUES (85, 'VIEW_COPY_NOTES', 'Allows a user to view all notes attached to a copy');
95 INSERT INTO permission.perm_list VALUES (86, 'VIEW_VOLUME_NOTES', 'Allows a user to view all notes attached to a volume');
96 INSERT INTO permission.perm_list VALUES (87, 'VIEW_TITLE_NOTES', 'Allows a user to view all notes attached to a title');
97 INSERT INTO permission.perm_list VALUES (89, 'CREATE_VOLUME_NOTE', 'Allows a user to create a new volume note');
98 INSERT INTO permission.perm_list VALUES (88, 'CREATE_COPY_NOTE', 'Allows a user to create a new copy note');
99 INSERT INTO permission.perm_list VALUES (90, 'CREATE_TITLE_NOTE', 'Allows a user to create a new title note');
100 INSERT INTO permission.perm_list VALUES (91, 'DELETE_COPY_NOTE', 'Allows a user to delete someone elses copy notes');
101 INSERT INTO permission.perm_list VALUES (92, 'DELETE_VOLUME_NOTE', 'Allows a user to delete someone elses volume note');
102 INSERT INTO permission.perm_list VALUES (93, 'DELETE_TITLE_NOTE', 'Allows a user to delete someone elses title note');
103 INSERT INTO permission.perm_list VALUES (94, 'UPDATE_CONTAINER', 'Allows a user to update another users container');
104 INSERT INTO permission.perm_list VALUES (95, 'CREATE_MY_CONTAINER', 'Allows a user to create a container for themselves');
105 INSERT INTO permission.perm_list VALUES (96, 'VIEW_HOLD_NOTIFICATION', 'Allows a user to view notifications attached to a hold');
106 INSERT INTO permission.perm_list VALUES (97, 'CREATE_HOLD_NOTIFICATION', 'Allows a user to create new hold notifications');
107 INSERT INTO permission.perm_list VALUES (98, 'UPDATE_ORG_SETTING', 'Allows a user to update an org unit setting');
108 INSERT INTO permission.perm_list VALUES (99, 'OFFLINE_UPLOAD', 'Allows a user to upload an offline script');
109 INSERT INTO permission.perm_list VALUES (100, 'OFFLINE_VIEW', 'Allows a user to view uploaded offline script information');
110 INSERT INTO permission.perm_list VALUES (101, 'OFFLINE_EXECUTE', 'Allows a user to execute an offline script batch');
111 INSERT INTO permission.perm_list VALUES (102, 'CIRC_OVERRIDE_DUE_DATE', 'Allows a user to change set the due date on an item to any date');
112 INSERT INTO permission.perm_list VALUES (103, 'CIRC_PERMIT_OVERRIDE', 'Allows a user to bypass the circ permit call for checkout');
113 INSERT INTO permission.perm_list VALUES (104, 'COPY_IS_REFERENCE.override', 'Allows a user to override the copy_is_reference event');
114
115 SELECT SETVAL('permission.perm_list_id_seq'::TEXT, 105);
116 */
117
118 CREATE TABLE permission.grp_tree (
119         id                      SERIAL  PRIMARY KEY,
120         name                    TEXT    NOT NULL UNIQUE,
121         parent                  INT     REFERENCES permission.grp_tree (id) ON DELETE RESTRICT,
122         perm_interval           INTERVAL DEFAULT '3 years'::interval NOT NULL,
123         description             TEXT,
124         application_perm        TEXT
125 );
126 CREATE INDEX grp_tree_parent_idx ON permission.grp_tree (parent);
127
128 /*
129 INSERT INTO permission.grp_tree VALUES (1, 'Users', NULL, NULL, '3 years');
130 INSERT INTO permission.grp_tree VALUES (2, 'Patrons', 1, NULL, '3 years');
131 INSERT INTO permission.grp_tree VALUES (3, 'Staff', 1, NULL, '3 years');
132 INSERT INTO permission.grp_tree VALUES (4, 'Catalogers', 3, NULL, '3 years');
133 INSERT INTO permission.grp_tree VALUES (5, 'Circulators', 3, NULL, '3 years');
134 INSERT INTO permission.grp_tree VALUES (10, 'Local System Administrator', 3, 'System maintenance, configuration, etc.', '3 years');
135
136 SELECT SETVAL('permission.grp_tree_id_seq'::TEXT, 11);
137 */
138
139 CREATE TABLE permission.grp_perm_map (
140         id              SERIAL  PRIMARY KEY,
141         grp             INT     NOT NULL REFERENCES permission.grp_tree (id) ON DELETE CASCADE,
142         perm            INT     NOT NULL REFERENCES permission.perm_list (id) ON DELETE CASCADE,
143         depth           INT     NOT NULL,
144         grantable       BOOL    NOT NULL DEFAULT FALSE,
145                 CONSTRAINT perm_grp_once UNIQUE (grp,perm)
146 );
147
148 /*
149 INSERT INTO permission.grp_perm_map VALUES (57, 2, 15, 0, false);
150 INSERT INTO permission.grp_perm_map VALUES (109, 2, 95, 0, false);
151 INSERT INTO permission.grp_perm_map VALUES (1, 1, 2, 0, false);
152 INSERT INTO permission.grp_perm_map VALUES (12, 1, 5, 0, false);
153 INSERT INTO permission.grp_perm_map VALUES (13, 1, 6, 0, false);
154 INSERT INTO permission.grp_perm_map VALUES (51, 1, 32, 0, false);
155 INSERT INTO permission.grp_perm_map VALUES (111, 1, 95, 0, false);
156 INSERT INTO permission.grp_perm_map VALUES (11, 3, 4, 0, false);
157 INSERT INTO permission.grp_perm_map VALUES (14, 3, 7, 2, false);
158 INSERT INTO permission.grp_perm_map VALUES (16, 3, 9, 0, false);
159 INSERT INTO permission.grp_perm_map VALUES (19, 3, 15, 0, false);
160 INSERT INTO permission.grp_perm_map VALUES (20, 3, 16, 0, false);
161 INSERT INTO permission.grp_perm_map VALUES (21, 3, 17, 0, false);
162 INSERT INTO permission.grp_perm_map VALUES (116, 3, 18, 0, false);
163 INSERT INTO permission.grp_perm_map VALUES (117, 3, 20, 0, false);
164 INSERT INTO permission.grp_perm_map VALUES (118, 3, 21, 2, false);
165 INSERT INTO permission.grp_perm_map VALUES (119, 3, 22, 2, false);
166 INSERT INTO permission.grp_perm_map VALUES (120, 3, 23, 2, false);
167 INSERT INTO permission.grp_perm_map VALUES (121, 3, 25, 2, false);
168 INSERT INTO permission.grp_perm_map VALUES (26, 3, 27, 0, false);
169 INSERT INTO permission.grp_perm_map VALUES (27, 3, 28, 0, false);
170 INSERT INTO permission.grp_perm_map VALUES (28, 3, 29, 0, false);
171 INSERT INTO permission.grp_perm_map VALUES (29, 3, 30, 0, false);
172 INSERT INTO permission.grp_perm_map VALUES (44, 3, 31, 0, false);
173 INSERT INTO permission.grp_perm_map VALUES (31, 3, 33, 0, false);
174 INSERT INTO permission.grp_perm_map VALUES (32, 3, 34, 0, false);
175 INSERT INTO permission.grp_perm_map VALUES (33, 3, 35, 0, false);
176 INSERT INTO permission.grp_perm_map VALUES (41, 3, 36, 0, false);
177 INSERT INTO permission.grp_perm_map VALUES (45, 3, 37, 0, false);
178 INSERT INTO permission.grp_perm_map VALUES (46, 3, 38, 0, false);
179 INSERT INTO permission.grp_perm_map VALUES (47, 3, 39, 0, false);
180 INSERT INTO permission.grp_perm_map VALUES (122, 3, 41, 0, false);
181 INSERT INTO permission.grp_perm_map VALUES (123, 3, 43, 0, false);
182 INSERT INTO permission.grp_perm_map VALUES (60, 3, 44, 0, false);
183 INSERT INTO permission.grp_perm_map VALUES (110, 3, 45, 0, false);
184 INSERT INTO permission.grp_perm_map VALUES (124, 3, 8, 2, false);
185 INSERT INTO permission.grp_perm_map VALUES (125, 3, 24, 2, false);
186 INSERT INTO permission.grp_perm_map VALUES (126, 3, 19, 0, false);
187 INSERT INTO permission.grp_perm_map VALUES (61, 3, 47, 2, false);
188 INSERT INTO permission.grp_perm_map VALUES (95, 3, 48, 0, false);
189 INSERT INTO permission.grp_perm_map VALUES (17, 3, 11, 0, false);
190 INSERT INTO permission.grp_perm_map VALUES (62, 3, 42, 0, false);
191 INSERT INTO permission.grp_perm_map VALUES (63, 3, 49, 0, false);
192 INSERT INTO permission.grp_perm_map VALUES (64, 3, 50, 0, false);
193 INSERT INTO permission.grp_perm_map VALUES (127, 3, 53, 0, false);
194 INSERT INTO permission.grp_perm_map VALUES (65, 3, 54, 0, false);
195 INSERT INTO permission.grp_perm_map VALUES (128, 3, 55, 2, false);
196 INSERT INTO permission.grp_perm_map VALUES (67, 3, 56, 2, false);
197 INSERT INTO permission.grp_perm_map VALUES (68, 3, 57, 2, false);
198 INSERT INTO permission.grp_perm_map VALUES (69, 3, 58, 2, false);
199 INSERT INTO permission.grp_perm_map VALUES (70, 3, 59, 2, false);
200 INSERT INTO permission.grp_perm_map VALUES (71, 3, 60, 2, false);
201 INSERT INTO permission.grp_perm_map VALUES (72, 3, 61, 2, false);
202 INSERT INTO permission.grp_perm_map VALUES (73, 3, 62, 2, false);
203 INSERT INTO permission.grp_perm_map VALUES (74, 3, 63, 2, false);
204 INSERT INTO permission.grp_perm_map VALUES (81, 3, 72, 2, false);
205 INSERT INTO permission.grp_perm_map VALUES (82, 3, 73, 2, false);
206 INSERT INTO permission.grp_perm_map VALUES (83, 3, 74, 2, false);
207 INSERT INTO permission.grp_perm_map VALUES (84, 3, 75, 0, false);
208 INSERT INTO permission.grp_perm_map VALUES (85, 3, 76, 2, false);
209 INSERT INTO permission.grp_perm_map VALUES (86, 3, 77, 2, false);
210 INSERT INTO permission.grp_perm_map VALUES (89, 3, 79, 0, false);
211 INSERT INTO permission.grp_perm_map VALUES (90, 3, 80, 0, false);
212 INSERT INTO permission.grp_perm_map VALUES (91, 3, 81, 0, false);
213 INSERT INTO permission.grp_perm_map VALUES (92, 3, 82, 0, false);
214 INSERT INTO permission.grp_perm_map VALUES (98, 3, 83, 0, false);
215 INSERT INTO permission.grp_perm_map VALUES (115, 3, 84, 0, false);
216 INSERT INTO permission.grp_perm_map VALUES (100, 3, 85, 0, false);
217 INSERT INTO permission.grp_perm_map VALUES (101, 3, 86, 0, false);
218 INSERT INTO permission.grp_perm_map VALUES (102, 3, 87, 0, false);
219 INSERT INTO permission.grp_perm_map VALUES (103, 3, 89, 2, false);
220 INSERT INTO permission.grp_perm_map VALUES (104, 3, 88, 2, false);
221 INSERT INTO permission.grp_perm_map VALUES (108, 3, 94, 0, false);
222 INSERT INTO permission.grp_perm_map VALUES (112, 3, 96, 0, false);
223 INSERT INTO permission.grp_perm_map VALUES (113, 3, 97, 0, false);
224 INSERT INTO permission.grp_perm_map VALUES (130, 3, 99, 1, false);
225 INSERT INTO permission.grp_perm_map VALUES (131, 3, 100, 1, false);
226 INSERT INTO permission.grp_perm_map VALUES (22, 4, 18, 0, false);
227 INSERT INTO permission.grp_perm_map VALUES (24, 4, 20, 0, false);
228 INSERT INTO permission.grp_perm_map VALUES (38, 4, 21, 2, false);
229 INSERT INTO permission.grp_perm_map VALUES (34, 4, 22, 2, false);
230 INSERT INTO permission.grp_perm_map VALUES (39, 4, 23, 2, false);
231 INSERT INTO permission.grp_perm_map VALUES (35, 4, 25, 2, false);
232 INSERT INTO permission.grp_perm_map VALUES (129, 4, 26, 2, false);
233 INSERT INTO permission.grp_perm_map VALUES (15, 4, 8, 2, false);
234 INSERT INTO permission.grp_perm_map VALUES (40, 4, 24, 2, false);
235 INSERT INTO permission.grp_perm_map VALUES (23, 4, 19, 0, false);
236 INSERT INTO permission.grp_perm_map VALUES (66, 4, 55, 2, false);
237 INSERT INTO permission.grp_perm_map VALUES (134, 10, 51, 1, false);
238 INSERT INTO permission.grp_perm_map VALUES (75, 10, 66, 2, false);
239 INSERT INTO permission.grp_perm_map VALUES (76, 10, 67, 2, false);
240 INSERT INTO permission.grp_perm_map VALUES (77, 10, 68, 2, false);
241 INSERT INTO permission.grp_perm_map VALUES (78, 10, 69, 2, false);
242 INSERT INTO permission.grp_perm_map VALUES (79, 10, 70, 2, false);
243 INSERT INTO permission.grp_perm_map VALUES (80, 10, 71, 2, false);
244 INSERT INTO permission.grp_perm_map VALUES (87, 10, 78, 2, false);
245 INSERT INTO permission.grp_perm_map VALUES (105, 10, 91, 1, false);
246 INSERT INTO permission.grp_perm_map VALUES (106, 10, 92, 1, false);
247 INSERT INTO permission.grp_perm_map VALUES (107, 10, 93, 0, false);
248 INSERT INTO permission.grp_perm_map VALUES (114, 10, 98, 1, false);
249 INSERT INTO permission.grp_perm_map VALUES (132, 10, 101, 1, true);
250 INSERT INTO permission.grp_perm_map VALUES (136, 10, 102, 1, false);
251 INSERT INTO permission.grp_perm_map VALUES (137, 10, 103, 1, false);
252 INSERT INTO permission.grp_perm_map VALUES (97, 5, 41, 0, false);
253 INSERT INTO permission.grp_perm_map VALUES (96, 5, 43, 0, false);
254 INSERT INTO permission.grp_perm_map VALUES (93, 5, 48, 0, false);
255 INSERT INTO permission.grp_perm_map VALUES (94, 5, 53, 0, false);
256 INSERT INTO permission.grp_perm_map VALUES (133, 5, 102, 0, false);
257 INSERT INTO permission.grp_perm_map VALUES (138, 5, 104, 1, false);
258
259 SELECT SETVAL('permission.grp_perm_map_id_seq'::TEXT, 139);
260 */
261
262 CREATE TABLE permission.usr_perm_map (
263         id              SERIAL  PRIMARY KEY,
264         usr             INT     NOT NULL REFERENCES actor.usr (id) ON DELETE CASCADE,
265         perm            INT     NOT NULL REFERENCES permission.perm_list (id) ON DELETE CASCADE,
266         depth           INT     NOT NULL,
267         grantable       BOOL    NOT NULL DEFAULT FALSE,
268                 CONSTRAINT perm_usr_once UNIQUE (usr,perm)
269 );
270
271 CREATE TABLE permission.usr_grp_map (
272         id      SERIAL  PRIMARY KEY,
273         usr     INT     NOT NULL REFERENCES actor.usr (id) ON DELETE CASCADE,
274         grp     INT     NOT NULL REFERENCES permission.grp_tree (id) ON DELETE CASCADE,
275                 CONSTRAINT usr_grp_once UNIQUE (usr,grp)
276 );
277
278 -- Admin user
279 INSERT INTO permission.usr_perm_map (usr,perm,depth) VALUES (1,-1,0);
280
281 CREATE OR REPLACE FUNCTION permission.grp_ancestors ( INT ) RETURNS SETOF permission.grp_tree AS $$
282         SELECT  a.*
283         FROM    connectby('permission.grp_tree','parent','id','name',$1,'100','.')
284                         AS t(keyid text, parent_keyid text, level int, branch text,pos int)
285                 JOIN permission.grp_tree a ON a.id = t.keyid
286         ORDER BY
287                 CASE WHEN a.parent IS NULL
288                         THEN 0
289                         ELSE 1
290                 END, a.name;
291 $$ LANGUAGE SQL STABLE;
292
293 CREATE OR REPLACE FUNCTION permission.usr_perms ( INT ) RETURNS SETOF permission.usr_perm_map AS $$
294         SELECT  DISTINCT ON (usr,perm) *
295           FROM  (
296                         (SELECT * FROM permission.usr_perm_map WHERE usr = $1)
297                                         UNION ALL
298                         (SELECT -p.id, $1 AS usr, p.perm, p.depth, p.grantable
299                           FROM  permission.grp_perm_map p
300                           WHERE p.grp IN (
301                                 SELECT  (permission.grp_ancestors(
302                                                 (SELECT profile FROM actor.usr WHERE id = $1)
303                                         )).id
304                                 )
305                         )
306                                         UNION ALL
307                         (SELECT -p.id, $1 AS usr, p.perm, p.depth, p.grantable
308                           FROM  permission.grp_perm_map p 
309                           WHERE p.grp IN (SELECT (permission.grp_ancestors(m.grp)).id FROM permission.usr_grp_map m WHERE usr = $1))
310                 ) AS x
311           ORDER BY 2, 3, 1 DESC, 5 DESC ;
312 $$ LANGUAGE SQL STABLE;
313
314 CREATE OR REPLACE FUNCTION permission.usr_can_grant_perm ( iuser INT, tperm TEXT, target_ou INT ) RETURNS BOOL AS $$
315 DECLARE
316         r_usr   actor.usr%ROWTYPE;
317         r_perm  permission.usr_perm_map%ROWTYPE;
318 BEGIN
319
320         SELECT * INTO r_usr FROM actor.usr WHERE id = iuser;
321
322         IF r_usr.active = FALSE THEN
323                 RETURN FALSE;
324         END IF;
325
326         IF r_usr.super_user = TRUE THEN
327                 RETURN TRUE;
328         END IF;
329
330
331         FOR r_perm IN   SELECT  *
332                           FROM  permission.usr_perms(iuser) p
333                                 JOIN permission.perm_list l
334                                         ON (l.id = p.perm)
335                           WHERE (l.code = tperm AND p.grantable IS TRUE)
336                 LOOP
337
338                 PERFORM *
339                   FROM  actor.org_unit_descendants(target_ou,r_perm.depth)
340                   WHERE id = r_usr.home_ou;
341
342                 IF FOUND THEN
343                         RETURN TRUE;
344                 ELSE
345                         RETURN FALSE;
346                 END IF;
347         END LOOP;
348
349         RETURN FALSE;
350 END;
351 $$ LANGUAGE PLPGSQL;
352
353 CREATE OR REPLACE FUNCTION permission.usr_has_perm ( iuser INT, tperm TEXT, target_ou INT ) RETURNS BOOL AS $$
354 DECLARE
355         r_usr   actor.usr%ROWTYPE;
356         r_perm  permission.usr_perm_map%ROWTYPE;
357 BEGIN
358
359         SELECT * INTO r_usr FROM actor.usr WHERE id = iuser;
360
361         IF r_usr.active = FALSE THEN
362                 RETURN FALSE;
363         END IF;
364
365         IF r_usr.super_user = TRUE THEN
366                 RETURN TRUE;
367         END IF;
368
369
370         FOR r_perm IN   SELECT  *
371                           FROM  permission.usr_perms(iuser) p
372                                 JOIN permission.perm_list l
373                                         ON (l.id = p.perm)
374                           WHERE l.code = tperm
375                                 OR p.perm = -1 LOOP
376
377                 PERFORM *
378                   FROM  actor.org_unit_descendants(target_ou,r_perm.depth)
379                   WHERE id = r_usr.home_ou;
380
381                 IF FOUND THEN
382                         RETURN TRUE;
383                 ELSE
384                         RETURN FALSE;
385                 END IF;
386         END LOOP;
387
388         RETURN FALSE;
389 END;
390 $$ LANGUAGE PLPGSQL;
391
392 COMMIT;
393