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