-
Notifications
You must be signed in to change notification settings - Fork 7
/
islandora_workflow.permissions.inc
767 lines (686 loc) · 23.1 KB
/
islandora_workflow.permissions.inc
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
<?php
/**
* @file
* This file contains all of the helper functions dealing with Permissions for
* Islandora workflow.
*/
/**
* Get details of Islandora Workflow user permissions.
*
* Queries the islandora_workflow_user_permissions table of the database.
*
* @param array $options
* An associative array of options if no options are passed in then the
* function will query for everything.
* The array contains the following elements:
* - 'user': (int) the Drupal uid to query for.
* - 'collection': (string) the PID of the collection.
* - 'permissions': (array) an array of permission strings.
* - 'info': (boolean) if TRUE a join is done to return the username as well
* as drupal user id.
*
* @return array
* indexed array containing all of the results returned.
* each individual result is a associative array
* with the keys: user, collection, permission and optionally name.
*/
function islandora_workflow_get_user_permissions(array $options = array()) {
$user = isset($options['user']) ? $options['user'] : NULL;
$collection = isset($options['collection']) ? $options['collection'] : NULL;
$permissions = isset($options['permissions']) ? $options['permissions'] : NULL;
$userinfo = isset($options['info']) ? $options['info'] : FALSE;
$and = FALSE;
$where = FALSE;
$results = array();
$args = array();
if ($userinfo) {
$query = 'SELECT user, name, collection, permission FROM {islandora_workflow_user_permissions} iw, ' .
'{users} u WHERE iw.user = u.uid';
$and = TRUE;
$where = TRUE;
}
else {
$query = 'SELECT user, collection, permission FROM {islandora_workflow_user_permissions} iw';
}
if ($user) {
if (!$where) {
$query .= ' WHERE';
$where = TRUE;
}
if ($and) {
$query .= ' AND';
}
else {
$and = TRUE;
}
$query .= " iw.user = %d";
$args[] = $user;
}
if ($collection) {
if (!$where) {
$query .= ' WHERE';
$where = TRUE;
}
if ($and) {
$query .= ' AND';
}
else {
$and = TRUE;
}
$query .= " iw.collection = '%s'";
$args[] = $collection;
}
if ($permissions) {
if (!$where) {
$query .= ' WHERE';
$where = TRUE;
}
if ($and) {
$query .= ' AND';
}
else {
$and = TRUE;
}
$or = FALSE;
$query .= ' (';
foreach ($permissions as $permission) {
if ($or) {
$query .= ' OR';
}
$query .= " iw.permission = '%s'";
$args[] = $permission;
$or = TRUE;
}
$query .= ') ';
}
array_unshift($args, $query);
$resource = call_user_func_array('db_query', $args);
// Build return array.
while ($row = db_fetch_array($resource)) {
$results[] = $row;
}
return $results;
}
/**
* Get details of Islandora Workflow role permissions.
*
* Queries the islandora_workflow_role_permissions table of the database.
*
* @param array $options
* An associative array of options if no options are
* passed in then the function will
* query for everything. The array contains the following elements:
* - 'roles': (array) an array of drupal rid to query for.
* - 'collection': (string) the pid of the collection.
* - 'permissions': (array) an array of permissions strings.
* - 'info': (boolean) if TRUE a join is done to return the
* role name as well as drupal role id.
*
* @return array
* indexed array containing all of the results returned. each individual
* result is a associative array
* with the keys: role, collection, permission and optionally name.
*/
function islandora_workflow_get_role_permissions(array $options = array()) {
$roles = isset($options['roles']) ? $options['roles'] : array();
$collection = isset($options['collection']) ? $options['collection'] : NULL;
$permissions = isset($options['permissions']) ? $options['permissions'] : NULL;
$roleinfo = isset($options['info']) ? $options['info'] : FALSE;
$and = FALSE;
$where = FALSE;
$results = array();
$args = array();
if ($roleinfo) {
$query = 'SELECT role, name, collection, permission FROM {islandora_workflow_role_permissions} iw, ' .
'{role} r WHERE iw.role = r.rid';
$and = TRUE;
$where = TRUE;
}
else {
$query = 'SELECT role, collection, permission FROM {islandora_workflow_role_permissions} iw';
}
if ($roles) {
if (!$where) {
$query .= ' WHERE';
$where = TRUE;
}
if ($and) {
$query .= ' AND';
}
else {
$and = TRUE;
}
$or = FALSE;
$query .= ' (';
foreach ($roles as $role) {
if ($or) {
$query .= ' OR';
}
$query .= " iw.role = %d";
$args[] = $role;
$or = TRUE;
}
$query .= ' )';
}
if ($collection) {
if (!$where) {
$query .= ' WHERE';
$where = TRUE;
}
if ($and) {
$query .= ' AND';
}
else {
$and = TRUE;
}
$query .= " iw.collection = '%s'";
$args[] = $collection;
}
if ($permissions) {
if (!$where) {
$query .= ' WHERE';
$where = TRUE;
}
if ($and) {
$query .= ' AND';
}
else {
$and = TRUE;
}
$or = FALSE;
$query .= '(';
foreach ($permissions as $permission) {
if ($or) {
$query .= ' OR';
}
$query .= " iw.permission = '%s'";
$args[] = $permission;
$or = TRUE;
}
$query .= ')';
}
array_unshift($args, $query);
$resource = call_user_func_array('db_query', $args);
// Build return array.
while ($row = db_fetch_array($resource)) {
$results[] = $row;
}
return $results;
}
/**
* Get the users and roles with access to a objects in a collection
* that have a given state.
*
* It is used when making XACML policies.
*
* @param string $collection
* Collection PID.
* @param string $state
* Workflow state.
*
* @return array()
* An associative array containing 'roles' and 'users'.
* Each of those is an indexed array containing the usernames and rolenames.
*/
function islandora_workflow_get_permission_state($collection, $state) {
$permission = array();
$users = array();
$roles = array();
switch ($state) {
case 'created':
case 'rejected':
case 'submitted':
$permission[] = 'islandora_workflow_Editor';
case 'approved':
$permission[] = 'islandora_workflow_Manager';
default:
$permission[] = 'islandora_workflow_Administrator';
}
$user_results = islandora_workflow_get_user_permissions(array(
'collection' => $collection,
'info' => TRUE,
'permissions' => $permission,
));
$role_results = islandora_workflow_get_role_permissions(array(
'collection' => $collection,
'info' => TRUE,
'permissions' => $permission,
));
foreach ($role_results as $role) {
$roles[] = $role['name'];
}
foreach ($user_results as $user) {
$users[] = $user['name'];
}
$users = array_merge(islandora_workflow_user_access_list('islandora_workflow_Administrator'), $users);
return array('users' => $users, 'roles' => $roles);
}
/**
* Get all role permission entries, ordered by role and collection.
*
* This function queries Drupal's DB for all the collection permission
* entries by role used when displaying the current state
* for the permission page.
*
* @return array
* $perm_list list of permissions that roles have with the form
* $perm_list[$role_id][$collection_id]=perms
*/
function islandora_workflow_get_all_role_permissions() {
$perm_list = array();
$results = islandora_workflow_get_role_permissions();
// Build return array.
foreach ($results as $row) {
$perm_list[$row['role']][$row['collection']] = $row['permission'];
}
return $perm_list;
}
/**
* Get all role permission entries, ordered by role and collection.
*
* Used when displaying the current state for the permission page
*
* @return array
* $perm_list list of all permissions that users have with the form
* $perm_list[$user_id][$collection_id][1...4]=perms
*/
function islandora_workflow_get_all_user_permissions() {
$perm_list = array();
$results = islandora_workflow_get_user_permissions();
// Build return array.
foreach ($results as $row) {
$perm_list[$row['user']][$row['collection']] = $row['permission'];
}
return $perm_list;
}
/**
* Get all Drupal users with any level of access to a given collection.
*
* This function will get all the drupal users who have the
* indicated (or any level of access, if none is indicated)
* to the indicated collections.
*
* @param array $collections
* the collection(s) to get the permissions on
* @param string $permission_level
* Defaults to NULL; the permission level users to return
*
* @return array
* $users an associative array of the indicated collections
* and all the Drupal user names that have permissions on them
*/
function islandora_workflow_get_all_users_with_collection_permissions($collections, $permission_level = NULL) {
// All user perms.
$user_permissions = islandora_workflow_get_all_user_permissions();
// All role perms.
$role_permissions = islandora_workflow_get_all_role_permissions();
$users_via_role = array();
// All users that may be in collections.
$possible_users = islandora_workflow_get_users_with_module_permissions();
$users = array();
$admins = islandora_workflow_user_access_list('islandora_workflow_Administrator');
/* Make sure those users with admin rights show up for every
collection/perm level.*/
foreach ($collections as $collection_id) {
$users[$collection_id] = array();
// Islandora_workflow_Administrator.
foreach ($admins as $user_id => $user_name) {
$users[$collection_id][] = $user_name;
}
}
// From user perms.
foreach ($user_permissions as $user_id => $collection_permission) {
foreach ($collection_permission as $collection_id => $permission) {
if (($permission_level == NULL || $permission == $permission_level) && in_array($collection_id, $collections)) {
$user_name = user_load($user_id)->name;
// Initialize the collection if it needs to be.
if (!isset($users[$collection_id])) {
$users[$collection_id] = array();
}
if (!in_array($user_name, $users[$collection_id])) {
/* Add the user to the
* users array for the collection if they aren't in it.*/
$users[$collection_id][] = $user_name;
}
}
}
}
// From role perms.
foreach ($role_permissions as $role_id => $collection_permission) {
foreach ($collection_permission as $collection_id => $permission) {
if (($permission_level == NULL || $permission == $permission_level) && in_array($collection_id, $collections)) {
// Populate users per role.
if (!isset($users_via_role[$role_id])) {
$users_via_role[$role_id] = array();
$users_query = "SELECT u.uid FROM {users} u, {users_roles} ur, {permission} p
WHERE u.uid = ur.uid
AND ur.rid = p.rid
AND p.rid LIKE '%%%s%'
";
$users_result = db_query($users_query, $role_id);
while ($row = db_fetch_object($users_result)) {
$users_via_role[$role_id][] = $row->uid;
}
}
/* Add the user/collection relationships for each
* role to the $users list if they are not already on it.*/
foreach ($users_via_role[$role_id] as $user_id) {
$user_name = user_load($user_id)->name;
if (!isset($users[$collection_id])) {
$users[$collection_id] = array();
}
if (!in_array($user_name, $users[$collection_id])) {
$users[$collection_id][] = $user_name;
}
}
}
}
}
return $users;
}
/**
* Query the core Drupal database for all users with a specified permission.
*
* @param string $perm
* the permission to search for
*
* @return array
* $users the list of users with the permission in the form
* users[user_id]=user_name or false
*/
function islandora_workflow_user_access_list($perm = NULL) {
$users = FALSE;
if (isset($perm)) {
$users_query = "SELECT u.uid, u.name FROM {users} u, {users_roles} ur, {permission} p
WHERE u.uid = ur.uid
AND ur.rid = p.rid
AND p.perm LIKE '%%%s%'
";
$users_result = db_query($users_query, $perm);
$users = array();
while ($u = db_fetch_object($users_result)) {
$users[$u->uid] = $u->name;
}
/*Drupal 6(this may be different in 7) users_roles
* table does not record authenticated/anonymous/admin user
*I need to know about authenticated users so,
*here we manualy check and add them to the list
*/
$authenticated_users_query = "SELECT rid, perm FROM {permission}
WHERE rid = 2
AND perm LIKE '%%%s%'
";
$authenticated_users_result = db_query($authenticated_users_query, $perm);
while ($u = db_fetch_object($authenticated_users_result)) {
$get_authenticated_users_query = "SELECT uid, name FROM {users}
WHERE uid > 0
";
$get_authenticated_users_result = db_query($get_authenticated_users_query);
while ($u = db_fetch_object($get_authenticated_users_result)) {
$users[$u->uid] = $u->name;
}
}
}
return $users;
}
/**
* Get collection permissions related to a user's roles.
*
* Defaults to the current user.
* Queries the Drupal database.
*
* Most useful for building the work portal,
* and also used for getting user's owned collections
*
* @return array
* $perm_listlist of permissions the current user has with the indicated
* $perm_list[$role_id][$collection_id]=perms
*/
function islandora_workflow_get_users_role_permissions($account = NULL) {
/* Do islandora_workflow_get_all_role_permissions with
restrictions on the roles to the list of roles the current user has. */
$perm_list = array();
// Use $user->roles to get an array $roles[$role_id]=[$role_name].
global $user;
if (empty($account)) {
$account = drupal_clone($user);
}
$roles = array();
foreach ($account->roles as $rid => $name) {
$roles[] = $rid;
}
// Build query.
$results = islandora_workflow_get_role_permissions(array('roles' => $roles));
// Build return array.
foreach ($results as $row) {
$perm_list[$row['role']][$row['collection']] = $row['permission'];
}
return $perm_list;
}
/**
* Get all user-based collection permissions for a user.
*
* Defaults to the current user.
* Queries the Drupal database.
*
* Most useful for building the work portal, and also used for getting a user's
* owned collections
*
* @return array
* $perm_listlist of permissions the current user has with the indicated
* $perm_list[$role_id][$collection_id]=perms
*/
function islandora_workflow_get_users_user_permissions($account = NULL) {
/* Do islandora_workflow_get_all_role_permissions with restrictions on
the roles to the list of roles the current user has*/
$perm_list = array();
global $user;
if (empty($account)) {
$account = drupal_clone($user);
}
// Build query.
$results = islandora_workflow_get_user_permissions(array('user' => $account->uid));
// Build return array.
foreach ($results as $row) {
$perm_list[$row['user']][$row['collection']] = $row['permission'];
}
return $perm_list;
}
/**
* Get a list of all roles with islandora_workflow permissions.
*
* @return array
* $privileged_roles The list of roles
*/
function islandora_workflow_get_roles_with_module_permissions() {
$privileged_roles = array();
// Use user_roles for each permission individualy.
$admin_roles = user_roles($members_only = FALSE, 'islandora_workflow_Administrator');
$manager_roles = user_roles($members_only = FALSE, 'islandora_workflow_Manager');
$editor_roles = user_roles($members_only = FALSE, 'islandora_workflow_Editor');
$submitter_roles = user_roles($members_only = FALSE, 'islandora_workflow_Submitter');
/* Modify the returned arrays to include the permission name,
and merge into one array [role_pid][role_name][0...4]permission*/
foreach ($admin_roles as $role_pid => $role_name) {
$privileged_roles[$role_pid][$role_name][] = 'islandora_workflow_Administrator';
}
foreach ($manager_roles as $role_pid => $role_name) {
$privileged_roles[$role_pid][$role_name][] = 'islandora_workflow_Manager';
}
foreach ($editor_roles as $role_pid => $role_name) {
$privileged_roles[$role_pid][$role_name][] = 'islandora_workflow_Editor';
}
foreach ($submitter_roles as $role_pid => $role_name) {
$privileged_roles[$role_pid][$role_name][] = 'islandora_workflow_Submitter';
}
return $privileged_roles;
}
/**
* Get a list of all users that have islandora_workflow permissions.
*
* @return array
* $privileged_users The list of users
*/
function islandora_workflow_get_users_with_module_permissions() {
$privileged_users = array();
// Use user_roles for each permission individualy.
$admin_users = islandora_workflow_user_access_list('islandora_workflow_Administrator');
$manager_users = islandora_workflow_user_access_list('islandora_workflow_Manager');
$editor_users = islandora_workflow_user_access_list('islandora_workflow_Editor');
$submitter_users = islandora_workflow_user_access_list('islandora_workflow_Submitter');
/* Modify the returned arrays to include the permission name,
and merge into one array [role_pid][role_name][0...4]permission. */
foreach ($admin_users as $user_id => $user_name) {
$privileged_users[$user_id][$user_name]['islandora_workflow_Administrator'] = 'islandora_workflow_Administrator';
}
foreach ($manager_users as $user_id => $user_name) {
$privileged_users[$user_id][$user_name]['islandora_workflow_Manager'] = 'islandora_workflow_Manager';
}
foreach ($editor_users as $user_id => $user_name) {
$privileged_users[$user_id][$user_name]['islandora_workflow_Editor'] = 'islandora_workflow_Editor';
}
foreach ($submitter_users as $user_id => $user_name) {
$privileged_users[$user_id][$user_name]['islandora_workflow_Submitter'] = 'islandora_workflow_Submitter';
}
return $privileged_users;
}
/**
* Check a user's workflow permissions on a given object.
*
* This function is for general workflow permission retrieval on individual
* non-collection objects. Please note that this function does not simply report
* the indicated user's permission level with the parent object.
* The function also takes into consideration the workflow state of the object,
* and only reports those
* permissions that will allow for altering the state of the object.
* e.g. islandora_workflow_Submitter will not be
* reported on objects that have the
* 'published' workflow state.
* Init is broken up for performance reasons.
*
* @param string $object_id
* The object that we are looking for permissions on
*
* @param object $account
* The Drupal user whose permissions we are
* checking; NULL for the current user
*
* @return mixed
* Either the string of the highest available permission
* to the user on the object, or FALSE if there are no permissions
*/
function islandora_workflow_user_object_permission_check($object_id, $account = NULL) {
module_load_include('collection.inc', 'islandora_workflow');
module_load_include('inc', 'islandora_workflow');
global $user;
if (empty($account)) {
$account = drupal_clone($user);
}
$manager_permissions = islandora_workflow_get_users_collections('islandora_workflow_Manager', $account);
$parent = islandora_workflow_get_object_parent($object_id);
// If the user has manager level access return true.
if (isset($manager_permissions[$parent])) {
return 'islandora_workflow_Manager';
}
$editor_permissions = islandora_workflow_get_users_collections('islandora_workflow_Editor', $account);
$workflow_state = islandora_workflow_get_object_workflow_state($object_id);
// Editor level.
if (isset($editor_permissions[$parent]) && ($workflow_state == 'submitted' || $workflow_state == 'created' || $workflow_state == 'rejected')) {
return 'islandora_workflow_Editor';
}
// Even more init.
$submitter_permissions = islandora_workflow_get_users_collections('islandora_workflow_Submitter', $account);
$creator = islandora_workflow_get_object_creator($object_id);
// Submitter level.
if (isset($submitter_permissions[$parent]) && ($workflow_state == 'created' || $workflow_state == 'rejected') && $account->name == $creator) {
return 'islandora_workflow_Submitter';
}
// Permission denied.
return FALSE;
}
/**
* Check the current user's permissions on a given collection.
*
* @param string $collection_pid
* The PID of the collection.
*
* @return mixed
* The level of permission if one exists, otherwise FALSE.
*/
function islandora_workflow_user_collection_permission_check($collection_pid) {
module_load_include('collection.inc', 'islandora_workflow');
module_load_include('inc', 'islandora_workflow');
$manager_permissions = islandora_workflow_get_users_collections('islandora_workflow_Manager');
if ($manager_permissions[$collection_pid]) {
return 'islandora_workflow_Manager';
}
$editor_permissions = islandora_workflow_get_users_collections('islandora_workflow_Editor');
if ($editor_permissions[$collection_pid]) {
return 'islandora_workflow_Editor';
}
$submitter_permissions = islandora_workflow_get_users_collections('islandora_workflow_Submitter');
if ($submitter_permissions[$collection_pid]) {
return 'islandora_workflow_Submitter';
}
// Permission denied.
return FALSE;
}
/**
* This function will remove any out of date references
* in the islandora_workflow_user_permissions regarding the
* indicated user.
*
* @param object $account
* A Drupal "user" object
*/
function islandora_workflow_normalize_users_permissions($account = NULL) {
if (is_null($account)) {
global $user;
$account = drupal_clone($user);
}
$possible_perms = islandora_workflow_perm();
foreach ($possible_perms as $permission) {
if (!user_access($permission, $account)) {
db_query('DELETE FROM {islandora_workflow_user_permissions}
WHERE user = %d AND permission = %d', $account->uid, $permission);
}
}
}
/**
* This function will clear any role level permissions in the
* Drupal database regarding workflow collections. It will
* also normalize all the permissions of users who have that role.
*
* @param object $role_id
* A Drupal role id.
*/
function islandora_workflow_clear_role_permissions($role_id) {
db_query('DELETE FROM {islandora_workflow_role_permissions} WHERE role = %d', $role_id);
// Get all users with the role and normalize their perms.
$users = islandora_workflow_get_users_by_roles($role_id);
foreach ($users as $user_data) {
islandora_workflow_normalize_users_permissions(user_load($user_data['uid']));
}
}
/**
* This function will return all users with a given role.
*
* @param int $role_id
* A Drupal role id.
*
* @return array
* $users[$result->uid] = $result->name;
*/
function islandora_workflow_get_users_by_roles($role_id) {
$users = array();
$sql = "SELECT u.uid, u.name
FROM {users} u
INNER JOIN {users_roles} ur
ON u.uid = ur.uid
WHERE ur.rid = %d";
$results = db_query($sql, $role_id);
while ($result = db_fetch_object($results)) {
$users[$result->uid] = $result->name;
}
return $users;
}