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