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