src/Entity/User.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\User\Role;
  4. use App\Repository\UserRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. /**
  9.  * @ORM\Entity(repositoryClass=UserRepository::class)
  10.  */
  11. class User implements UserInterfacePasswordAuthenticatedUserInterface
  12. {
  13.     const USER_FIELD_NAME "user";
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      */
  19.     private $id;
  20.     /**
  21.      * @ORM\Column(type="string", length=180, unique=true)
  22.      */
  23.     private $username;
  24.     /**
  25.      * @ORM\Column(type="json")
  26.      */
  27.     private $roles = [];
  28.     /**
  29.      * @var string The hashed password
  30.      * @ORM\Column(type="string")
  31.      */
  32.     private $password;
  33.     /**
  34.      * @ORM\Column(type="string", length=255)
  35.      */
  36.     private $firstName;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $lastName;
  41.     /**
  42.      * @var string|null
  43.      */
  44.     private $oldPassword;
  45.     /**
  46.      * @var string|null
  47.      */
  48.     private $newPassword;
  49.     /**
  50.      * @ORM\OneToOne(targetEntity=UserSettings::class, cascade={"persist", "remove"})
  51.      * @ORM\JoinColumn(nullable=false)
  52.      */
  53.     private $settings;
  54.     /**
  55.      * @ORM\Column(type="string", length=30, nullable=true)
  56.      */
  57.     private $telegramUserId;
  58.     /**
  59.      * @ORM\Column(type="string", length=30, nullable=true)
  60.      */
  61.     private $telegramPhone;
  62.     /**
  63.      * @ORM\Column(type="integer", nullable=true)
  64.      */
  65.     private $sortId;
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     /**
  71.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  72.      */
  73.     public function getUsername(): string
  74.     {
  75.         return (string) $this->username;
  76.     }
  77.     public function setUsername(string $username): self
  78.     {
  79.         $this->username $username;
  80.         return $this;
  81.     }
  82.     /**
  83.      * A visual identifier that represents this user.
  84.      *
  85.      * @see UserInterface
  86.      */
  87.     public function getUserIdentifier(): string
  88.     {
  89.         return (string) $this->username;
  90.     }
  91.     /**
  92.      * @see UserInterface
  93.      */
  94.     public function getRoles(): array
  95.     {
  96.         $roles $this->roles;
  97.         // guarantee every user at least has ROLE_USER
  98.         // $roles[] = 'ROLE_USER';
  99.         return array_unique($roles);
  100.     }
  101.     public function getRolesData(): array
  102.     {
  103.         $rolesData = [];
  104.         foreach ($this->getRoles() as $role) {
  105.             $rolesData[] = [
  106.                 'value' => $role,
  107.                 'text' => Role::getText($role),
  108.             ];
  109.         }
  110.         return $rolesData;
  111.     }
  112.     public function setRoles(array $roles): self
  113.     {
  114.         $this->roles $roles;
  115.         return $this;
  116.     }
  117.     public function isAdmin(): bool
  118.     {
  119.         return in_array(Role::ROLE_ADMIN$this->getRoles(), true);
  120.     }
  121.     public function isModerator(): bool
  122.     {
  123.         return in_array(Role::ROLE_MODERATOR$this->getRoles(), true);
  124.     }
  125.     public function isDeveloper(): bool
  126.     {
  127.         return in_array(Role::ROLE_DEVELOPER$this->getRoles(), true);
  128.     }
  129.     /**
  130.      * @see PasswordAuthenticatedUserInterface
  131.      */
  132.     public function getPassword(): ?string
  133.     {
  134.         return $this->password;
  135.     }
  136.     public function setPassword(?string $password): self
  137.     {
  138.         $this->password $password;
  139.         return $this;
  140.     }
  141.     public function getOldPassword(): ?string
  142.     {
  143.         return $this->oldPassword;
  144.     }
  145.     public function setOldPassword(string $oldPassword): self
  146.     {
  147.         $this->oldPassword $oldPassword;
  148.         return $this;
  149.     }
  150.     public function getNewPassword(): ?string
  151.     {
  152.         return $this->newPassword;
  153.     }
  154.     public function setNewPassword(string $newPassword): self
  155.     {
  156.         $this->newPassword $newPassword;
  157.         return $this;
  158.     }
  159.     /**
  160.      * Returning a salt is only needed, if you are not using a modern
  161.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  162.      *
  163.      * @see UserInterface
  164.      */
  165.     public function getSalt(): ?string
  166.     {
  167.         return null;
  168.     }
  169.     /**
  170.      * @see UserInterface
  171.      */
  172.     public function eraseCredentials()
  173.     {
  174.         // If you store any temporary, sensitive data on the user, clear it here
  175.         // $this->plainPassword = null;
  176.     }
  177.     public function getFirstName(): ?string
  178.     {
  179.         return $this->firstName;
  180.     }
  181.     public function setFirstName(string $firstName): self
  182.     {
  183.         $this->firstName $firstName;
  184.         return $this;
  185.     }
  186.     public function getLastName(): ?string
  187.     {
  188.         return $this->lastName;
  189.     }
  190.     public function setLastName(?string $lastName): self
  191.     {
  192.         $this->lastName $lastName;
  193.         return $this;
  194.     }
  195.     public function getName(): string
  196.     {
  197.         return $this->firstName ' ' $this->lastName;
  198.     }
  199.     public function getSettings(): ?UserSettings
  200.     {
  201.         return $this->settings;
  202.     }
  203.     public function setSettings(UserSettings $settings): self
  204.     {
  205.         $this->settings $settings;
  206.         return $this;
  207.     }
  208.     public function getTelegramUserId(): ?string
  209.     {
  210.         return $this->telegramUserId;
  211.     }
  212.     public function setTelegramUserId(?string $telegramUserId): self
  213.     {
  214.         $this->telegramUserId $telegramUserId;
  215.         return $this;
  216.     }
  217.     public function getTelegramPhone(): ?string
  218.     {
  219.         return $this->telegramPhone;
  220.     }
  221.     public function setTelegramPhone(?string $telegramPhone): self
  222.     {
  223.         $this->telegramPhone $telegramPhone;
  224.         return $this;
  225.     }
  226.     public function getSortId(): ?int
  227.     {
  228.         return $this->sortId;
  229.     }
  230.     public function setSortId(?int $sortId): self
  231.     {
  232.         $this->sortId $sortId;
  233.         return $this;
  234.     }
  235. }