src/Authentication/Entity/User.php line 22

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Authentication\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Serializable;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
  10. /**
  11.  * @ORM\Entity(repositoryClass="App\Authentication\Repository\UserRepository")
  12.  * @ORM\Table(name="users")
  13.  * @ORM\HasLifecycleCallbacks()
  14.  * @UniqueEntity(
  15.  *    fields={"email"},
  16.  *    message="error.user.email_used"
  17.  * )
  18.  */
  19. class User implements UserInterfaceLegacyPasswordAuthenticatedUserInterfaceSerializable
  20. {
  21.     /**
  22.      * @ORM\Column(type="integer")
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue(strategy="AUTO")
  25.      */
  26.     private int $id;
  27.     /**
  28.      * @ORM\Column(type="string", length=100, nullable=true)
  29.      */
  30.     private ?string $username null;
  31.     /**
  32.      * @ORM\Column(type="string", length=255)
  33.      * @Assert\NotBlank()
  34.      */
  35.     private string $password '';
  36.     /**
  37.      * @ORM\Column(name="salt", type="string", length=255)
  38.      */
  39.     private string $salt '';
  40.     /**
  41.      * @ORM\Column(type="string", length=60, nullable=true, unique=true)
  42.      */
  43.     #[Assert\Email(message'El email \'{{ value }}\' no es un email vĂ¡lido')]
  44.     private ?string $email null;
  45.     /**
  46.      * @ORM\Column(type="string", length=100)
  47.      * @Assert\NotBlank()
  48.      */
  49.     private string $name '';
  50.     /**
  51.      * @ORM\Column(type="string", length=255, options={"default" = "ROLE_USER"})
  52.      */
  53.     private string $roles 'ROLE_USER';
  54.     /**
  55.      * @ORM\Column(type="string", length=255, nullable=true)
  56.      */
  57.     private ?string $verificationCode null;
  58.     /**
  59.      * @ORM\Column(type="boolean")
  60.      */
  61.     private bool $isVerified false;
  62.     /**
  63.      * @ORM\Column(name="is_active", type="boolean")
  64.      */
  65.     private bool $isActive false;
  66.     public function __construct()
  67.     {
  68.         $this->roles 'ROLE_USER';
  69.     }
  70.     public function isEqualTo(UserInterface $user): bool
  71.     {
  72.         /** @var User $user */
  73.         $rolesDiff array_diff($this->getRoles(), $user->getRoles());
  74.         return $this->getId() === $user->getId() && $rolesDiff === [];
  75.     }
  76.     public function getId(): int
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function setId(int $id): void
  81.     {
  82.         $this->id $id;
  83.     }
  84.     public function getUserIdentifier(): string
  85.     {
  86.         if (!$this->username || empty($this->username)) {
  87.             return $this->email;
  88.         }
  89.         return $this->username;
  90.     }
  91.     public function getUsername(): ?string
  92.     {
  93.         return $this->username;
  94.     }
  95.     
  96.     public function setUsername(string $username): void
  97.     {
  98.         $this->username $username;
  99.     }
  100.     public function getPassword(): string
  101.     {
  102.         return $this->password;
  103.     }
  104.     public function setPassword(string $password): void
  105.     {
  106.         $this->password $password;
  107.     }
  108.     public function getSalt(): string
  109.     {
  110.         return $this->salt;
  111.     }
  112.     public function setSalt(string $salt): void
  113.     {
  114.         $this->salt $salt;
  115.     }
  116.     public function getEmail(): string
  117.     {
  118.         return $this->email $this->email '';
  119.     }
  120.     public function setEmail(string $email): void
  121.     {
  122.         $this->email $email;
  123.     }
  124.     public function getName(): string
  125.     {
  126.         return $this->name;
  127.     }
  128.     public function setName(string $name): void
  129.     {
  130.         $this->name $name;
  131.     }
  132.     /**
  133.      * @return string[]
  134.      */
  135.     public function getRoles(): array
  136.     {
  137.         return explode(','$this->roles);
  138.     }
  139.     public function setRoles(array $roles): void
  140.     {
  141.         $this->roles '';
  142.         foreach ($roles as $role) {
  143.             if ($this->roles !== '') {
  144.                 $this->roles .= ',';
  145.             }
  146.             $this->roles .= $role;
  147.         }
  148.     }
  149.     public function setVerificationCode(string $code): void
  150.     {
  151.         $this->verificationCode $code;
  152.     }
  153.     public function getVerificationCode(): ?string
  154.     {
  155.         return $this->verificationCode;
  156.     }
  157.     public function setIsVerified(bool $verified): void
  158.     {
  159.         $this->isVerified $verified;
  160.     }
  161.     public function getIsVerified(): bool
  162.     {
  163.         return $this->isVerified;
  164.     }
  165.     public function setIsActive(bool $active): void
  166.     {
  167.         $this->isActive $active;
  168.     }
  169.     public function getIsActive(): bool
  170.     {
  171.         return $this->isActive;
  172.     }
  173.     public function eraseCredentials(): void
  174.     {
  175.     }
  176.     /**
  177.      * @see \Serializable::serialize()
  178.      */
  179.     public function serialize(): string
  180.     {
  181.         return serialize([$this->id$this->username$this->email$this->password$this->salt]);
  182.     }
  183.     /**
  184.      * @see \Serializable::unserialize()
  185.      */
  186.     public function unserialize($serialized): void
  187.     {
  188.         [
  189.             $this->id,
  190.             $this->username,
  191.             $this->email,
  192.             $this->password,
  193.             $this->salt
  194.         ] = unserialize($serialized);
  195.     }
  196. }