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