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