This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.AJXP_Role.php
579 lines (520 loc) · 18.3 KB
/
class.AJXP_Role.php
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
<?php
/*
* Copyright 2007-2012 Charles du Jeu <contact (at) cdujeu.me>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
defined('AJXP_EXEC') or die('Access not allowed');
define('AJXP_VALUE_CLEAR', "AJXP_VALUE_CLEAR");
define('AJXP_REPO_SCOPE_ALL',"AJXP_REPO_SCOPE_ALL");
define('AJXP_REPO_SCOPE_SHARED',"AJXP_REPO_SCOPE_SHARED");
define('AJXP_PLUGINS_SCOPE_ALL',"plugin_all");
/**
* @package Pydio
* @subpackage Core
*/
class AJXP_Role implements AjxpGroupPathProvider
{
/**
* @var String path of this role, default is root path
*/
protected $groupPath;
/**
* @var String Role identifier
*/
protected $roleId;
/**
* @var array List of access rights for each workspaces (wsId => "r", "w", "rw", "d")
*/
protected $acls = array();
/**
* @var array List of plugins parameters values, (SCOPE => PLUGIN NAME => PARAM NAME => value)
*/
protected $parameters = array();
/**
* @var array List of plugin actions that can be disabled/enabled (SCOPE => PLUGIN NAME => ACTION NAME => status)
*/
protected $actions = array();
/**
* @var array Automatically applies to a given list of profiles
*/
protected $autoApplies = array();
/**
* @var AJXP_PermissionMask[]
*/
protected $masks = array();
/**
* @var integer
*/
protected $lastUpdated = 0;
static $cypheredPassPrefix = '$pydio_password$';
public function __construct($id)
{
$this->roleId = $id;
}
public function migrateDeprectated($repositoriesList, AjxpRole $oldRole)
{
$repositoriesList["ajxp.all"] = "";
foreach ($repositoriesList as $repoId => $repoObject) {
$right = $oldRole->getRight($repoId);
if(!empty($right)) $this->setAcl($repoId, $right);
$actions = $oldRole->getSpecificActionsRights($repoId);
if (count($actions)) {
foreach ($actions as $act => $status) {
if ($repoId == "ajxp.all") {
$this->setActionState(AJXP_PLUGINS_SCOPE_ALL, $act, AJXP_REPO_SCOPE_ALL, $status);
} else {
$this->setActionState(AJXP_PLUGINS_SCOPE_ALL, $act, $repoId, $status);
}
}
}
}
$this->setGroupPath($oldRole->getGroupPath());
if ($oldRole->isDefault()) {
$this->setAutoApplies(array("all"));
}
}
public function isGroupRole()
{
return strpos($this->roleId, "AJXP_GRP_") === 0;
}
public function isUserRole()
{
return strpos($this->roleId, "AJXP_USER_") === 0;
}
/**
* Whether this role can read the given repo
* @param string $repositoryId Repository ID
* @return bool
*/
public function canRead($repositoryId)
{
$right = $this->getAcl($repositoryId);
if($right == "rw" || $right == "r") return true;
return false;
}
/**
* Whether this role can write the given repo
* @param string $repositoryId Repository ID
* @return bool
*/
public function canWrite($repositoryId)
{
$right = $this->getAcl($repositoryId);
if($right == "rw" || $right == "w") return true;
return false;
}
/**
* @param string $repositoryId
* @param string $rightString
* @return void
*/
public function setAcl($repositoryId, $rightString)
{
if (empty($rightString)) {
if(isSet($this->acls[$repositoryId])) unset($this->acls[$repositoryId]);
} else {
$this->acls[$repositoryId] = $rightString;
}
return;
}
/**
* @param string $repositoryId
* @return string
*/
public function getAcl($repositoryId)
{
if (isSet($this->acls[$repositoryId])) {
return $this->acls[$repositoryId];
}
return "";
}
/**
* @return array Associative array[REPO_ID] => RIGHT_STRING (r / w / rw / AJXP_VALUE_CLEAR)
*/
public function listAcls()
{
return $this->acls;
}
public function clearAcls()
{
$this->acls = array();
}
/**
* @param String $repositoryId
* @param AJXP_PermissionMask $mask
*/
public function setMask($repositoryId, $mask){
$this->masks[$repositoryId] = $mask;
}
/**
* @param string $repositoryId
*/
public function clearMask($repositoryId){
if(isSet($this->masks[$repositoryId])){
unset($this->masks[$repositoryId]);
}
}
/**
* @param string $repositoryId
* @return bool
*/
public function hasMask($repositoryId){
return isSet($this->masks[$repositoryId]);
}
/**
* @param $repositoryId
* @return AJXP_PermissionMask|null
*/
public function getMask($repositoryId){
return (isSet($this->masks[$repositoryId]) ? $this->masks[$repositoryId] : null);
}
/**
* @return AJXP_PermissionMask[]
*/
public function listMasks(){
return $this->masks;
}
/**
* Send all role informations as an associative array
* @param bool $blurPasswords
* @return array
*/
public function getDataArray($blurPasswords = false)
{
$roleData = array();
$roleData["ACL"] = $this->listAcls();
$roleData["MASKS"] = $this->listMasks();
$roleData["ACTIONS"] = $this->listActionsStates();
$roleData["PARAMETERS"] = $this->listParameters(false, $blurPasswords);
$roleData["APPLIES"] = $this->listAutoApplies();
return $roleData;
}
/**
* Update the role information from an associative array
* @see getDataArray()
* @param array $roleData
*/
public function bunchUpdate($roleData)
{
$this->acls = $roleData["ACL"];
$this->actions = $roleData["ACTIONS"];
$this->parameters = $roleData["PARAMETERS"];
$this->autoApplies = $roleData["APPLIES"];
if(isSet($roleData["MASKS"])){
$this->masks = $roleData["MASKS"];
}
}
/**
* @param string $pluginId
* @param string $parameterName
* @param mixed $parameterValue can be AJXP_VALUE_CLEAR (force clear previous), or empty string for clearing value (apply previous).
* @param string|null $repositoryId
*/
public function setParameterValue($pluginId, $parameterName, $parameterValue, $repositoryId = null)
{
if($repositoryId === null) $repositoryId = AJXP_REPO_SCOPE_ALL;
if (empty($parameterValue) && $parameterValue !== false && $parameterValue !== "0") {
if (isSet($this->parameters[$repositoryId][$pluginId][$parameterName])) {
unset($this->parameters[$repositoryId][$pluginId][$parameterName]);
if(!count($this->parameters[$repositoryId][$pluginId])) unset($this->parameters[$repositoryId][$pluginId]);
if(!count($this->parameters[$repositoryId])) unset($this->parameters[$repositoryId]);
}
} else {
$this->parameters = $this->setArrayValue($this->parameters, $repositoryId, $pluginId, $parameterName, $parameterValue);
}
return;
}
/**
* @param string $pluginId
* @param string $parameterName
* @param string $repositoryId
* @param mixed $parameterValue
* @return mixed
*/
public function filterParameterValue($pluginId, $parameterName, $repositoryId, $parameterValue)
{
if (isSet($this->parameters[$repositoryId][$pluginId][$parameterName])) {
$v = $this->parameters[$repositoryId][$pluginId][$parameterName];
if($v === AJXP_VALUE_CLEAR) return "";
else return $this->filterCypheredPasswordValue($v);
}
if (isSet($this->parameters[AJXP_REPO_SCOPE_ALL][$pluginId][$parameterName])) {
$v = $this->parameters[AJXP_REPO_SCOPE_ALL][$pluginId][$parameterName];
if($v === AJXP_VALUE_CLEAR) return "";
else return $this->filterCypheredPasswordValue($v);
}
return $parameterValue;
}
/**
* @param bool $preserveCypheredPasswords
* @param bool $blurCypheredPasswords
* @return array Associative array of parameters : array[REPO_ID][PLUGIN_ID][PARAMETER_NAME] = PARAMETER_VALUE
*/
public function listParameters($preserveCypheredPasswords = false, $blurCypheredPasswords = false)
{
if($preserveCypheredPasswords) return $this->parameters;
$copy = $this->parameters;
foreach($copy as $repo => &$plugs){
foreach($plugs as $plugName => &$plugData){
foreach($plugData as $paramName => &$paramValue){
$testValue = $this->filterCypheredPasswordValue($paramValue);
if($testValue != $paramValue){
if($blurCypheredPasswords) $paramValue = "__AJXP_VALUE_SET__";
else $paramValue = $testValue;
}
}
}
}
return $copy;
}
public function listAutoApplies()
{
return $this->autoApplies;
}
/**
* @param String $value
* @return String
*/
private function filterCypheredPasswordValue($value){
if(is_string($value) && strpos($value, self::$cypheredPassPrefix) === 0) return str_replace(self::$cypheredPassPrefix, "", $value);
return $value;
}
/**
* @param string $pluginId
* @param string $actionName
* @param string|null $repositoryId
* @param string $state
*/
public function setActionState($pluginId, $actionName, $repositoryId = null, $state = "disabled")
{
$this->actions = $this->setArrayValue($this->actions, $repositoryId, $pluginId, $actionName, $state);
return;
}
public function listActionsStates()
{
return $this->actions;
}
/**
* @param Repository $repository
* @return array
*/
public function listActionsStatesFor($repository)
{
$actions = array();
if (isSet($this->actions[AJXP_REPO_SCOPE_ALL])) {
$actions = $this->actions[AJXP_REPO_SCOPE_ALL];
}
if ($repository != null && isSet($this->actions[AJXP_REPO_SCOPE_SHARED]) && $repository->hasParent()) {
$actions = array_merge($actions, $this->actions[AJXP_REPO_SCOPE_SHARED]);
}
if ($repository != null && isSet($this->actions[$repository->getId()])) {
$actions = array_merge($actions, $this->actions[$repository->getId()]);
}
return $actions;
}
/**
* @param string $pluginId
* @param string $actionName
* @param string $repositoryId
* @param boolean $inputState
* @return boolean
*/
public function actionEnabled($pluginId, $actionName, $repositoryId, $inputState)
{
if (isSet($this->actions[AJXP_REPO_SCOPE_ALL][$pluginId][$actionName])) {
return $this->actions[AJXP_REPO_SCOPE_ALL][$pluginId][$actionName] == "enabled" ? true : false ;
}
if (isSet($this->actions[$repositoryId][$pluginId][$actionName])) {
return $this->actions[$repositoryId][$pluginId][$actionName] == "enabled" ? true : false ;
}
return $inputState;
}
/**
* @return array
*/
public function listAllActionsStates()
{
return $this->actions;
}
/**
* @param AJXP_Role $role
* @return AJXP_Role
*/
public function override(AJXP_Role $role)
{
$newRole = new AJXP_Role($role->getId());
$roleAcl = $role->listAcls();
$newAcls = $this->array_merge_recursive2($roleAcl, $this->listAcls());
foreach ($newAcls as $repoId => $rightString) {
//if($rightString == AJXP_VALUE_CLEAR) continue;
if(empty($rightString) && !empty($roleAcl[$repoId])){
$rightString = $roleAcl[$repoId];
}
$newRole->setAcl($repoId, $rightString);
}
$roleParameters = $role->listParameters(true);
$newParams = $this->array_merge_recursive2($roleParameters, $this->listParameters(true));
foreach ($newParams as $repoId => $data) {
foreach ($data as $pluginId => $param) {
foreach ($param as $parameterName => $parameterValue) {
if ($parameterValue === true || $parameterValue === false) {
$newRole->setParameterValue($pluginId, $parameterName, $parameterValue, $repoId);
continue;
}
if($parameterValue == AJXP_VALUE_CLEAR) continue;
if($parameterValue === "" && !empty($roleParameters[$repoId][$pluginId][$parameterName])){
$parameterValue = $newParams[$repoId][$pluginId][$parameterName];
}
$newRole->setParameterValue($pluginId, $parameterName, $parameterValue, $repoId);
}
}
}
$newActions = $this->array_merge_recursive2($role->listActionsStates(), $this->listActionsStates());
foreach ($newActions as $repoId => $data) {
foreach ($data as $pluginId => $action) {
foreach ($action as $actionName => $actionState) {
$newRole->setActionState($pluginId, $actionName, $repoId, $actionState);
}
}
}
$roleMasks = $role->listMasks();
$allKeys = array_merge(array_keys($this->masks), array_keys($roleMasks));
foreach($allKeys as $repoId){
if(isSet($roleMasks[$repoId]) && isSet($this->masks[$repoId])){
$newRole->setMask($repoId, $roleMasks[$repoId]->override($this->masks[$repoId]));
}else if(isSet($roleMasks[$repoId])){
$newRole->setMask($repoId, $roleMasks[$repoId]);
}else{
$newRole->setMask($repoId, $this->masks[$repoId]);
}
}
return $newRole;
}
/**
* @param array
* @param key1
* @param key2
* @param key3...
* @param value
*/
public function setArrayValue()
{
$args = func_get_args();
$arr = $args[0]; //array_shift($args);
$argMaxIndex = count($args)-1;
$value = $args[$argMaxIndex]; //array_pop($args);
$current = &$arr;
foreach ($args as $index => $key) {
if($index == 0) continue;
if ($index < $argMaxIndex -1) {
if(!isset($current[$key])) $current[$key] = array();
$current = &$current[$key];
} else {
$current[$key] = $value;
break;
}
}
return $arr;
}
/**
* @param array $array1
* @param array $array2
* @return array
*/
public function array_merge_recursive2($array1, $array2)
{
$arrays = func_get_args();
$narrays = count($arrays);
// check arguments
// comment out if more performance is necessary (in this case the foreach loop will trigger a warning if the argument is not an array)
for ($i = 0; $i < $narrays; $i ++) {
if (!is_array($arrays[$i])) {
// also array_merge_recursive returns nothing in this case
trigger_error('Argument #' . ($i+1) . ' is not an array - trying to merge array with scalar! Returning null!', E_USER_WARNING);
return null;
}
}
// the first array is in the output set in every case
$ret = $arrays[0];
// merege $ret with the remaining arrays
for ($i = 1; $i < $narrays; $i ++) {
foreach ($arrays[$i] as $key => $value) {
//if (((string) $key) === ((string) intval($key))) { // integer or string as integer key - append
// $ret[] = $value;
//}
//{ // string key - megre
if (is_array($value) && isset($ret[$key])) {
// if $ret[$key] is not an array you try to merge an scalar value with an array - the result is not defined (incompatible arrays)
// in this case the call will trigger an E_USER_WARNING and the $ret[$key] will be null.
$ret[$key] = $this->array_merge_recursive2($ret[$key], $value);
} else {
$ret[$key] = $value;
}
// }
}
}
return $ret;
}
public function setGroupPath($groupPath)
{
$this->groupPath = $groupPath;
}
public function getGroupPath()
{
return $this->groupPath;
}
public function getId()
{
return $this->roleId;
}
public function setLabel($roleLabel)
{
$this->setParameterValue("core.conf", "ROLE_DISPLAY_NAME", $roleLabel);
}
public function getLabel()
{
$test = $this->filterParameterValue("core.conf", "ROLE_DISPLAY_NAME", AJXP_REPO_SCOPE_ALL, $this->roleId);
if(!empty($test)) return $test;
return $this->roleId;
}
public function alwaysOverrides()
{
return $this->filterParameterValue("core.conf", "ROLE_FORCE_OVERRIDE", AJXP_REPO_SCOPE_ALL, false);
}
/**
* @param array $specificRights
*/
public function setAutoApplies($specificRights)
{
$this->autoApplies = $specificRights;
}
/**
* @param string $specificRight
* @return boolean
*/
public function autoAppliesTo($specificRight)
{
return in_array($specificRight, $this->autoApplies);
}
public function getLastUpdated(){
return $this->lastUpdated;
}
public function setLastUpdated($time){
$this->lastUpdated = $time;
}
}