|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of tenside/core-bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Christian Schiffler <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* This project is provided in good faith and hope to be usable by anyone. |
|
12
|
|
|
* |
|
13
|
|
|
* @package tenside/core-bundle |
|
14
|
|
|
* @author Christian Schiffler <[email protected]> |
|
15
|
|
|
* @copyright 2015 Christian Schiffler <[email protected]> |
|
16
|
|
|
* @license https://github.com/tenside/core-bundle/blob/master/LICENSE MIT |
|
17
|
|
|
* @link https://github.com/tenside/core-bundle |
|
18
|
|
|
* @filesource |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Tenside\CoreBundle\Security; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A user information.. |
|
25
|
|
|
*/ |
|
26
|
|
|
class UserInformation implements UserInformationInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The contained data. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $data = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The password salt. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $salt; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Map for translating roles to array of strings and vice versa. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $roleMap = [ |
|
48
|
|
|
UserInformationInterface::ROLE_NONE => 'ROLE_NONE', |
|
49
|
|
|
UserInformationInterface::ROLE_UPGRADE => 'ROLE_UPGRADE', |
|
50
|
|
|
UserInformationInterface::ROLE_MANIPULATE_REQUIREMENTS => 'ROLE_MANIPULATE_REQUIREMENTS', |
|
51
|
|
|
UserInformationInterface::ROLE_EDIT_COMPOSER_JSON => 'ROLE_EDIT_COMPOSER_JSON', |
|
52
|
|
|
UserInformationInterface::ROLE_EDIT_APPKERNEL => 'ROLE_EDIT_APP_KERNEL', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $data An array of values. |
|
59
|
|
|
* |
|
60
|
|
|
* @api |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(array $data = []) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->salt = uniqid('', true); |
|
65
|
|
|
$this->setAccessLevel(0); |
|
66
|
|
|
foreach ($data as $key => $value) { |
|
67
|
|
|
$this->set($key, $value); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
* |
|
74
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseVariableName) |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getRoles() |
|
77
|
|
|
{ |
|
78
|
|
|
$user = $this; |
|
79
|
|
|
$comparator = function ($level) use ($user) { |
|
80
|
|
|
return $user->hasAccessLevel($level); |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
// Fallback for PHP < 5.6 which do not support ARRAY_FILTER_USE_KEY. |
|
84
|
|
|
if (version_compare(PHP_VERSION, '5.6', '<')) { |
|
85
|
|
|
$filtered = array_flip(array_filter(array_keys(self::$roleMap), $comparator)); |
|
86
|
|
|
return array_values(array_intersect_key(self::$roleMap, $filtered)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return array_values(array_filter(self::$roleMap, $comparator, ARRAY_FILTER_USE_KEY)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getPassword() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->get('password'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets the password for this user instance. |
|
102
|
|
|
* |
|
103
|
|
|
* NOTE: the password must be encrypted. |
|
104
|
|
|
* |
|
105
|
|
|
* Example: |
|
106
|
|
|
* $encoder = $container->get('security.password_encoder'); |
|
107
|
|
|
* $encoded = $encoder->encodePassword($user, $plainPassword); |
|
108
|
|
|
* $user->setPassword($encoded); |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $encoded The encoded password. |
|
111
|
|
|
* |
|
112
|
|
|
* @return UserInformation |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setPassword($encoded) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->set('password', $encoded); |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritDoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getSalt() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->salt; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritDoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getUsername() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->get('username'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritDoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function eraseCredentials() |
|
141
|
|
|
{ |
|
142
|
|
|
$this->remove('password'); |
|
143
|
|
|
$this->salt = null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the granted access levels. |
|
148
|
|
|
* |
|
149
|
|
|
* @return int |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getAccessLevel() |
|
152
|
|
|
{ |
|
153
|
|
|
return (int) $this->get('acl', 0); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Set the granted access levels. |
|
158
|
|
|
* |
|
159
|
|
|
* @param int $accessLevel The granted access levels. |
|
160
|
|
|
* |
|
161
|
|
|
* @return UserInformation |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setAccessLevel($accessLevel) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->set('acl', (int) $accessLevel); |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
|
|
public function hasAccessLevel($accessLevel) |
|
174
|
|
|
{ |
|
175
|
|
|
return $accessLevel === ($this->getAccessLevel() & $accessLevel); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
* |
|
181
|
|
|
* @api |
|
182
|
|
|
*/ |
|
183
|
|
|
public function keys() |
|
184
|
|
|
{ |
|
185
|
|
|
return array_keys($this->data); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* {@inheritdoc} |
|
190
|
|
|
* |
|
191
|
|
|
* @api |
|
192
|
|
|
*/ |
|
193
|
|
|
public function get($key, $default = null) |
|
194
|
|
|
{ |
|
195
|
|
|
$key = strtolower($key); |
|
196
|
|
|
|
|
197
|
|
|
if (!array_key_exists($key, $this->data)) { |
|
198
|
|
|
if (null === $default) { |
|
199
|
|
|
return null; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $default; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $this->data[$key]; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Sets a value by name. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $key The key. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string|array $value The value. |
|
214
|
|
|
* |
|
215
|
|
|
* @return UserInformation |
|
216
|
|
|
* |
|
217
|
|
|
* @api |
|
218
|
|
|
*/ |
|
219
|
|
|
public function set($key, $value) |
|
220
|
|
|
{ |
|
221
|
|
|
$key = strtolower($key); |
|
222
|
|
|
|
|
223
|
|
|
$this->data[$key] = $value; |
|
224
|
|
|
|
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* {@inheritdoc} |
|
230
|
|
|
* |
|
231
|
|
|
* @api |
|
232
|
|
|
*/ |
|
233
|
|
|
public function has($key) |
|
234
|
|
|
{ |
|
235
|
|
|
return array_key_exists(strtolower($key), $this->data); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Removes a value. |
|
240
|
|
|
* |
|
241
|
|
|
* @param string $key The value name. |
|
242
|
|
|
* |
|
243
|
|
|
* @return UserInformation |
|
244
|
|
|
* |
|
245
|
|
|
* @api |
|
246
|
|
|
*/ |
|
247
|
|
|
public function remove($key) |
|
248
|
|
|
{ |
|
249
|
|
|
$key = strtolower($key); |
|
250
|
|
|
|
|
251
|
|
|
unset($this->data[$key]); |
|
252
|
|
|
|
|
253
|
|
|
return $this; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* {@inheritdoc} |
|
258
|
|
|
*/ |
|
259
|
|
|
public function values() |
|
260
|
|
|
{ |
|
261
|
|
|
return array_merge( |
|
262
|
|
|
[ |
|
263
|
|
|
'acl' => $this->getAccessLevel(), |
|
264
|
|
|
'salt' => $this->salt |
|
265
|
|
|
], |
|
266
|
|
|
$this->data |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* String representation of this user information for use in logs. |
|
272
|
|
|
* |
|
273
|
|
|
* Examples may be: "user foo" or "token 0123456789". |
|
274
|
|
|
* |
|
275
|
|
|
* @return string |
|
276
|
|
|
*/ |
|
277
|
|
|
public function asString() |
|
278
|
|
|
{ |
|
279
|
|
|
return 'authenticated'; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|