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