src/Entity/User.php line 15

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