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\Collection
;
15 use Doctrine\Common\Collections\ArrayCollection
;
16 use Doctrine\ORM\Event\PreUpdateEventArgs
;
17 use Symfony\Component\Security\Core\User\UserInterface
;
18 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
;
20 use Rapsys\UserBundle\Entity\Civility
;
21 use Rapsys\UserBundle\Entity\Group
;
26 class User
implements UserInterface
, PasswordAuthenticatedUserInterface
{
35 protected string $mail;
40 protected string $password;
45 protected ?string $forename;
50 protected ?string $surname;
55 protected bool $active;
60 protected bool $disabled;
65 protected \DateTime
$created;
70 protected \DateTime
$updated;
75 protected ?Civility
$civility;
78 * @var Doctrine\Common\Collections\Collection
80 protected Collection
$groups;
85 * @param string $mail The user mail
86 * @param string $password The user password
87 * @param ?Civility $civility The user civility
88 * @param ?string $forename The user forename
89 * @param ?string $surname The user surname
90 * @param bool $active The user active
91 * @param bool $disabled The user disabled
93 public function __construct(string $mail, string $password, ?Civility
$civility = null, ?string $forename = null, ?string $surname = null, bool $active = false, bool $disabled = false) {
96 $this->password
= $password;
97 $this->civility
= $civility;
98 $this->forename
= $forename;
99 $this->surname
= $surname;
100 $this->active
= $active;
101 $this->disabled
= $disabled;
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
{
136 public function getMail(): string {
143 * @param ?string $forename
147 public function setForename(?string $forename): User
{
148 $this->forename
= $forename;
158 public function getForename(): ?string {
159 return $this->forename
;
165 * @param ?string $surname
169 public function setSurname(?string $surname): User
{
170 $this->surname
= $surname;
180 public function getSurname(): ?string {
181 return $this->surname
;
187 * @param string $password
191 public function setPassword(string $password): User
{
193 $this->password
= $password;
205 public function getPassword(): string {
206 return $this->password
;
212 * @param bool $active
216 public function setActive(bool $active): User
{
217 $this->active
= $active;
227 public function getActive(): bool {
228 return $this->active
;
234 * @param bool $disabled
238 public function setDisabled(bool $disabled): User
{
239 $this->disabled
= $disabled;
249 public function getDisabled(): bool {
250 return $this->disabled
;
256 * @param \DateTime $created
260 public function setCreated(\DateTime
$created): User
{
261 $this->created
= $created;
271 public function getCreated(): \DateTime
{
272 return $this->created
;
278 * @param \DateTime $updated
282 public function setUpdated(\DateTime
$updated): User
{
283 $this->updated
= $updated;
293 public function getUpdated(): \DateTime
{
294 return $this->updated
;
300 public function setCivility(?Civility
$civility = null): User
{
301 $this->civility
= $civility;
309 public function getCivility(): ?Civility
{
310 return $this->civility
;
316 * @param Group $group
320 public function addGroup(Group
$group): User
{
321 $this->groups
[] = $group;
329 * @param Group $group
331 * @return Doctrine\Common\Collections\Collection
333 public function removeGroup(Group
$group): Collection
{
334 return $this->groups
->removeElement($group);
340 * @return Doctrine\Common\Collections\Collection
342 public function getGroups(): Collection
{
343 return $this->groups
;
349 public function getRoles(): array {
350 //Get the unique roles list by id
351 return array_unique(array_reduce(
352 //Cast groups as array
353 $this->groups
->toArray(),
354 //Reduce to an array of id => group tuples
355 function ($array, $group) {
356 $array[$group->getId()] = $group->getRole();
359 //Init with empty array
360 //XXX: on registration, add each group present in rapsys_user.default.group array to user
361 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
369 public function getRole(): ?string {
371 $roles = $this->getRoles();
373 //With roles array empty
379 //Return the role with max id
380 //XXX: should be rewriten if it change in your configuration
381 return $roles[array_reduce(
383 function($cur, $id) {
384 if ($cur === null || $id > $cur) {
396 public function getSalt(): ?string {
397 //No salt required with bcrypt
404 public function getUsername(): string {
411 public function getUserIdentifier(): string {
418 public function eraseCredentials(): void {}
423 public function __serialize(): array {
440 public function __unserialize(array $data): void {
455 * Check if account is activated
457 * It was from deprecated AdvancedUserInterface, see if it's used anymore
459 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
461 public function isActivated(): bool {
462 return $this->active
;
466 * Check if account is disabled
468 * It was from deprecated AdvancedUserInterface, see if it's used anymore
470 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
472 public function isDisabled(): bool {
473 return $this->disabled
;
479 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
480 //Check that we have an user instance
481 if (($user = $eventArgs->getEntity()) instanceof User
) {
483 $user->setUpdated(new \
DateTime('now'));
488 * Returns a recipient name of the user
492 public function getRecipientName(): string {
493 //Without forename and surname
494 if (empty($this->forename
) && empty($this->surname
)) {
495 //Return recipient name from mail
496 return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail
)))));
499 //Return recipient name from forename and surname
500 return implode(' ', [$this->forename
, $this->surname
]);
504 * Returns a string representation of the user
508 public function __toString(): string {
509 return $this->civility
.' '.$this->forename
.' '.$this->surname
;