1
|
|
|
<?php |
2
|
|
|
namespace Ubiquity\security\acl\persistence; |
3
|
|
|
|
4
|
|
|
use Ubiquity\security\acl\models\AclElement; |
5
|
|
|
use Ubiquity\security\acl\models\Permission; |
6
|
|
|
use Ubiquity\security\acl\models\Resource; |
7
|
|
|
use Ubiquity\security\acl\models\Role; |
8
|
|
|
use Ubiquity\exceptions\AclException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Ubiquity\security\acl\persistence$AclArrayProvider |
12
|
|
|
* This class is part of Ubiquity |
13
|
|
|
* |
14
|
|
|
* @author jc |
15
|
|
|
* @version 1.0.0 |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
abstract class AclArrayProvider implements AclProviderInterface { |
19
|
|
|
|
20
|
|
|
protected $aclsArray; |
21
|
|
|
|
22
|
|
|
protected $parts; |
23
|
|
|
|
24
|
|
|
public function __construct() {} |
25
|
|
|
|
26
|
3 |
|
protected function loadAllPart($class): array { |
27
|
3 |
|
$elements = []; |
28
|
3 |
|
foreach ($this->parts[$class] as $partArray) { |
29
|
3 |
|
$elm = new $class(); |
30
|
3 |
|
$elm->fromArray($partArray); |
31
|
3 |
|
$elements[$partArray['name']] = $elm; |
32
|
|
|
} |
33
|
3 |
|
return $elements; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
* @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllAcls() |
40
|
|
|
*/ |
41
|
3 |
|
public function loadAllAcls(): array { |
42
|
3 |
|
$acls = []; |
43
|
3 |
|
foreach ($this->aclsArray as $aclArray) { |
44
|
3 |
|
$aclElement = new AclElement(); |
45
|
3 |
|
$aclElement->fromArray($aclArray); |
46
|
3 |
|
$acls[$aclElement->getId_()] = $aclElement; |
47
|
|
|
} |
48
|
3 |
|
return $acls; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function saveAcl(AclElement $aclElement) { |
52
|
1 |
|
$this->aclsArray[$aclElement->getId_()] = $aclElement->toArray(); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
public function removeAcl(AclElement $aclElement) { |
56
|
1 |
|
unset($this->aclsArray[$aclElement->getId_()]); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllPermissions() |
63
|
|
|
*/ |
64
|
3 |
|
public function loadAllPermissions(): array { |
65
|
3 |
|
return $this->loadAllPart(Permission::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllResources() |
72
|
|
|
*/ |
73
|
3 |
|
public function loadAllResources(): array { |
74
|
3 |
|
return $this->loadAllPart(Resource::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllRoles() |
81
|
|
|
*/ |
82
|
3 |
|
public function loadAllRoles(): array { |
83
|
3 |
|
return $this->loadAllPart(Role::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function savePart(\Ubiquity\security\acl\models\AbstractAclPart $part) { |
87
|
2 |
|
$class = \get_class($part); |
88
|
2 |
|
$this->parts[$class][$part->getName()] = $part->toArray(); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
1 |
|
public function updatePart(\Ubiquity\security\acl\models\AbstractAclPart $part) { |
92
|
1 |
|
$class = \get_class($part); |
93
|
1 |
|
$this->parts[$class][$part->getName()] = $part->toArray(); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
2 |
|
public function removePart(\Ubiquity\security\acl\models\AbstractAclPart $part) { |
97
|
2 |
|
$name = $part->getName(); |
98
|
2 |
|
if ($part instanceof Resource) { |
99
|
|
|
$field = 'resource'; |
100
|
2 |
|
} elseif ($part instanceof Role) { |
101
|
1 |
|
$field = 'role'; |
102
|
|
|
} else { |
103
|
1 |
|
$field = 'permission'; |
104
|
|
|
} |
105
|
2 |
|
foreach ($this->aclsArray as $acl) { |
106
|
2 |
|
if ($acl[$field]['name'] === $name) { |
107
|
|
|
throw new AclException("$name is in use in ACLs and can't be removed!"); |
108
|
|
|
} |
109
|
|
|
} |
110
|
2 |
|
unset($this->parts[\get_class($part)][$name]); |
111
|
2 |
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|