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
, \Serializable
{
77 * @var ArrayCollection
84 * @param string $mail The user mail
86 public function __construct(string $mail) {
88 if (!empty($this->mail
= $mail)) {
89 $this->password
= $mail;
95 $this->forename
= null;
96 $this->surname
= null;
97 $this->active
= false;
98 $this->disabled
= false;
99 $this->created
= new \
DateTime('now');
100 $this->updated
= new \
DateTime('now');
103 $this->groups
= new ArrayCollection();
111 public function getId(): int {
118 * @param string $mail
122 public function setMail(string $mail): User
{
124 if (!empty($this->mail
= $mail)) {
126 if (empty($this->password
)) {
127 //Set mail as password
128 $this->password
= $mail;
140 public function getMail(): string {
147 * @param ?string $forename
151 public function setForename(?string $forename): User
{
152 $this->forename
= $forename;
162 public function getForename(): ?string {
163 return $this->forename
;
169 * @param ?string $surname
173 public function setSurname(?string $surname): User
{
174 $this->surname
= $surname;
184 public function getSurname(): ?string {
185 return $this->surname
;
191 * @param string $password
195 public function setPassword(string $password): User
{
196 $this->password
= $password;
208 public function getPassword(): ?string {
209 return $this->password
;
215 * @param bool $active
219 public function setActive(bool $active): User
{
220 $this->active
= $active;
230 public function getActive(): bool {
231 return $this->active
;
237 * @param bool $disabled
241 public function setDisabled(bool $disabled): User
{
242 $this->disabled
= $disabled;
252 public function getDisabled(): bool {
253 return $this->disabled
;
259 * @param \DateTime $created
263 public function setCreated(\DateTime
$created): User
{
264 $this->created
= $created;
274 public function getCreated(): \DateTime
{
275 return $this->created
;
281 * @param \DateTime $updated
285 public function setUpdated(\DateTime
$updated): User
{
286 $this->updated
= $updated;
296 public function getUpdated(): \DateTime
{
297 return $this->updated
;
303 public function setCivility(Civility
$civility): User
{
304 $this->civility
= $civility;
312 public function getCivility(): ?Civility
{
313 return $this->civility
;
319 * @param Group $group
323 public function addGroup(Group
$group) {
324 $this->groups
[] = $group;
332 * @param Group $group
334 public function removeGroup(Group
$group) {
335 $this->groups
->removeElement($group);
341 * @return ArrayCollection
343 public function getGroups(): ArrayCollection
{
344 return $this->groups
;
350 public function getRoles(): array {
351 //Get the unique roles list by id
352 return array_unique(array_reduce(
353 //Cast groups as array
354 $this->groups
->toArray(),
355 //Reduce to an array of id => group tuples
356 function ($array, $group) {
357 $array[$group->getId()] = $group->getRole();
360 //Init with empty array
361 //XXX: on registration, add each group present in rapsys_user.default.group array to user
362 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
370 public function getRole(): ?string {
372 $roles = $this->getRoles();
374 //With roles array empty
380 //Return the role with max id
381 //XXX: should be rewriten if it change in your configuration
382 return $roles[array_reduce(
384 function($cur, $id) {
385 if ($cur === null || $id > $cur) {
397 public function getSalt(): ?string {
398 //No salt required with bcrypt
405 public function getUsername(): string {
412 public function getUserIdentifier(): string {
419 public function eraseCredentials(): void {}
424 public function serialize(): string {
441 public function unserialize($serialized) {
452 ) = unserialize($serialized);
456 * Check if account is activated
458 * It was from deprecated AdvancedUserInterface, see if it's used anymore
460 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
462 public function isActivated(): bool {
463 return $this->active
;
467 * Check if account is disabled
469 * It was from deprecated AdvancedUserInterface, see if it's used anymore
471 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
473 public function isDisabled(): bool {
474 return $this->disabled
;
480 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
481 //Check that we have an user instance
482 if (($user = $eventArgs->getEntity()) instanceof User
) {
484 $user->setUpdated(new \
DateTime('now'));
489 * Returns a recipient name of the user
493 public function getRecipientName(): string {
494 //Without forename and surname
495 if (empty($this->forename
) && empty($this->surname
)) {
496 //Return recipient name from mail
497 return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail
)))));
500 //Return recipient name from forename and surname
501 return implode(' ', [$this->forename
, $this->surname
]);
505 * Returns a string representation of the user
509 public function __toString(): string {
510 return $this->civility
.' '.$this->forename
.' '.$this->surname
;