3 // src/Rapsys/UserBundle/Entity/User.php
4 namespace Rapsys\UserBundle\Entity
;
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
;
11 class User
implements UserInterface
, \Serializable
{
58 * @var \Rapsys\UserBundle\Entity\Civility
63 * @var \Doctrine\Common\Collections\Collection
70 public function __construct() {
71 $this->active
= false;
72 $this->groups
= new ArrayCollection();
80 public function getId() {
91 public function setMail($mail) {
102 public function getMail() {
109 * @param string $pseudonym
113 public function setPseudonym($pseudonym) {
114 $this->pseudonym
= $pseudonym;
124 public function getPseudonym() {
125 return $this->pseudonym
;
131 * @param string $forename
135 public function setForename($forename) {
136 $this->forename
= $forename;
146 public function getForename() {
147 return $this->forename
;
153 * @param string $surname
157 public function setSurname($surname) {
158 $this->surname
= $surname;
168 public function getSurname() {
169 return $this->surname
;
175 * @param string $password
179 public function setPassword($password) {
180 $this->password
= $password;
192 public function getPassword() {
193 return $this->password
;
199 * @param bool $active
203 public function setActive($active) {
204 $this->active
= $active;
214 public function getActive() {
215 return $this->active
;
221 * @param \DateTime $created
225 public function setCreated($created) {
226 $this->created
= $created;
236 public function getCreated() {
237 return $this->created
;
243 * @param \DateTime $updated
247 public function setUpdated($updated) {
248 $this->updated
= $updated;
258 public function getUpdated() {
259 return $this->updated
;
265 public function setCivility(Civility
$civility) {
266 $this->civility
= $civility;
274 public function getCivility(): Civility
{
275 return $this->civility
;
281 * @param \Rapsys\UserBundle\Entity\Group $group
285 public function addGroup(Group
$group) {
286 $this->groups
[] = $group;
294 * @param \Rapsys\UserBundle\Entity\Group $group
296 public function removeGroup(Group
$group) {
297 $this->groups
->removeElement($group);
303 * @return \Doctrine\Common\Collections\Collection
305 public function getGroups() {
306 return $this->groups
;
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();
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
332 public function getRole() {
334 $roles = $this->getRoles();
336 //With roles array empty
342 //Return the role with max id
343 //XXX: should be rewriten if it change in your configuration
344 return $roles[array_reduce(
346 function($cur, $id) {
347 if ($cur === null || $id > $cur) {
359 public function getSalt() {
360 //No salt required with bcrypt
367 public function getUsername() {
374 public function eraseCredentials() {}
376 public function serialize(): string {
387 public function unserialize($serialized) {
395 ) = unserialize($serialized);
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
;
406 public function preUpdate(\Doctrine\ORM\Event\PreUpdateEventArgs
$eventArgs) {
407 //Check that we have an user instance
408 if (($user = $eventArgs->getEntity()) instanceof User
) {
410 $user->setUpdated(new \
DateTime('now'));
415 * Returns a string representation of the user
419 public function __toString(): string {
420 return $this->civility
.' '.$this->forename
.' '.$this->surname
;