1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tleckie\Acl\Operation; |
4
|
|
|
|
5
|
|
|
use Tleckie\Acl\Acl\OperationEnum; |
6
|
|
|
use Tleckie\Acl\Acl\PermissionTypeEnum; |
7
|
|
|
use Tleckie\Acl\Resource\ResourceInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AddChain |
11
|
|
|
* |
12
|
|
|
* @package Tleckie\Acl\Operation |
13
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class AddChain implements OperationChainInterface |
16
|
|
|
{ |
17
|
|
|
/** @var OperationChainInterface|null */ |
18
|
|
|
private OperationChainInterface|null $handler; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AddChain constructor. |
22
|
|
|
* |
23
|
|
|
* @param OperationChainInterface|null $handler |
24
|
|
|
*/ |
25
|
|
|
public function __construct(?OperationChainInterface $handler = null) |
26
|
|
|
{ |
27
|
|
|
$this->handler = $handler; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function next(OperationChainInterface $handler): OperationChainInterface |
34
|
|
|
{ |
35
|
|
|
$this->handler = $handler; |
36
|
|
|
|
37
|
|
|
return $handler; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function handle( |
44
|
|
|
OperationEnum $operation, |
45
|
|
|
PermissionTypeEnum $type, |
46
|
|
|
array $roles, |
47
|
|
|
array $resources, |
48
|
|
|
array $privileges, |
49
|
|
|
array &$rules |
50
|
|
|
): OperationChainInterface { |
51
|
|
|
if ($operation === $operation::ADD()) { |
52
|
|
|
$this->handleRoles($roles, $resources, $rules, $type, $privileges); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($this->handler) { |
56
|
|
|
$this->handler->handle( |
57
|
|
|
$operation, |
58
|
|
|
$type, |
59
|
|
|
$roles, |
60
|
|
|
$resources, |
61
|
|
|
$privileges, |
62
|
|
|
$rules |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $roles |
71
|
|
|
* @param array $resources |
72
|
|
|
* @param array $rules |
73
|
|
|
* @param PermissionTypeEnum $type |
74
|
|
|
* @param array $privileges |
75
|
|
|
*/ |
76
|
|
|
private function handleRoles( |
77
|
|
|
array $roles, |
78
|
|
|
array $resources, |
79
|
|
|
array &$rules, |
80
|
|
|
PermissionTypeEnum $type, |
81
|
|
|
array $privileges |
82
|
|
|
): void { |
83
|
|
|
foreach ($roles as $role) { |
84
|
|
|
$this->handleResources($resources, $rules, $role, $type, $privileges); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $resources |
90
|
|
|
* @param array $rules |
91
|
|
|
* @param string $role |
92
|
|
|
* @param PermissionTypeEnum $type |
93
|
|
|
* @param array $privileges |
94
|
|
|
*/ |
95
|
|
|
private function handleResources( |
96
|
|
|
array $resources, |
97
|
|
|
array &$rules, |
98
|
|
|
string $role, |
99
|
|
|
PermissionTypeEnum $type, |
100
|
|
|
array $privileges |
101
|
|
|
): void { |
102
|
|
|
$typeId = (string)$type; |
103
|
|
|
$items = &$rules['roles']; |
104
|
|
|
|
105
|
|
|
foreach ($resources as $resource) { |
106
|
|
|
$resourceId = (string)$resource; |
107
|
|
|
|
108
|
|
|
if (!isset($items[$role][$typeId][$resourceId])) { |
109
|
|
|
$items[$role][$typeId][$resourceId] = []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->handleAllow($type, $resource, $rules, $role, $privileges, $privilege); |
113
|
|
|
|
114
|
|
|
foreach ($privileges as $privilege) { |
115
|
|
|
$items[$role][$typeId][$resourceId][$privilege] = $typeId; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param PermissionTypeEnum $type |
122
|
|
|
* @param ResourceInterface $resource |
123
|
|
|
* @param array $rules |
124
|
|
|
* @param string $role |
125
|
|
|
* @param array $privileges |
126
|
|
|
* @param $privilege |
127
|
|
|
*/ |
128
|
|
|
private function handleAllow( |
129
|
|
|
PermissionTypeEnum $type, |
130
|
|
|
ResourceInterface $resource, |
131
|
|
|
array &$rules, |
132
|
|
|
string $role, |
133
|
|
|
array $privileges, |
134
|
|
|
&$privilege |
135
|
|
|
): void { |
136
|
|
|
$typeId = (string)$type; |
137
|
|
|
$items = &$rules['roles']; |
138
|
|
|
|
139
|
|
|
foreach ($resource->children() as $res) { |
140
|
|
|
if (!isset($items[$role][$typeId][(string)$res])) { |
141
|
|
|
$items[$role][$typeId][(string)$res] = []; |
142
|
|
|
} |
143
|
|
|
foreach ($privileges as $privilege) { |
144
|
|
|
$items[$role][$typeId][(string)$res][$privilege] = $typeId; |
145
|
|
|
} |
146
|
|
|
$this->handleAllow($type, $res, $rules, $role, $privileges, $privilege); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|