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