src/Core/Model/User.php line 32

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2019 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2019 TECLA Consulting Group oü
  16.  */
  17. namespace App\Core\Model;
  18. use App\Contact\Model\ContactInterface;
  19. use App\Core\Model\Identity\UserIdInterface;
  20. use JetBrains\PhpStorm\Pure;
  21. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  22. use Symfony\Component\Validator\Constraints as Assert;
  23. #[UniqueEntity(
  24.     fields'username',
  25.     message'username.not_unique',
  26.     ignoreNullfalse,
  27. )]
  28. class User implements UserInterface
  29. {
  30.     protected UserIdInterface $id;
  31.     protected \DateTimeImmutable $createdAt;
  32.     protected ?\DateTimeImmutable $updatedAt;
  33.     #[Assert\NotBlank(
  34.         message'username.blank',
  35.     )]
  36.     #[Assert\Email(
  37.         message'username.invalid',
  38.     )]
  39.     protected ?string $username;
  40.     protected ?string $password;
  41.     protected ?string $legacyPassword;
  42.     protected ?string $confirmationToken;
  43.     protected ?string $passwordResetToken;
  44.     #[Assert\Valid]
  45.     protected ?ContactInterface $contact;
  46.     protected ?ContactInterface $invitedBy;
  47.     #[Assert\NotBlank(
  48.         message'password.blank',
  49.     )]
  50.     #[Assert\Length(
  51.         min8,
  52.         minMessage'password.minimum_length',
  53.     )]
  54.     protected ?string $plainPassword null;
  55.     #[Assert\NotNull(
  56.         message'locale.blank',
  57.     )]
  58.     protected ?string $locale;
  59.     protected ?\DateTimeImmutable $passwordResetTokenUpdatedAt;
  60.     public function __construct(UserIdInterface $id)
  61.     {
  62.         $this->id $id;
  63.         $this->username null;
  64.         $this->password null;
  65.         $this->legacyPassword null;
  66.         $this->confirmationToken null;
  67.         $this->passwordResetToken null;
  68.         $this->contact null;
  69.         $this->invitedBy null;
  70.         $this->locale null;
  71.         $this->passwordResetTokenUpdatedAt null;
  72.     }
  73.     public function getId(): UserIdInterface
  74.     {
  75.         return $this->id;
  76.     }
  77.     public function getCreatedAt(): \DateTimeImmutable
  78.     {
  79.         return $this->createdAt;
  80.     }
  81.     public function setCreatedAt(\DateTimeImmutable $createdAt): void
  82.     {
  83.         $this->createdAt $createdAt;
  84.         $this->setUpdatedAt($createdAt);
  85.     }
  86.     public function getUpdatedAt(): \DateTimeImmutable
  87.     {
  88.         return $this->updatedAt;
  89.     }
  90.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): void
  91.     {
  92.         $this->updatedAt $updatedAt;
  93.     }
  94.     #[Pure]
  95.     public function getUsername(): ?string
  96.     {
  97.         return $this->getUserIdentifier();
  98.     }
  99.     public function setUsername(?string $username): void
  100.     {
  101.         $this->username $username;
  102.     }
  103.     public function getUserIdentifier(): string
  104.     {
  105.         return $this->username ?? '';
  106.     }
  107.     /**
  108.      * Sets the username used to authenticate the user.
  109.      */
  110.     public function setUserIdentifier(string $userIdentifier): void
  111.     {
  112.         $this->username $userIdentifier;
  113.     }
  114.     #[Pure]
  115.     public function getEmail(): string
  116.     {
  117.         return $this->getUserIdentifier();
  118.     }
  119.     /**
  120.      * Sets the e-mail.
  121.      */
  122.     public function setEmail(string $email): void
  123.     {
  124.         $this->setUserIdentifier($email);
  125.     }
  126.     public function getPassword(): ?string
  127.     {
  128.         return $this->password;
  129.     }
  130.     /**
  131.      * Sets the hashed password used to authenticate the user.
  132.      */
  133.     public function setPassword(string $password): void
  134.     {
  135.         $this->password $password;
  136.     }
  137.     public function getLegacyPassword(): ?string
  138.     {
  139.         return $this->legacyPassword;
  140.     }
  141.     public function setLegacyPassword(?string $legacyPassword): void
  142.     {
  143.         $this->legacyPassword $legacyPassword;
  144.     }
  145.     /**
  146.      * Gets the plain password.
  147.      *
  148.      * @return string
  149.      */
  150.     public function getPlainPassword(): ?string
  151.     {
  152.         return $this->plainPassword;
  153.     }
  154.     /**
  155.      * Sets the plain password.
  156.      */
  157.     public function setPlainPassword(string $plainPassword): void
  158.     {
  159.         $this->plainPassword $plainPassword;
  160.     }
  161.     /**
  162.      * Gets the confirmation token.
  163.      */
  164.     public function getConfirmationToken(): ?string
  165.     {
  166.         return $this->confirmationToken;
  167.     }
  168.     /**
  169.      * Sets the confirmation token.
  170.      *
  171.      * @param string $confirmationToken
  172.      */
  173.     public function setConfirmationToken(?string $confirmationToken): void
  174.     {
  175.         $this->confirmationToken $confirmationToken;
  176.     }
  177.     /**
  178.      * Returns the Contact.
  179.      */
  180.     public function getContact(): ?ContactInterface
  181.     {
  182.         return $this->contact;
  183.     }
  184.     /**
  185.      * Sets the Contact.
  186.      */
  187.     public function setContact(ContactInterface $contact): void
  188.     {
  189.         $this->contact $contact;
  190.         $contact->setUser($this);
  191.     }
  192.     /**
  193.      * Returns the Contact what invited user.
  194.      */
  195.     public function getInvitedBy(): ?ContactInterface
  196.     {
  197.         return $this->invitedBy;
  198.     }
  199.     /**
  200.      * Sets the Contact what invited user.
  201.      */
  202.     public function setInvitedBy(ContactInterface $invitedBy): void
  203.     {
  204.         $this->invitedBy $invitedBy;
  205.     }
  206.     /**
  207.      * Returns the roles granted to the user.
  208.      *
  209.      * @return string[] The user roles
  210.      */
  211.     public function getRoles(): array
  212.     {
  213.         return ['ROLE_USER'];
  214.     }
  215.     /**
  216.      * Returns the salt that was originally used to encode the password.
  217.      *
  218.      * @return string|null The salt
  219.      */
  220.     public function getSalt(): ?string
  221.     {
  222.         return 'salt';
  223.     }
  224.     /**
  225.      * Removes sensitive data from the user.
  226.      */
  227.     public function eraseCredentials()
  228.     {
  229.         $this->plainPassword null;
  230.     }
  231.     public function getLocale(): ?string
  232.     {
  233.         return $this->locale;
  234.     }
  235.     public function setLocale(string $locale): void
  236.     {
  237.         $this->locale $locale;
  238.     }
  239.     public function setPasswordResetToken(?string $passwordResetToken): void
  240.     {
  241.         $this->passwordResetToken $passwordResetToken;
  242.         if (null === $passwordResetToken) {
  243.             $this->setPasswordResetTokenUpdatedAt(null);
  244.         } else {
  245.             $this->setPasswordResetTokenUpdatedAt(new \DateTimeImmutable());
  246.         }
  247.     }
  248.     public function getPasswordResetToken(): ?string
  249.     {
  250.         return $this->passwordResetToken;
  251.     }
  252.     public function getPasswordResetTokenUpdatedAt(): ?\DateTimeImmutable
  253.     {
  254.         return $this->passwordResetTokenUpdatedAt;
  255.     }
  256.     public function setPasswordResetTokenUpdatedAt(?\DateTimeImmutable $passwordResetTokenUpdatedAt): void
  257.     {
  258.         $this->passwordResetTokenUpdatedAt $passwordResetTokenUpdatedAt;
  259.     }
  260.     public function __serialize(): array
  261.     {
  262.         return [
  263.             'id' => $this->id,
  264.             'username' => $this->username,
  265.             'password' => $this->password,
  266.             'legacyPassword' => $this->legacyPassword,
  267.         ];
  268.     }
  269.     public function __unserialize(array $data): void
  270.     {
  271.         $this->id $data['id'];
  272.         $this->username $data['username'];
  273.         $this->password $data['password'];
  274.         $this->legacyPassword $data['legacyPassword'];
  275.     }
  276. }