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