|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\User\Acl; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
// From Pimple |
|
8
|
|
|
use Pimple\Container; |
|
9
|
|
|
|
|
10
|
|
|
// From 'charcoal-translator' |
|
11
|
|
|
use Charcoal\Translator\TranslatorAwareTrait; |
|
12
|
|
|
|
|
13
|
|
|
// From 'charcoal-core' |
|
14
|
|
|
use Charcoal\Model\AbstractModel; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ACL Roles define hierarchical allowed and denied permissions. |
|
18
|
|
|
* |
|
19
|
|
|
* They can be attached to user accounts for fine-grained permission control. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Role extends AbstractModel |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
use TranslatorAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
const SEPARATOR = ','; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string|null $ident |
|
29
|
|
|
*/ |
|
30
|
|
|
public $ident; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The parent ACL role. |
|
34
|
|
|
* |
|
35
|
|
|
* This role will inherit all of its parent's permissions. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string $parent |
|
38
|
|
|
*/ |
|
39
|
|
|
public $parent; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The user-friendly name. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \Charcoal\Translator\Translation |
|
45
|
|
|
*/ |
|
46
|
|
|
public $name; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* List of explicitely allowed permissions. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string[]|null $allowed |
|
52
|
|
|
*/ |
|
53
|
|
|
public $allowed; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* List of explicitely denied permissions. |
|
57
|
|
|
* |
|
58
|
|
|
* @var string[]|null $denied |
|
59
|
|
|
*/ |
|
60
|
|
|
public $denied; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var boolean |
|
64
|
|
|
*/ |
|
65
|
|
|
private $superuser = false; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var integer |
|
69
|
|
|
*/ |
|
70
|
|
|
private $position; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* ACL Role can be used as a string (ident). |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __toString() |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->ident === null) { |
|
80
|
|
|
return ''; |
|
81
|
|
|
} |
|
82
|
|
|
return $this->ident; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function key() |
|
89
|
|
|
{ |
|
90
|
|
|
return 'ident'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string|Role $parent Role's parent. |
|
95
|
|
|
* @return self |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setParent($parent) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->parent = (string)$parent; |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getParent() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->parent; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string[]|string|null $allowed The allowed permissions for this role. |
|
113
|
|
|
* @throws InvalidArgumentException If the passed arguments is not an array, null, or a comma-separated string. |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
|
View Code Duplication |
public function setAllowed($allowed) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
if ($allowed === null) { |
|
119
|
|
|
$this->allowed = null; |
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (is_string($allowed)) { |
|
124
|
|
|
$allowed = explode(self::SEPARATOR, $allowed); |
|
125
|
|
|
$allowed = array_map('trim', $allowed); |
|
126
|
|
|
} |
|
127
|
|
|
if (!is_array($allowed)) { |
|
128
|
|
|
throw new InvalidArgumentException( |
|
129
|
|
|
'Invalid allowed value. Must be an array, null, or a comma-separated string.' |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
$this->allowed = $allowed; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string[]|null |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getAllowed() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->allowed; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string[]|string|null $denied The denied permissions for this role. |
|
146
|
|
|
* @throws InvalidArgumentException If the passed arguments is not an array, null, or a comma-separated string. |
|
147
|
|
|
* @return self |
|
148
|
|
|
*/ |
|
149
|
|
View Code Duplication |
public function setDenied($denied) |
|
|
|
|
|
|
150
|
|
|
{ |
|
151
|
|
|
if ($denied === null) { |
|
152
|
|
|
$this->denied = null; |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (is_string($denied)) { |
|
157
|
|
|
$denied = explode(self::SEPARATOR, $denied); |
|
158
|
|
|
$denied = array_map('trim', $denied); |
|
159
|
|
|
} |
|
160
|
|
|
if (!is_array($denied)) { |
|
161
|
|
|
throw new InvalidArgumentException( |
|
162
|
|
|
'Invalid denied value. Must be an array, null, or a comma-separated string.' |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
$this->denied = $denied; |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return string[]|null |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getDenied() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->denied; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param boolean $isSuper The superuser flag. |
|
179
|
|
|
* @return self |
|
180
|
|
|
*/ |
|
181
|
|
|
public function setSuperuser($isSuper) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->superuser = !!$isSuper; |
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return boolean |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getSuperuser() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->superuser; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param integer|string|null $position The role's ordering position. |
|
197
|
|
|
* @return self |
|
198
|
|
|
*/ |
|
199
|
|
|
public function setPosition($position) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->position = (int)$position; |
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return integer |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getPosition() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->position; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param Container $container Pimple DI container. |
|
215
|
|
|
* @return void |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function setDependencies(Container $container) |
|
218
|
|
|
{ |
|
219
|
|
|
parent::setDependencies($container); |
|
220
|
|
|
|
|
221
|
|
|
$this->setTranslator($container['translator']); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|