]> Raphaƫl G. Git Repositories - userbundle/blob - Entity/User.php
Set as static
[userbundle] / Entity / User.php
1 <?php
2
3 // src/Rapsys/UserBundle/Entity/User.php
4 namespace Rapsys\UserBundle\Entity;
5
6 use Rapsys\UserBundle\Entity\Group;
7 use Symfony\Component\Security\Core\User\UserInterface;
8 use Doctrine\Common\Collections\ArrayCollection;
9 use Rapsys\UserBundle\Entity\Civility;
10
11 class User implements UserInterface, \Serializable {
12 /**
13 * @var integer
14 */
15 protected $id;
16
17 /**
18 * @var string
19 */
20 protected $mail;
21
22 /**
23 * @var string
24 */
25 protected $pseudonym;
26
27 /**
28 * @var string
29 */
30 protected $forename;
31
32 /**
33 * @var string
34 */
35 protected $surname;
36
37 /**
38 * @var string
39 */
40 protected $password;
41
42 /**
43 * @var bool
44 */
45 protected $active;
46
47 /**
48 * @var bool
49 */
50 protected $disabled;
51
52 /**
53 * @var \DateTime
54 */
55 protected $created;
56
57 /**
58 * @var \DateTime
59 */
60 protected $updated;
61
62 /**
63 * @var \Rapsys\UserBundle\Entity\Civility
64 */
65 protected $civility;
66
67 /**
68 * @var \Doctrine\Common\Collections\ArrayCollection
69 */
70 protected $groups;
71
72 /**
73 * Constructor
74 */
75 public function __construct() {
76 $this->active = false;
77 $this->disabled = false;
78 $this->groups = new ArrayCollection();
79 }
80
81 /**
82 * Get id
83 *
84 * @return integer
85 */
86 public function getId(): int {
87 return $this->id;
88 }
89
90 /**
91 * Set mail
92 *
93 * @param string $mail
94 *
95 * @return User
96 */
97 public function setMail(string $mail) {
98 $this->mail = $mail;
99
100 return $this;
101 }
102
103 /**
104 * Get mail
105 *
106 * @return string
107 */
108 public function getMail(): ?string {
109 return $this->mail;
110 }
111
112 /**
113 * Set pseudonym
114 *
115 * @param string $pseudonym
116 *
117 * @return User
118 */
119 public function setPseudonym(string $pseudonym) {
120 $this->pseudonym = $pseudonym;
121
122 return $this;
123 }
124
125 /**
126 * Get pseudonym
127 *
128 * @return string
129 */
130 public function getPseudonym(): ?string {
131 return $this->pseudonym;
132 }
133
134 /**
135 * Set forename
136 *
137 * @param string $forename
138 *
139 * @return User
140 */
141 public function setForename(string $forename) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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 \Rapsys\UserBundle\Entity\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 \Rapsys\UserBundle\Entity\Group $group
323 */
324 public function removeGroup(Group $group) {
325 $this->groups->removeElement($group);
326 }
327
328 /**
329 * Get groups
330 *
331 * @return \Doctrine\Common\Collections\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 public function serialize(): string {
405 return serialize([
406 $this->id,
407 $this->mail,
408 $this->password,
409 $this->active,
410 $this->disabled,
411 $this->created,
412 $this->updated
413 ]);
414 }
415
416 public function unserialize($serialized) {
417 list(
418 $this->id,
419 $this->mail,
420 $this->password,
421 $this->active,
422 $this->disabled,
423 $this->created,
424 $this->updated
425 ) = unserialize($serialized);
426 }
427
428 /**
429 * Check if account is activated
430 *
431 * @xxx was from deprecated AdvancedUserInterface, see if it's used anymore
432 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
433 */
434 public function isActivated(): bool {
435 return $this->active;
436 }
437
438 /**
439 * Check if account is disabled
440 *
441 * @xxx was from deprecated AdvancedUserInterface, see if it's used anymore
442 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
443 */
444 public function isDisabled(): bool {
445 return $this->disabled;
446 }
447
448 /**
449 * {@inheritdoc}
450 */
451 public function preUpdate(\Doctrine\ORM\Event\PreUpdateEventArgs $eventArgs) {
452 //Check that we have an user instance
453 if (($user = $eventArgs->getEntity()) instanceof User) {
454 //Set updated value
455 $user->setUpdated(new \DateTime('now'));
456 }
457 }
458
459 /**
460 * Returns a string representation of the user
461 *
462 * @return string
463 */
464 public function __toString(): string {
465 return $this->civility.' '.$this->forename.' '.$this->surname;
466 }
467 }