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