1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\AuthenticationBundle\Entity\User; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Basic methods to implements Symfony\Component\Security\Core\User\UserInterface |
11
|
|
|
* |
12
|
|
|
* @author Nicolas Bastien <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
trait UserTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
* |
19
|
|
|
* @ORM\Column(type="string", length=255, nullable=false, unique=true) |
20
|
|
|
* |
21
|
|
|
* @Assert\NotBlank |
22
|
|
|
* @Assert\Email |
23
|
|
|
*/ |
24
|
|
|
private $email; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
* |
29
|
|
|
* @ORM\Column(name="password", type="string", length=100, nullable=false) |
30
|
|
|
*/ |
31
|
|
|
private $password; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $plainPassword; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
* |
41
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
42
|
|
|
*/ |
43
|
|
|
protected $firstName; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
* |
48
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
49
|
|
|
*/ |
50
|
|
|
protected $lastName; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var DateTime |
54
|
|
|
* |
55
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
56
|
|
|
*/ |
57
|
|
|
private $passwordRequestedAt; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
* |
62
|
|
|
* @ORM\Column(type="string", length=100, nullable=true) |
63
|
|
|
*/ |
64
|
|
|
private $passwordRequestToken; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
* |
69
|
|
|
* @ORM\Column(type="json_array") |
70
|
|
|
*/ |
71
|
|
|
private $roles; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function __toString() |
77
|
|
|
{ |
78
|
|
|
if (strlen(trim($this->getFullName())) > 0) { |
79
|
|
|
return (string) $this->getFullName(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return (string) $this->getEmail(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* On list we want to be able to sort by Lastname |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getListDisplay() |
90
|
|
|
{ |
91
|
|
|
if (strlen(trim($this->getLastName())) > 0) { |
92
|
|
|
return (string) $this->getListFullName(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return (string) $this->getEmail(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getId() |
102
|
|
|
{ |
103
|
|
|
return $this->id; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function getUsername() |
110
|
|
|
{ |
111
|
|
|
return $this->email; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
|
|
public function getEmail() |
118
|
|
|
{ |
119
|
|
|
return $this->email; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $email |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function setEmail($email) |
128
|
|
|
{ |
129
|
|
|
$this->email = $email; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
|
|
public function getPassword() |
138
|
|
|
{ |
139
|
|
|
return $this->password; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $password |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setPassword($password) |
148
|
|
|
{ |
149
|
|
|
$this->password = $password; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
public function getSalt() |
158
|
|
|
{ |
159
|
|
|
return null; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getPlainPassword() |
166
|
|
|
{ |
167
|
|
|
return $this->plainPassword; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $plainPassword |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setPlainPassword($plainPassword) |
176
|
|
|
{ |
177
|
|
|
$this->plainPassword = $plainPassword; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @inheritdoc |
184
|
|
|
*/ |
185
|
|
|
public function eraseCredentials() |
186
|
|
|
{ |
187
|
|
|
$this->plainPassword = null; |
188
|
|
|
|
189
|
|
|
return; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param int $ttl |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
public function isPasswordRequestNonExpired($ttl) |
198
|
|
|
{ |
199
|
|
|
return $this->passwordRequestedAt instanceof \DateTime && $this->passwordRequestedAt->getTimestamp() + $ttl > time(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return DateTime |
204
|
|
|
*/ |
205
|
|
|
public function getPasswordRequestedAt() |
206
|
|
|
{ |
207
|
|
|
return $this->passwordRequestedAt; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param DateTime $passwordRequestedAt |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function setPasswordRequestedAt($passwordRequestedAt) |
216
|
|
|
{ |
217
|
|
|
$this->passwordRequestedAt = $passwordRequestedAt; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function getPasswordRequestToken() |
226
|
|
|
{ |
227
|
|
|
return $this->passwordRequestToken; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $passwordRequestToken |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function setPasswordRequestToken($passwordRequestToken) |
236
|
|
|
{ |
237
|
|
|
$this->passwordRequestToken = $passwordRequestToken; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritdoc |
244
|
|
|
*/ |
245
|
|
|
public function getRoles() |
246
|
|
|
{ |
247
|
|
|
return $this->roles; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param array $roles |
252
|
|
|
* |
253
|
|
|
* @return $this |
254
|
|
|
*/ |
255
|
|
|
public function setRoles(array $roles = []) |
256
|
|
|
{ |
257
|
|
|
$this->roles = $roles; |
258
|
|
|
|
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
|
|
public function getFirstName() |
266
|
|
|
{ |
267
|
|
|
return $this->firstName; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $firstName |
272
|
|
|
* |
273
|
|
|
* @return $this |
274
|
|
|
*/ |
275
|
|
|
public function setFirstName($firstName) |
276
|
|
|
{ |
277
|
|
|
$this->firstName = $firstName; |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
public function getLastName() |
286
|
|
|
{ |
287
|
|
|
return $this->lastName; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $lastName |
292
|
|
|
* |
293
|
|
|
* @return $this |
294
|
|
|
*/ |
295
|
|
|
public function setLastName($lastName) |
296
|
|
|
{ |
297
|
|
|
$this->lastName = $lastName; |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function getFullName() |
306
|
|
|
{ |
307
|
|
|
return sprintf('%s %s', (string)$this->getFirstName(), (string)$this->getLastName()); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
|
|
public function getListFullName() |
314
|
|
|
{ |
315
|
|
|
return sprintf('%s %s', (string)$this->getLastName(), (string)$this->getFirstName()); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function getFullNameAndEmail() |
322
|
|
|
{ |
323
|
|
|
if (strlen(trim($this->getFullName())) > 0) { |
324
|
|
|
return sprintf('%s - %s', $this->getFullName(), $this->getEmail()); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $this->getEmail(); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @see \Serializable::serialize() |
332
|
|
|
*/ |
333
|
|
|
public function serialize() |
334
|
|
|
{ |
335
|
|
|
return serialize(array( |
336
|
|
|
$this->id, |
337
|
|
|
$this->email, |
338
|
|
|
$this->password, |
339
|
|
|
)); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @see \Serializable::unserialize() |
344
|
|
|
*/ |
345
|
|
|
public function unserialize($serialized) |
346
|
|
|
{ |
347
|
|
|
list ( |
348
|
|
|
$this->id, |
349
|
|
|
$this->email, |
350
|
|
|
$this->password, |
351
|
|
|
// see section on salt below |
352
|
|
|
// $this->salt |
353
|
|
|
) = unserialize($serialized); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: