-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpermissions-manager.php
More file actions
508 lines (461 loc) · 19.8 KB
/
Copy pathpermissions-manager.php
File metadata and controls
508 lines (461 loc) · 19.8 KB
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
<?php
class ImageAnnotationPermissionsManager extends ImageAnnotationAbstractPermissionsManager
{
static protected $pluginName = 'ImageAnnotation';
static public function getInstance()
{
return ImageAnnotationAbstractPermissionsManager::getInstance(self::$pluginName);
}
/**
* Initializes the plugin permissions manager.
*
* @return void
*/
protected function init()
{
// set the display texts for each of the resources
$this->setResourceTexts(array(
'ImageAnnotation_Annotations' => 'Image Annotations'
));
// set the display texts for each permission of each resource
$this->setPermissionsTexts(array(
'ImageAnnotation_Annotations' => array(
'add' => 'Add Their Own Annotations',
'editSelf' => 'Edit Their Own Annotations',
'editAll' => 'Edit The Annotations Of Other Users',
'deleteSelf' => 'Delete Their Own Annotations',
'deleteAll' => 'Delete The Annotations Of Other Users',
'showPublic' => 'View Public Annotations',
'showNotPublic' => 'View Private Annotations'
)
));
// set whether the default permissions for a role can be editted
$this->setDefaultPermissionIsEditableForRole('anyone',
'ImageAnnotation_Annotations',
array('add',
'editSelf',
'editAll',
'deleteSelf',
'deleteAll'),
false);
// set the default permissions for each role
$this->setRoleHasPermissionByDefault(array('super', 'admin'),
'ImageAnnotation_Annotations',
$this->getPermissionsForResource('ImageAnnotation_Annotations'),
true);
$this->setRoleHasPermissionByDefault(array('contributor', 'guest'),
'ImageAnnotation_Annotations',
array('add', 'editSelf', 'deleteSelf', 'showPublic'),
true);
$this->setRoleHasPermissionByDefault(array('researcher'),
'ImageAnnotation_Annotations',
array('add', 'editSelf', 'deleteSelf', 'showPublic', 'showNotPublic'),
true);
$this->setRoleHasPermissionByDefault(array('anyone'),
'ImageAnnotation_Annotations',
array('showPublic'),
true);
}
}
class ImageAnnotationAbstractPermissionsManager
{
static private $pInstances;
protected $resourceNamesToTexts;
protected $resourceNamesToPermissionsToTexts;
protected $canEditDefaultPermission;
protected $hasPermissionByDefault;
protected $pluginPrefix;
/**
* Returns the singleton PluginPermissionsManager for the plugin name.
* Assumes that the name of the subclass of PluginPermissionsManger
* is named: camelcased version of $pluginName plus 'PermissionsManager'
*
* @return PluginPermissionsManager The singleton PluginPermissionsManager
*/
public static function getInstance($pluginName)
{
$pluginName = trim($pluginName);
if (empty($pluginName)) {
return null;
}
if (!self::$pInstances[$pluginName]) {
$inflector = new Inflector;
$pluginManagerClassName = $inflector->camelize($pluginName) . 'PermissionsManager';
$pManager = new $pluginManagerClassName;
$pManager->initPluginPrefix($pluginName);
$pManager->init();
self::$pInstances[$pluginName] = $pManager;
}
return self::$pInstances[$pluginName];
}
public function initPluginPrefix($pluginName)
{
$inflector = new Inflector;
$this->pluginPrefix = $inflector->tableize($pluginName);
}
/**
* Returns the permissions for a resource.
*
* @param string $resourceName The name of the resource
* @return array An array of strings containing the permission names
*/
public function getPermissionsForResource($resourceName)
{
$permissionsToTexts = $this->resourceNamesToPermissionsToTexts[$resourceName];
return array_keys($permissionsToTexts);
}
/**
* Sets the resource names and their corresponding display texts
*
* @return void
*/
public function setResourceTexts($resourceNamesToTexts)
{
$this->resourceNamesToTexts = $resourceNamesToTexts;
}
/**
* For each resource, sets the permission names and their corresponding display texts
*
* @return void
*/
public function setPermissionsTexts($resourceNamesToPermissionsToTexts)
{
$this->resourceNamesToPermissionsToTexts = $resourceNamesToPermissionsToTexts;
}
/**
* Loads the plugin resources and permissions for various roles based on plugin options
*
* @param Omeka_Acl $acl
* @return void
*/
public function defineAcl($acl)
{
// load the plugin resources
$resourceNames = $this->getResourceNames();
$resources = array();
foreach($resourceNames as $resourceName) {
$resources[$resourceName] = $this->getPermissionsForResource($resourceName);
}
$acl->loadResourceList($resources);
// Clear out permissions for the admin role so they can be customized.
$acl->deny('admin', $resourceNames);
// load the permissions based on the plugin options
$roleNames = $this->getRoleNames($acl);
foreach($resources as $resourceName => $permissions) {
$allowList = array();
foreach ($roleNames as $roleName) {
$permissionsForRole = array();
foreach ($permissions as $permission) {
$optionName = $this->getPermissionOptionName($resourceName, $permission, $roleName);
if (get_option($optionName) == '1') {
$permissionsForRole[] = $permission;
}
}
// convert 'anyone' role name to null, so that the resource applies to anyone
if ($roleName == 'anyone') {
$roleName = null;
}
if (count($permissionsForRole) > 0) {
$allowList[] = array($roleName, $resourceName, $permissionsForRole);
}
}
$acl->loadAllowList($allowList);
}
}
/**
* Returns whether a plugin permission for a specific resource and roleName
* can be changed from its default. If false, then no one, not even the super user,
* should change the permission for the roleName. If it is not specified whether the
* the a default permission can be editted, then it returns true.
*
* @param string $roleName The role name to be tested
* @param string $resourceName The name of the resource to be tested
* @param string $permission The permission to be tested
* @return boolean Whether or not the whether a plugin permission for a specific resource and roleName
* can be changed from its default.
*/
public function getDefaultPermissionIsEditableForRole($roleName, $resourceName, $permission)
{
return (!isset($this->canEditDefaultPermission[$roleName][$resourceName][$permission]) ||
$this->canEditDefaultPermission[$roleName][$resourceName][$permission]);
}
/**
* Sets whether a plugin permission for a specific resource and roleName
* can be changed from its default. If false, then no one, not even the super user,
* should change the permission for the roleName. If it is not specified whether the
* the a default permission can be editted, then it returns true.
*
* @param string|array $roleName The role name to be tested
* @param string|array $resourceName The name of the resource to be tested
* @param string|array $permission The permission to be tested
* @param boolean $canEdit Whether or not the whether a plugin permission for a specific resource and roleName
* can be changed from its default.
* @return void
*/
public function setDefaultPermissionIsEditableForRole($roleName, $resourceName, $permission, $canEdit)
{
$roleNames = $this->varToArray($roleName);
$resourceNames = $this->varToArray($resourceName);
$permissions = $this->varToArray($permission);
foreach($roleNames as $roleName) {
foreach($resourceNames as $resourceName) {
foreach($permissions as $permission) {
$this->canEditDefaultPermission[$roleName][$resourceName][$permission] = $canEdit;
}
}
}
}
/**
* Returns whether or not a role has permission to a resource by default,
* prior to user specified options. If the default permission is not specified, then
* it returns false.
*
* @param string $roleName The name of the role
* @param string $resourceName The name of the resource
* @param string $permission The name of the permission
* @return boolean Whether or not a role has permission to a resource by default,
* prior to user specified options.
*/
public function getRoleHasPermissionByDefault($roleName, $resourceName, $permission)
{
return (isset($this->hasPermissionByDefault[$roleName][$resourceName][$permission]) &&
$this->hasPermissionByDefault[$roleName][$resourceName][$permission]);
}
/**
* Sets whether or not a role has permission to a resource by default,
* prior to user specified options. If the default permission is not specified, then
* it returns false.
*
* @param string $roleName The name of the role
* @param string $resourceName The name of the resource
* @param string $permission The name of the permission
* @param boolean $hasByDefault Whether or not a role has permission to a resource by default,
* prior to user specified options.
*/
public function setRoleHasPermissionByDefault($roleName, $resourceName, $permission, $hasByDefault)
{
$roleNames = $this->varToArray($roleName);
$resourceNames = $this->varToArray($resourceName);
$permissions = $this->varToArray($permission);
foreach($roleNames as $roleName) {
foreach($resourceNames as $resourceName) {
foreach($permissions as $permission) {
$this->hasPermissionByDefault[$roleName][$resourceName][$permission] = $hasByDefault;
}
}
}
}
/**
* Returns the resource text to display on the configuration page for a resourceName
*
* @param string $resourceName The name of the resource
* @return string The resource text to display on the configuration page for a resourceName
*/
public function getResourceText($resourceName)
{
if (array_key_exists($resourceName, $this->resourceNamesToTexts)) {
return $this->resourceNamesToTexts[$resourceName];
}
return '';
}
/**
* Returns the permission text to display on the configuration page for a permission
*
* @param string $permission The name of the permission
* @return string The permission text to display on the configuration page for a permission
*/
public function getPermissionText($resourceName, $permission)
{
if (array_key_exists($resourceName, $this->resourceNamesToPermissionsToTexts) &&
array_key_exists($permission, $this->resourceNamesToPermissionsToTexts[$resourceName]))
{
return $this->resourceNamesToPermissionsToTexts[$resourceName][$permission];
}
return '';
}
/**
* Returns an array of all of the resource names for the plugin.
*
* @return array The resource names for the plugin
*/
public function getResourceNames()
{
return array_keys($this->resourceNamesToTexts);
}
/**
* Returns an array of all of the role names for the plugin.
* For the purposes of saving permissions as options,
* it adds an 'anyone' role for any user, although this role is not saved in the database.
*
* @param Omeka_Acl $acl
* @return array The role names for the plugin
*/
public function getRoleNames($acl=null)
{
if (!$acl) {
$acl = get_acl();
}
// return all role names except the super user and 'anyone'
$roleNames = array_diff($acl->getRoleNames(), array('super', 'anyone'));
// add the role name 'anyone' for anyone on the site
$roleNames[] = 'anyone';
// sort the role names alphabetically
// sort($roleNames);
return $roleNames;
}
/**
* Sets the default permissions as options in the database.
*
* @return void
*/
public function setDefaultPermissionOptions()
{
$roleNames = $this->getRoleNames();
$resourceNames = $this->getResourceNames();
foreach($resourceNames as $resourceName) {
$permissions = $this->getPermissionsForResource($resourceName);
foreach ($roleNames as $roleName) {
foreach ($permissions as $permission) {
$hasPermission = $this->getRoleHasPermissionByDefault($roleName, $resourceName, $permission) ? '1' : '0';
$optionName = $this->getPermissionOptionName($resourceName, $permission, $roleName);
set_option($optionName, $hasPermission);
}
}
}
}
/**
* Deletes all the permission options from the database.
*
* @return void
*/
public function deletePermissionOptions()
{
$roleNames = $this->getRoleNames();
$resourceNames = $this->getResourceNames();
foreach($resourceNames as $resourceName) {
$permissions = $this->getPermissionsForResource($resourceName);
foreach ($roleNames as $roleName) {
foreach ($permissions as $permission) {
$optionName = $this->getPermissionOptionName($resourceName, $permission, $roleName);
delete_option($optionName);
}
}
}
}
/**
* Returns the HTML code for the permissions configuration table to put on the plugin configuration page.
*
* @return string The HTML code for the permissions configuration table.
*/
public function getPermissionsConfigTable()
{
$html = '';
// start the permissions table
$html .= '<table>';
// build the table header
$html .= '<tr>';
$html .= '<th>Resource</th>';
$html .= '<th>Permission</th>';
$roleNames = $this->getRoleNames();
foreach($roleNames as $roleName) {
$html .= '<th>' . html_escape($roleName) . '</th>';
}
$html .= '<tr>';
$resourceNames = $this->getResourceNames();
foreach($resourceNames as $resourceName) {
$permissions = $this->getPermissionsForResource($resourceName);
$isNewResource = true;
foreach($permissions as $permission) {
// build a row to specify whether each role has a permission for a resource
$html .= '<tr>';
if ($isNewResource) {
$html .= '<td>' . html_escape($this->getResourceText($resourceName)) . '</td>';
$isNewResource = false;
} else {
$html .= '<td></td>';
}
$permissionText = $this->getPermissionText($resourceName, $permission);
$html .= '<td>' . html_escape($permissionText) . '</td>';
foreach ($roleNames as $roleName) {
$cbName = $this->pluginPrefix . '_' . $permission . '[' . $roleName . ']';
$cbId = $this->getPermissionOptionName($resourceName, $permission, $roleName);
$isChecked = (get_option($cbId) == '1') ? true : false;
$isDisabled = !$this->getDefaultPermissionIsEditableForRole($roleName, $resourceName, $permission);
$cbParams = array('name'=>$cbName, 'id'=>$cbId);
if ($isDisabled) {
$cbParams['disabled'] = 'disabled';
}
$html .= '<td>' . checkbox($cbParams, $isChecked, '1') . '</td>';
}
}
}
// close the permissions table
$html .= '</table>';
return $html;
}
/**
* Saves the permissions in configuration table on the plugin configuration page
* as options in the database.
*
* @return void
*/
public function savePermissionsConfigTable()
{
$roleNames = $this->getRoleNames();
$resourceNames = $this->getResourceNames();
foreach($resourceNames as $resourceName) {
$permissions = $this->getPermissionsForResource($resourceName);
foreach ($roleNames as $roleName) {
foreach ($permissions as $permission) {
$cbName = $this->pluginPrefix . '_' . $permission;
$optionName = $this->getPermissionOptionName($resourceName, $permission, $roleName);
if ($_POST[$cbName][$roleName] == '1') {
set_option($optionName, '1');
} else {
set_option($optionName, '0');
}
}
}
}
}
/**
* Returns the name of the option that specifies whether a role has permission to a resource
*
* @param string $resourceName The name of the resource
* @param string $permission The name of the permission
* @param string $roleName The name of the role
* @return string The name of the option that specifies whether a role has a permission to a resource
*/
public function getPermissionOptionName($resourceName, $permission, $roleName)
{
return $this->pluginPrefix . '_' . strtolower($resourceName) . '_' . $permission . '_' . $roleName;
}
/**
* Returns whether the current user has permission to access a resource
*
* @param string $resourceName The name of the resource
* @param string $permission The name of the permission
* @return bool Whether the current user has permission to access a resource
*/
public function currentUserHasPermission($resourceName, $permission)
{
return get_acl()->checkUserPermission($resourceName, $permission);
}
/**
* If the variable is not an array,
* then it returns an an array that contains that variable,
* else it returns the variable.
*
* @param mixed $v
* @return array
*/
private function varToArray($v)
{
if (!is_array($v)) {
$a = array($v);
} else {
$a = $v;
}
return $a;
}
}