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