]> Raphaël G. Git Repositories - userbundle/blob - Entity/User.php
Add requirement
[userbundle] / Entity / User.php
1 <?php declare(strict_types=1);
2
3 /*
4 * this file is part of the rapsys packbundle package.
5 *
6 * (c) raphaël gertz <symfony@rapsys.eu>
7 *
8 * for the full copyright and license information, please view the license
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\UserBundle\Entity;
13
14 use Doctrine\Common\Collections\ArrayCollection;
15 use Doctrine\ORM\Event\PreUpdateEventArgs;
16 use Symfony\Component\Security\Core\User\UserInterface;
17 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
18
19 use Rapsys\UserBundle\Entity\Civility;
20 use Rapsys\UserBundle\Entity\Group;
21
22 /**
23 * User
24 */
25 class User implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable {
26 /**
27 * @var integer
28 */
29 protected $id;
30
31 /**
32 * @var string
33 */
34 protected $mail;
35
36 /**
37 * @var string
38 */
39 protected $forename;
40
41 /**
42 * @var string
43 */
44 protected $surname;
45
46 /**
47 * @var string
48 */
49 protected $password;
50
51 /**
52 * @var bool
53 */
54 protected $active;
55
56 /**
57 * @var bool
58 */
59 protected $disabled;
60
61 /**
62 * @var \DateTime
63 */
64 protected $created;
65
66 /**
67 * @var \DateTime
68 */
69 protected $updated;
70
71 /**
72 * @var Civility
73 */
74 protected $civility;
75
76 /**
77 * @var ArrayCollection
78 */
79 protected $groups;
80
81 /**
82 * Constructor
83 *
84 * @param ?string $mail The user mail
85 */
86 public function __construct(?string $mail) {
87 //With mail
88 if ($mail !== null && !empty($mail)) {
89 $this->mail = $mail;
90 $this->password = $mail;
91 //Without mail
92 } else {
93 $this->mail = '';
94 $this->password = '';
95 }
96
97 //Set defaults
98 $this->forename = null;
99 $this->surname = null;
100 $this->active = false;
101 $this->disabled = false;
102 $this->created = new \DateTime('now');
103 $this->updated = new \DateTime('now');
104
105 //Set collections
106 $this->groups = new ArrayCollection();
107 }
108
109 /**
110 * Get id
111 *
112 * @return integer
113 */
114 public function getId(): int {
115 return $this->id;
116 }
117
118 /**
119 * Set mail
120 *
121 * @param ?string $mail
122 * @return User
123 */
124 public function setMail(?string $mail): User {
125 //With mail
126 if ($mail !== null && !empty($mail)) {
127 //Set mail
128 $this->mail = $mail;
129
130 //Without password
131 if (empty($this->password)) {
132 //Set mail as password
133 $this->password = $mail;
134 }
135 //Without mail
136 } else {
137 $this->mail = '';
138 }
139
140 return $this;
141 }
142
143 /**
144 * Get mail
145 *
146 * @return string
147 */
148 public function getMail(): string {
149 return $this->mail;
150 }
151
152 /**
153 * Set forename
154 *
155 * @param ?string $forename
156 *
157 * @return User
158 */
159 public function setForename(?string $forename): User {
160 $this->forename = $forename;
161
162 return $this;
163 }
164
165 /**
166 * Get forename
167 *
168 * @return ?string
169 */
170 public function getForename(): ?string {
171 return $this->forename;
172 }
173
174 /**
175 * Set surname
176 *
177 * @param ?string $surname
178 *
179 * @return User
180 */
181 public function setSurname(?string $surname): User {
182 $this->surname = $surname;
183
184 return $this;
185 }
186
187 /**
188 * Get surname
189 *
190 * @return ?string
191 */
192 public function getSurname(): ?string {
193 return $this->surname;
194 }
195
196 /**
197 * Set password
198 *
199 * @param string $password
200 *
201 * @return User
202 */
203 public function setPassword(?string $password): User {
204 //With password
205 if ($password !== null && !empty($password)) {
206 $this->password = $password;
207 //Without password
208 } else {
209 $this->password = '';
210 }
211
212 return $this;
213 }
214
215 /**
216 * Get password
217 *
218 * {@inheritdoc}
219 *
220 * @return string
221 */
222 public function getPassword(): string {
223 return $this->password;
224 }
225
226 /**
227 * Set active
228 *
229 * @param bool $active
230 *
231 * @return User
232 */
233 public function setActive(bool $active): User {
234 $this->active = $active;
235
236 return $this;
237 }
238
239 /**
240 * Get active
241 *
242 * @return bool
243 */
244 public function getActive(): bool {
245 return $this->active;
246 }
247
248 /**
249 * Set disabled
250 *
251 * @param bool $disabled
252 *
253 * @return User
254 */
255 public function setDisabled(bool $disabled): User {
256 $this->disabled = $disabled;
257
258 return $this;
259 }
260
261 /**
262 * Get disabled
263 *
264 * @return bool
265 */
266 public function getDisabled(): bool {
267 return $this->disabled;
268 }
269
270 /**
271 * Set created
272 *
273 * @param \DateTime $created
274 *
275 * @return User
276 */
277 public function setCreated(\DateTime $created): User {
278 $this->created = $created;
279
280 return $this;
281 }
282
283 /**
284 * Get created
285 *
286 * @return \DateTime
287 */
288 public function getCreated(): \DateTime {
289 return $this->created;
290 }
291
292 /**
293 * Set updated
294 *
295 * @param \DateTime $updated
296 *
297 * @return User
298 */
299 public function setUpdated(\DateTime $updated): User {
300 $this->updated = $updated;
301
302 return $this;
303 }
304
305 /**
306 * Get updated
307 *
308 * @return \DateTime
309 */
310 public function getUpdated(): \DateTime {
311 return $this->updated;
312 }
313
314 /**
315 * Set civility
316 */
317 public function setCivility(Civility $civility): User {
318 $this->civility = $civility;
319
320 return $this;
321 }
322
323 /**
324 * Get civility
325 */
326 public function getCivility(): ?Civility {
327 return $this->civility;
328 }
329
330 /**
331 * Add group
332 *
333 * @param Group $group
334 *
335 * @return User
336 */
337 public function addGroup(Group $group) {
338 $this->groups[] = $group;
339
340 return $this;
341 }
342
343 /**
344 * Remove group
345 *
346 * @param Group $group
347 */
348 public function removeGroup(Group $group) {
349 $this->groups->removeElement($group);
350 }
351
352 /**
353 * Get groups
354 *
355 * @return ArrayCollection
356 */
357 public function getGroups(): ArrayCollection {
358 return $this->groups;
359 }
360
361 /**
362 * {@inheritdoc}
363 */
364 public function getRoles(): array {
365 //Get the unique roles list by id
366 return array_unique(array_reduce(
367 //Cast groups as array
368 $this->groups->toArray(),
369 //Reduce to an array of id => group tuples
370 function ($array, $group) {
371 $array[$group->getId()] = $group->getRole();
372 return $array;
373 },
374 //Init with empty array
375 //XXX: on registration, add each group present in rapsys_user.default.group array to user
376 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
377 []
378 ));
379 }
380
381 /**
382 * {@inheritdoc}
383 */
384 public function getRole(): ?string {
385 //Retrieve roles
386 $roles = $this->getRoles();
387
388 //With roles array empty
389 if ($roles === []) {
390 //Return null
391 return null;
392 }
393
394 //Return the role with max id
395 //XXX: should be rewriten if it change in your configuration
396 return $roles[array_reduce(
397 array_keys($roles),
398 function($cur, $id) {
399 if ($cur === null || $id > $cur) {
400 return $id;
401 }
402 return $cur;
403 },
404 null
405 )];
406 }
407
408 /**
409 * {@inheritdoc}
410 */
411 public function getSalt(): ?string {
412 //No salt required with bcrypt
413 return null;
414 }
415
416 /**
417 * {@inheritdoc}
418 */
419 public function getUsername(): string {
420 return $this->mail;
421 }
422
423 /**
424 * {@inheritdoc}
425 */
426 public function getUserIdentifier(): string {
427 return $this->mail;
428 }
429
430 /**
431 * {@inheritdoc}
432 */
433 public function eraseCredentials(): void {}
434
435 /**
436 * {@inheritdoc}
437 */
438 public function serialize(): string {
439 return serialize([
440 $this->id,
441 $this->mail,
442 $this->forename,
443 $this->surname,
444 $this->password,
445 $this->active,
446 $this->disabled,
447 $this->created,
448 $this->updated
449 ]);
450 }
451
452 /**
453 * {@inheritdoc}
454 */
455 public function unserialize($serialized) {
456 list(
457 $this->id,
458 $this->mail,
459 $this->forename,
460 $this->surname,
461 $this->password,
462 $this->active,
463 $this->disabled,
464 $this->created,
465 $this->updated
466 ) = unserialize($serialized);
467 }
468
469 /**
470 * Check if account is activated
471 *
472 * It was from deprecated AdvancedUserInterface, see if it's used anymore
473 *
474 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
475 */
476 public function isActivated(): bool {
477 return $this->active;
478 }
479
480 /**
481 * Check if account is disabled
482 *
483 * It was from deprecated AdvancedUserInterface, see if it's used anymore
484 *
485 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
486 */
487 public function isDisabled(): bool {
488 return $this->disabled;
489 }
490
491 /**
492 * {@inheritdoc}
493 */
494 public function preUpdate(PreUpdateEventArgs $eventArgs) {
495 //Check that we have an user instance
496 if (($user = $eventArgs->getEntity()) instanceof User) {
497 //Set updated value
498 $user->setUpdated(new \DateTime('now'));
499 }
500 }
501
502 /**
503 * Returns a recipient name of the user
504 *
505 * @return string
506 */
507 public function getRecipientName(): string {
508 //Without forename and surname
509 if (empty($this->forename) && empty($this->surname)) {
510 //Return recipient name from mail
511 return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail)))));
512 }
513
514 //Return recipient name from forename and surname
515 return implode(' ', [$this->forename, $this->surname]);
516 }
517
518 /**
519 * Returns a string representation of the user
520 *
521 * @return string
522 */
523 public function __toString(): string {
524 return $this->civility.' '.$this->forename.' '.$this->surname;
525 }
526 }