1 <?php 
declare(strict_types
=1); 
   4  * this file is part of the rapsys packbundle package. 
   6  * (c) raphaël gertz <symfony@rapsys.eu> 
   8  * for the full copyright and license information, please view the license 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\UserBundle\Entity
; 
  14 use Doctrine\Common\Collections\ArrayCollection
; 
  15 use Doctrine\ORM\Event\PreUpdateEventArgs
; 
  16 use Symfony\Component\Security\Core\User\UserInterface
; 
  17 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
; 
  19 use Rapsys\UserBundle\Entity\Civility
; 
  20 use Rapsys\UserBundle\Entity\Group
; 
  25 class User 
implements UserInterface
, PasswordAuthenticatedUserInterface 
{ 
  77          * @var ArrayCollection 
  84          * @param ?string $mail The user mail 
  86         public function __construct(?string $mail = null) { 
  88                 if ($mail !== null && !empty($mail)) { 
  90                         $this->password 
= $mail; 
  98                 $this->forename 
= null; 
  99                 $this->surname 
= null; 
 100                 $this->active 
= false; 
 101                 $this->disabled 
= false; 
 102                 $this->created 
= new \
DateTime('now'); 
 103                 $this->updated 
= new \
DateTime('now'); 
 106                 $this->groups 
= new ArrayCollection(); 
 114         public function getId(): int { 
 121          * @param ?string $mail 
 124         public function setMail(?string $mail): User 
{ 
 126                 if ($mail !== null && !empty($mail)) { 
 131                         if (empty($this->password
)) { 
 132                                 //Set mail as password 
 133                                 $this->password 
= $mail; 
 148         public function getMail(): string { 
 155          * @param ?string $forename 
 159         public function setForename(?string $forename): User 
{ 
 160                 $this->forename 
= $forename; 
 170         public function getForename(): ?string { 
 171                 return $this->forename
; 
 177          * @param ?string $surname 
 181         public function setSurname(?string $surname): User 
{ 
 182                 $this->surname 
= $surname; 
 192         public function getSurname(): ?string { 
 193                 return $this->surname
; 
 199          * @param string $password 
 203         public function setPassword(?string $password): User 
{ 
 205                 if ($password !== null && !empty($password)) { 
 206                         $this->password 
= $password; 
 209                         $this->password 
= ''; 
 222         public function getPassword(): string { 
 223                 return $this->password
; 
 229          * @param bool $active 
 233         public function setActive(bool $active): User 
{ 
 234                 $this->active 
= $active; 
 244         public function getActive(): bool { 
 245                 return $this->active
; 
 251          * @param bool $disabled 
 255         public function setDisabled(bool $disabled): User 
{ 
 256                 $this->disabled 
= $disabled; 
 266         public function getDisabled(): bool { 
 267                 return $this->disabled
; 
 273          * @param \DateTime $created 
 277         public function setCreated(\DateTime 
$created): User 
{ 
 278                 $this->created 
= $created; 
 288         public function getCreated(): \DateTime 
{ 
 289                 return $this->created
; 
 295          * @param \DateTime $updated 
 299         public function setUpdated(\DateTime 
$updated): User 
{ 
 300                 $this->updated 
= $updated; 
 310         public function getUpdated(): \DateTime 
{ 
 311                 return $this->updated
; 
 317         public function setCivility(Civility 
$civility): User 
{ 
 318                 $this->civility 
= $civility; 
 326         public function getCivility(): ?Civility 
{ 
 327                 return $this->civility
; 
 333          * @param Group $group 
 337         public function addGroup(Group 
$group) { 
 338                 $this->groups
[] = $group; 
 346          * @param Group $group 
 348         public function removeGroup(Group 
$group) { 
 349                 $this->groups
->removeElement($group); 
 355          * @return ArrayCollection 
 357         public function getGroups(): ArrayCollection 
{ 
 358                 return $this->groups
; 
 364         public function getRoles(): array { 
 365                 //Get the unique roles list by id 
 366                 return array_unique(array_reduce( 
 367                         //Cast groups as array 
 368                         $this->groups
->toArray(), 
 369                         //Reduce to an array of id => group tuples 
 370                         function ($array, $group) { 
 371                                 $array[$group->getId()] = $group->getRole(); 
 374                         //Init with empty array 
 375                         //XXX: on registration, add each group present in rapsys_user.default.group array to user 
 376                         //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450 
 384         public function getRole(): ?string { 
 386                 $roles = $this->getRoles(); 
 388                 //With roles array empty 
 394                 //Return the role with max id 
 395                 //XXX: should be rewriten if it change in your configuration 
 396                 return $roles[array_reduce( 
 398                         function($cur, $id) { 
 399                                 if ($cur === null || $id > $cur) { 
 411         public function getSalt(): ?string { 
 412                 //No salt required with bcrypt 
 419         public function getUsername(): string { 
 426         public function getUserIdentifier(): string { 
 433         public function eraseCredentials(): void {} 
 438         public function __serialize(): array { 
 455         public function __unserialize(array $data): void { 
 470          * Check if account is activated 
 472          * It was from deprecated AdvancedUserInterface, see if it's used anymore 
 474          * @see vendor/symfony/security-core/User/AdvancedUserInterface.php 
 476         public function isActivated(): bool { 
 477                 return $this->active
; 
 481          * Check if account is disabled 
 483          * It was from deprecated AdvancedUserInterface, see if it's used anymore 
 485          * @see vendor/symfony/security-core/User/AdvancedUserInterface.php 
 487         public function isDisabled(): bool { 
 488                 return $this->disabled
; 
 494         public function preUpdate(PreUpdateEventArgs 
$eventArgs) { 
 495                 //Check that we have an user instance 
 496                 if (($user = $eventArgs->getEntity()) instanceof User
) { 
 498                         $user->setUpdated(new \
DateTime('now')); 
 503          * Returns a recipient name of the user 
 507         public function getRecipientName(): string { 
 508                 //Without forename and surname 
 509                 if (empty($this->forename
) && empty($this->surname
)) { 
 510                         //Return recipient name from mail 
 511                         return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail
))))); 
 514                 //Return recipient name from forename and surname 
 515                 return implode(' ', [$this->forename
, $this->surname
]); 
 519          * Returns a string representation of the user 
 523         public function __toString(): string { 
 524                 return $this->civility
.' '.$this->forename
.' '.$this->surname
;