1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Webcook security bundle. |
5
|
|
|
* |
6
|
|
|
* See LICENSE file in the root of the bundle. Webcook |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Webcook\Cms\SecurityBundle\Entity; |
10
|
|
|
|
11
|
|
|
use Doctrine\ORM\Mapping as ORM; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Webcook\Cms\CoreBundle\Base\BasicEntity; |
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
use ApiPlatform\Core\Annotation\ApiResource; |
16
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* System user entity. |
20
|
|
|
* |
21
|
|
|
* @ApiResource |
22
|
|
|
* @ORM\Table(name="SecurityUser") |
23
|
|
|
* @ORM\Entity(repositoryClass="Webcook\Cms\SecurityBundle\Entity\UserRepository") |
24
|
|
|
*/ |
25
|
|
|
class User extends BasicEntity implements UserInterface, \Serializable |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Username of the user. |
29
|
|
|
* |
30
|
|
|
* @ORM\Column(type="string", length=64, unique=true) |
31
|
|
|
* @Assert\NotNull |
32
|
|
|
* @Assert\NotBlank |
33
|
|
|
*/ |
34
|
|
|
private $username; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Password of the user. |
38
|
|
|
* |
39
|
|
|
* @ORM\Column(type="string", length=64, nullable=true) |
40
|
|
|
*/ |
41
|
|
|
private $password; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Password Reset Token of the agent. |
45
|
|
|
* |
46
|
|
|
* @ORM\Column(type="string", length=64, nullable=true) |
47
|
|
|
*/ |
48
|
|
|
private $passwordResetToken; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Password Reset Expiration of the agent. |
52
|
|
|
* @ORM\Column(name="passwordResetExpiration", type="datetime", nullable=true) |
53
|
|
|
*/ |
54
|
|
|
private $passwordResetExpiration; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Email of the user. |
58
|
|
|
* |
59
|
|
|
* @ORM\Column(type="string", length=60, unique=true) |
60
|
|
|
* @Assert\NotNull |
61
|
|
|
* @Assert\NotBlank |
62
|
|
|
*/ |
63
|
|
|
private $email; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* User's roles. |
67
|
|
|
* |
68
|
|
|
* @ORM\ManyToMany(targetEntity="Role") |
69
|
|
|
*/ |
70
|
|
|
private $roles; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Tells whether user account is active or not. |
74
|
|
|
* |
75
|
|
|
* @ORM\Column(name="is_active", type="boolean", nullable=true) |
76
|
|
|
*/ |
77
|
|
|
private $isActive; |
78
|
|
|
|
79
|
|
|
/** |
80
|
49 |
|
* @ORM\OneToMany(targetEntity="Setting", mappedBy="user", cascade={"persist"}) |
81
|
|
|
*/ |
82
|
49 |
|
private $settings; |
83
|
49 |
|
|
84
|
49 |
|
/** |
85
|
|
|
* Class constructor. |
86
|
49 |
|
*/ |
87
|
|
|
public function __construct() |
88
|
49 |
|
{ |
89
|
49 |
|
$this->roles = new ArrayCollection(); |
90
|
49 |
|
$this->isActive = true; |
91
|
49 |
|
$this->settings = new ArrayCollection(); |
92
|
49 |
|
|
93
|
|
|
$timezone = new Setting(); |
94
|
49 |
|
|
95
|
|
|
$timezone->setName('Timezone'); |
96
|
49 |
|
$timezone->setKey('timezone'); |
97
|
|
|
$timezone->setValue('GMT'); |
98
|
49 |
|
$timezone->setSection('general'); |
99
|
49 |
|
$timezone->setUser($this); |
100
|
49 |
|
|
101
|
49 |
|
$this->settings->add($timezone); |
102
|
49 |
|
|
103
|
|
|
$language = new Setting(); |
104
|
49 |
|
|
105
|
49 |
|
$language->setName('language'); |
106
|
|
|
$language->setKey('language'); |
107
|
|
|
$language->setValue('en'); |
108
|
|
|
$language->setSection('general'); |
109
|
|
|
$language->setUser($this); |
110
|
|
|
|
111
|
|
|
$this->settings->add($language); |
112
|
40 |
|
} |
113
|
|
|
|
114
|
40 |
|
/** |
115
|
|
|
* Get username. |
116
|
|
|
* |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
|
|
public function getUsername() |
120
|
|
|
{ |
121
|
|
|
return $this->username; |
122
|
49 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get salt. |
126
|
49 |
|
* |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
|
|
public function getSalt() |
130
|
|
|
{ |
131
|
|
|
// you *may* need a real salt depending on your encoder |
132
|
|
|
// see section on salt below |
133
|
|
|
return null; |
134
|
39 |
|
} |
135
|
|
|
|
136
|
39 |
|
/** |
137
|
|
|
* Get password. |
138
|
|
|
* |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function getPassword() |
142
|
|
|
{ |
143
|
|
|
return $this->password; |
144
|
39 |
|
} |
145
|
|
|
|
146
|
39 |
|
/** |
147
|
|
|
* Get all roles. |
148
|
|
|
* |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
|
|
public function getRoles() |
152
|
|
|
{ |
153
|
|
|
return $this->roles->toArray(); |
154
|
39 |
|
} |
155
|
|
|
|
156
|
39 |
|
/** |
157
|
|
|
* Not implemented. |
158
|
|
|
* |
159
|
|
|
* @inheritDoc |
160
|
|
|
*/ |
161
|
|
|
public function eraseCredentials() |
162
|
|
|
{ |
163
|
39 |
|
} |
164
|
|
|
|
165
|
39 |
|
/** |
166
|
39 |
|
* Serialize object into array. |
167
|
39 |
|
* |
168
|
39 |
|
* @see \Serializable::serialize() |
169
|
39 |
|
*/ |
170
|
39 |
|
public function serialize() |
171
|
|
|
{ |
172
|
|
|
return serialize(array( |
173
|
|
|
$this->id, |
174
|
|
|
$this->username, |
175
|
|
|
$this->password, |
176
|
|
|
$this->email, |
177
|
|
|
$this->version, |
178
|
|
|
// see section on salt below |
179
|
|
|
// $this->salt, |
180
|
|
|
)); |
181
|
|
|
} |
182
|
|
|
|
183
|
39 |
|
/** |
184
|
|
|
* Unserialize array into object. |
185
|
|
|
* |
186
|
39 |
|
* @param $serialized |
187
|
39 |
|
* |
188
|
39 |
|
* @see \Serializable::unserialize() |
189
|
39 |
|
*/ |
190
|
39 |
|
public function unserialize($serialized) |
191
|
|
|
{ |
192
|
|
|
list( |
193
|
|
|
$this->id, |
194
|
39 |
|
$this->username, |
195
|
39 |
|
$this->password, |
196
|
|
|
$this->email, |
197
|
|
|
$this->version, |
198
|
|
|
|
199
|
|
|
// see section on salt below |
200
|
|
|
// $this->salt |
201
|
|
|
) = unserialize($serialized); |
202
|
7 |
|
} |
203
|
|
|
|
204
|
7 |
|
/** |
205
|
|
|
* Gets the value of email. |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
|
public function getEmail() |
210
|
|
|
{ |
211
|
|
|
return $this->email; |
212
|
|
|
} |
213
|
|
|
|
214
|
49 |
|
/** |
215
|
|
|
* Sets the value of email. |
216
|
49 |
|
* |
217
|
|
|
* @param string $email the email |
218
|
49 |
|
* |
219
|
|
|
* @return self |
220
|
|
|
*/ |
221
|
|
|
public function setEmail($email) |
222
|
|
|
{ |
223
|
|
|
$this->email = $email; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
7 |
|
} |
227
|
|
|
|
228
|
7 |
|
/** |
229
|
|
|
* Gets the value of isActive. |
230
|
|
|
* |
231
|
|
|
* @return mixed |
232
|
|
|
*/ |
233
|
|
|
public function getIsActive() |
234
|
|
|
{ |
235
|
|
|
return $this->isActive; |
236
|
|
|
} |
237
|
|
|
|
238
|
6 |
|
/** |
239
|
|
|
* Sets the value of isActive. |
240
|
6 |
|
* |
241
|
|
|
* @param mixed $isActive the is active |
242
|
6 |
|
* |
243
|
|
|
* @return self |
244
|
|
|
*/ |
245
|
|
|
public function setIsActive($isActive) |
246
|
|
|
{ |
247
|
|
|
$this->isActive = $isActive; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
49 |
|
/** |
253
|
|
|
* Sets the value of username. |
254
|
49 |
|
* |
255
|
|
|
* @param string $username the username |
256
|
49 |
|
* |
257
|
|
|
* @return self |
258
|
|
|
*/ |
259
|
|
|
public function setUsername($username) |
260
|
|
|
{ |
261
|
|
|
$this->username = $username; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
49 |
|
/** |
267
|
|
|
* Sets the value of password. |
268
|
49 |
|
* |
269
|
|
|
* @param mixed $password the password |
270
|
49 |
|
* |
271
|
|
|
* @return self |
272
|
|
|
*/ |
273
|
|
|
public function setPassword($password) |
274
|
|
|
{ |
275
|
|
|
$this->password = $password; |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
49 |
|
} |
279
|
|
|
|
280
|
49 |
|
/** |
281
|
49 |
|
* Add role to the user. |
282
|
|
|
* |
283
|
49 |
|
* @param Role $role [description] |
284
|
|
|
*/ |
285
|
|
|
public function addRole(Role $role) |
286
|
|
|
{ |
287
|
|
|
if (!$this->roles->contains($role)) { |
288
|
|
|
$this->roles->add($role); |
289
|
|
|
} |
290
|
1 |
|
} |
291
|
|
|
|
292
|
1 |
|
/** |
293
|
1 |
|
* Remove role. |
294
|
|
|
* |
295
|
1 |
|
* @param Role $role |
296
|
|
|
*/ |
297
|
|
|
public function removeRole(Role $role) |
298
|
|
|
{ |
299
|
|
|
if ($this->roles->contains($role)) { |
300
|
|
|
$this->roles->removeElement($role); |
301
|
5 |
|
} |
302
|
|
|
} |
303
|
5 |
|
|
304
|
5 |
|
/** |
305
|
|
|
* Remove all roles. |
306
|
|
|
* |
307
|
|
|
*/ |
308
|
|
|
public function removeRoles() |
309
|
|
|
{ |
310
|
|
|
$this->roles->clear(); |
311
|
1 |
|
} |
312
|
|
|
|
313
|
1 |
|
/** |
314
|
|
|
* Gets the value of settings. |
315
|
|
|
* |
316
|
|
|
* @return ArrayCollection |
317
|
|
|
*/ |
318
|
|
|
public function getSettings() |
319
|
|
|
{ |
320
|
|
|
return $this->settings; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function getSettingsByName($name) |
324
|
|
|
{ |
325
|
|
|
foreach ($this->getSettings() as &$value) { |
|
|
|
|
326
|
|
|
if ($value->getName() == $name) { |
327
|
|
|
return $value; |
328
|
|
|
} |
329
|
|
|
} |
330
|
5 |
|
} |
331
|
|
|
|
332
|
5 |
|
/** |
333
|
|
|
* Get passwordResetToken. |
334
|
|
|
* |
335
|
|
|
* @inheritDoc |
336
|
|
|
*/ |
337
|
|
|
public function getPasswordResetToken() |
338
|
|
|
{ |
339
|
|
|
return $this->passwordResetToken; |
340
|
|
|
} |
341
|
|
|
|
342
|
6 |
|
/** |
343
|
|
|
* Sets the value of password reset token. |
344
|
6 |
|
* |
345
|
|
|
* @param null|string $passwordResetToken the passwordResetToken |
346
|
6 |
|
* |
347
|
|
|
* @return self |
348
|
|
|
*/ |
349
|
|
|
public function setPasswordResetToken($passwordResetToken) |
350
|
|
|
{ |
351
|
|
|
$this->passwordResetToken = $passwordResetToken; |
352
|
|
|
|
353
|
|
|
return $this; |
354
|
3 |
|
} |
355
|
|
|
|
356
|
3 |
|
/** |
357
|
|
|
* Get passwordResetExpiration. |
358
|
|
|
* |
359
|
|
|
* @inheritDoc |
360
|
|
|
*/ |
361
|
|
|
public function getPasswordResetExpiration() |
362
|
|
|
{ |
363
|
|
|
return $this->passwordResetExpiration; |
364
|
|
|
} |
365
|
|
|
|
366
|
6 |
|
/** |
367
|
|
|
* Sets the value of password reset token. |
368
|
6 |
|
* |
369
|
|
|
* @param null|\DateTime $passwordResetExpiration the passwordResetExpiration |
370
|
6 |
|
* |
371
|
|
|
* @return self |
372
|
|
|
*/ |
373
|
|
|
public function setPasswordResetExpiration($passwordResetExpiration) |
374
|
|
|
{ |
375
|
|
|
$this->passwordResetExpiration = $passwordResetExpiration; |
376
|
|
|
|
377
|
|
|
return $this; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
Let?s assume that you have the following
foreach
statement:$itemValue
is assigned by reference. This is possible because the expression (in the example$array
) can be used as a reference target.However, if we were to replace
$array
with something different like the result of a function call as inthen assigning by reference is not possible anymore as there is no target that could be modified.
Available Fixes
1. Do not assign by reference
2. Assign to a local variable first
3. Return a reference