]> Raphaƫl G. Git Repositories - userbundle/blob - Entity/User.php
Cleanup
[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 ROLE_USER
323 //XXX: we assume that ROLE_USER has id 1 in database
324 [ 1 => 'ROLE_USER' ]
325 ));
326 }
327
328 public function getRole() {
329 //Retrieve roles
330 $roles = $this->getRoles();
331
332 //Return the role with max id
333 //XXX: should be rewriten if it change in your configuration
334 return $roles[array_reduce(
335 array_keys($roles),
336 function($cur, $id) {
337 if ($id > $cur) {
338 return $id;
339 }
340 return $cur;
341 },
342 0
343 )];
344 }
345
346 /**
347 * {@inheritdoc}
348 */
349 public function getSalt() {
350 //No salt required with bcrypt
351 return null;
352 }
353
354 /**
355 * {@inheritdoc}
356 */
357 public function getUsername() {
358 return $this->mail;
359 }
360
361 /**
362 * {@inheritdoc}
363 */
364 public function eraseCredentials() {}
365
366 public function serialize(): string {
367 return serialize([
368 $this->id,
369 $this->mail,
370 $this->password,
371 $this->active,
372 $this->created,
373 $this->updated
374 ]);
375 }
376
377 public function unserialize($serialized) {
378 list(
379 $this->id,
380 $this->mail,
381 $this->password,
382 $this->active,
383 $this->created,
384 $this->updated
385 ) = unserialize($serialized);
386 }
387
388 //XXX: was from vendor/symfony/security-core/User/AdvancedUserInterface.php, see if it's used anymore
389 public function isEnabled() {
390 return $this->active;
391 }
392
393 /**
394 * Returns a string representation of the user
395 *
396 * @return string
397 */
398 public function __toString(): string {
399 return $this->title.' '.$this->forename.' '.$this->surname;
400 }
401 }