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