]> Raphaƫl G. Git Repositories - userbundle/blob - Entity/User.php
8286e55dd59c7c7cad07b03744e768459c23ff29
[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\Title;
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\Title
59 */
60 protected $title;
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 title
264 */
265 public function setTitle(Title $title) {
266 $this->title = $title;
267
268 return $this;
269 }
270
271 /**
272 * Get title
273 */
274 public function getTitle(): Title {
275 return $this->title;
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 public function getRole() {
330 //Retrieve roles
331 $roles = $this->getRoles();
332
333 //With roles array empty
334 if ($roles === []) {
335 //Return null
336 return null;
337 }
338
339 //Return the role with max id
340 //XXX: should be rewriten if it change in your configuration
341 return $roles[array_reduce(
342 array_keys($roles),
343 function($cur, $id) {
344 if ($cur === null || $id > $cur) {
345 return $id;
346 }
347 return $cur;
348 },
349 null
350 )];
351 }
352
353 /**
354 * {@inheritdoc}
355 */
356 public function getSalt() {
357 //No salt required with bcrypt
358 return null;
359 }
360
361 /**
362 * {@inheritdoc}
363 */
364 public function getUsername() {
365 return $this->mail;
366 }
367
368 /**
369 * {@inheritdoc}
370 */
371 public function eraseCredentials() {}
372
373 public function serialize(): string {
374 return serialize([
375 $this->id,
376 $this->mail,
377 $this->password,
378 $this->active,
379 $this->created,
380 $this->updated
381 ]);
382 }
383
384 public function unserialize($serialized) {
385 list(
386 $this->id,
387 $this->mail,
388 $this->password,
389 $this->active,
390 $this->created,
391 $this->updated
392 ) = unserialize($serialized);
393 }
394
395 //XXX: was from vendor/symfony/security-core/User/AdvancedUserInterface.php, see if it's used anymore
396 public function isEnabled() {
397 return $this->active;
398 }
399
400 /**
401 * Returns a string representation of the user
402 *
403 * @return string
404 */
405 public function __toString(): string {
406 return $this->title.' '.$this->forename.' '.$this->surname;
407 }
408 }