src/Entity/User.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\ToArray;
  4. use App\Enum\User\Role;
  5. use App\Enum\User\Status;
  6. use App\Repository\UserRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13.  * @ORM\Entity(repositoryClass=UserRepository::class)
  14.  */
  15. class User implements UserInterfacePasswordAuthenticatedUserInterface
  16. {
  17.     use ToArray;
  18.     const USER_FIELD_NAME "user";
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     private $id;
  25.     /**
  26.      * @ORM\Column(type="string", length=180, unique=true)
  27.      */
  28.     private $username;
  29.     /**
  30.      * @ORM\Column(type="json")
  31.      */
  32.     private $roles = [];
  33.     /**
  34.      * @var string The hashed password
  35.      * @ORM\Column(type="string", nullable=true)
  36.      */
  37.     private $password;
  38.     /**
  39.      * @ORM\Column(type="string", length=255)
  40.      */
  41.     private $firstName;
  42.     /**
  43.      * @ORM\Column(type="string", length=255, nullable=true)
  44.      */
  45.     private $lastName;
  46.     /**
  47.      * @var string|null
  48.      */
  49.     private $oldPassword;
  50.     /**
  51.      * @var string|null
  52.      */
  53.     private $newPassword;
  54.     /**
  55.      * @ORM\OneToOne(targetEntity=UserSettings::class, cascade={"persist", "remove"})
  56.      * @ORM\JoinColumn(nullable=true)
  57.      */
  58.     private $settings;
  59.     /**
  60.      * @ORM\Column(type="string", length=30, nullable=true)
  61.      */
  62.     private $telegramUserId;
  63.     /**
  64.      * @ORM\Column(type="string", length=30, nullable=true)
  65.      */
  66.     private $telegramPhone;
  67.     /**
  68.      * @ORM\Column(type="integer", nullable=true)
  69.      */
  70.     private $sortId;
  71.     /**
  72.      * @ORM\Column(type="enum", options={"values"="user_enum"})
  73.      */
  74.     private $status Status::STATUS_ACTIVE;
  75.     /**
  76.      * @ORM\Column(type="datetime", nullable=true)
  77.      */
  78.     private $createdAt;
  79.     /**
  80.      * @ORM\OneToMany(targetEntity=Device::class, mappedBy="user")
  81.      */
  82.     private $devices;
  83.     /**
  84.      * @ORM\OneToMany(targetEntity=Member::class, mappedBy="user")
  85.      */
  86.     private $members;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity=Contact::class, mappedBy="owner", orphanRemoval=true)
  89.      */
  90.     private $contacts;
  91.     /**
  92.      * @ORM\Column(type="string", length=50, nullable=true)
  93.      */
  94.     private $phone;
  95.     /**
  96.      * @ORM\Column(type="string", length=100, nullable=true)
  97.      */
  98.     private $patronymic;
  99.     public function __construct()
  100.     {
  101.         $this->createdAt = new \DateTime();
  102.         $this->devices = new ArrayCollection();
  103.         $this->members = new ArrayCollection();
  104.         $this->contacts = new ArrayCollection();
  105.     }
  106.     public function getId(): ?int
  107.     {
  108.         return $this->id;
  109.     }
  110.     /**
  111.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  112.      */
  113.     public function getUsername(): string
  114.     {
  115.         return (string) $this->username;
  116.     }
  117.     public function setUsername(string $username): self
  118.     {
  119.         $this->username $username;
  120.         return $this;
  121.     }
  122.     /**
  123.      * A visual identifier that represents this user.
  124.      *
  125.      * @see UserInterface
  126.      */
  127.     public function getUserIdentifier(): string
  128.     {
  129.         return (string) $this->username;
  130.     }
  131.     /**
  132.      * @see UserInterface
  133.      */
  134.     public function getRoles(): array
  135.     {
  136.         $roles $this->roles;
  137.         // guarantee every user at least has ROLE_USER
  138.         // $roles[] = 'ROLE_USER';
  139.         return array_unique($roles);
  140.     }
  141.     public function getRolesData(): array
  142.     {
  143.         $rolesData = [];
  144.         foreach ($this->getRoles() as $role) {
  145.             $rolesData[] = [
  146.                 'value' => $role,
  147.                 'text' => Role::getText($role),
  148.             ];
  149.         }
  150.         return $rolesData;
  151.     }
  152.     public function setRoles(array $roles): self
  153.     {
  154.         $this->roles $roles;
  155.         return $this;
  156.     }
  157.     public function isAdmin(): bool
  158.     {
  159.         return in_array(Role::ROLE_ADMIN$this->getRoles(), true);
  160.     }
  161.     public function isModerator(): bool
  162.     {
  163.         return in_array(Role::ROLE_MODERATOR$this->getRoles(), true);
  164.     }
  165.     public function isDeveloper(): bool
  166.     {
  167.         return in_array(Role::ROLE_DEVELOPER$this->getRoles(), true);
  168.     }
  169.     /**
  170.      * @see PasswordAuthenticatedUserInterface
  171.      */
  172.     public function getPassword(): ?string
  173.     {
  174.         return $this->password;
  175.     }
  176.     public function setPassword(?string $password): self
  177.     {
  178.         $this->password $password;
  179.         return $this;
  180.     }
  181.     public function getOldPassword(): ?string
  182.     {
  183.         return $this->oldPassword;
  184.     }
  185.     public function setOldPassword(string $oldPassword): self
  186.     {
  187.         $this->oldPassword $oldPassword;
  188.         return $this;
  189.     }
  190.     public function getNewPassword(): ?string
  191.     {
  192.         return $this->newPassword;
  193.     }
  194.     public function setNewPassword(string $newPassword): self
  195.     {
  196.         $this->newPassword $newPassword;
  197.         return $this;
  198.     }
  199.     /**
  200.      * Returning a salt is only needed, if you are not using a modern
  201.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  202.      *
  203.      * @see UserInterface
  204.      */
  205.     public function getSalt(): ?string
  206.     {
  207.         return null;
  208.     }
  209.     /**
  210.      * @see UserInterface
  211.      */
  212.     public function eraseCredentials()
  213.     {
  214.         // If you store any temporary, sensitive data on the user, clear it here
  215.         // $this->plainPassword = null;
  216.     }
  217.     public function getFirstName(): ?string
  218.     {
  219.         return $this->firstName;
  220.     }
  221.     public function setFirstName(string $firstName): self
  222.     {
  223.         $this->firstName $firstName;
  224.         return $this;
  225.     }
  226.     public function getLastName(): ?string
  227.     {
  228.         return $this->lastName;
  229.     }
  230.     public function setLastName(?string $lastName): self
  231.     {
  232.         $this->lastName $lastName;
  233.         return $this;
  234.     }
  235.     public function getName(): string
  236.     {
  237.         return $this->getFirstName()
  238.             . ($this->getPatronymic() ? ' ' $this->getPatronymic() : "")
  239.             . ' ' $this->getLastName();
  240.     }
  241.     public function getSettings(): ?UserSettings
  242.     {
  243.         return $this->settings;
  244.     }
  245.     public function setSettings(UserSettings $settings): self
  246.     {
  247.         $this->settings $settings;
  248.         return $this;
  249.     }
  250.     public function getTelegramUserId(): ?string
  251.     {
  252.         return $this->telegramUserId;
  253.     }
  254.     public function setTelegramUserId(?string $telegramUserId): self
  255.     {
  256.         $this->telegramUserId $telegramUserId;
  257.         return $this;
  258.     }
  259.     public function getTelegramPhone(): ?string
  260.     {
  261.         return $this->telegramPhone;
  262.     }
  263.     public function setTelegramPhone(?string $telegramPhone): self
  264.     {
  265.         $this->telegramPhone $telegramPhone;
  266.         return $this;
  267.     }
  268.     public function getSortId(): ?int
  269.     {
  270.         return $this->sortId;
  271.     }
  272.     public function setSortId(?int $sortId): self
  273.     {
  274.         $this->sortId $sortId;
  275.         return $this;
  276.     }
  277.     public function getStatus()
  278.     {
  279.         return $this->status;
  280.     }
  281.     public function setStatus($status): self
  282.     {
  283.         $this->status $status;
  284.         return $this;
  285.     }
  286.     public function getStatusText(): ?string
  287.     {
  288.         return Status::getText($this->getStatus());
  289.     }
  290.     public function getStatusCssClass($prefix null): ?string
  291.     {
  292.         return Status::getCssClass($this->getStatus(), $prefix);
  293.     }
  294.     public function getCreatedAt(): ?\DateTimeInterface
  295.     {
  296.         return $this->createdAt;
  297.     }
  298.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  299.     {
  300.         $this->createdAt $createdAt;
  301.         return $this;
  302.     }
  303.     /**
  304.      * @return Collection<int, Device>
  305.      */
  306.     public function getDevices(): Collection
  307.     {
  308.         return $this->devices;
  309.     }
  310.     public function addDevice(Device $device): self
  311.     {
  312.         if (!$this->devices->contains($device)) {
  313.             $this->devices[] = $device;
  314.             $device->setUser($this);
  315.         }
  316.         return $this;
  317.     }
  318.     public function removeDevice(Device $device): self
  319.     {
  320.         if ($this->devices->removeElement($device)) {
  321.             // set the owning side to null (unless already changed)
  322.             if ($device->getUser() === $this) {
  323.                 $device->setUser(null);
  324.             }
  325.         }
  326.         return $this;
  327.     }
  328.     /**
  329.      * @return Collection<int, Member>
  330.      */
  331.     public function getMembers(): Collection
  332.     {
  333.         return $this->members;
  334.     }
  335.     public function addMember(Member $member): self
  336.     {
  337.         if (!$this->members->contains($member)) {
  338.             $this->members[] = $member;
  339.             $member->setUser($this);
  340.         }
  341.         return $this;
  342.     }
  343.     public function removeMember(Member $member): self
  344.     {
  345.         if ($this->members->removeElement($member)) {
  346.             // set the owning side to null (unless already changed)
  347.             if ($member->getUser() === $this) {
  348.                 $member->setUser(null);
  349.             }
  350.         }
  351.         return $this;
  352.     }
  353.     /**
  354.      * @return Collection<int, Contact>
  355.      */
  356.     public function getContacts(): Collection
  357.     {
  358.         return $this->contacts;
  359.     }
  360.     public function addContact(Contact $contact): self
  361.     {
  362.         if (!$this->contacts->contains($contact)) {
  363.             $this->contacts[] = $contact;
  364.             $contact->setOwner($this);
  365.         }
  366.         return $this;
  367.     }
  368.     public function removeContact(Contact $contact): self
  369.     {
  370.         if ($this->contacts->removeElement($contact)) {
  371.             // set the owning side to null (unless already changed)
  372.             if ($contact->getOwner() === $this) {
  373.                 $contact->setOwner(null);
  374.             }
  375.         }
  376.         return $this;
  377.     }
  378.     public function getPhone(): ?string
  379.     {
  380.         return $this->phone;
  381.     }
  382.     public function setPhone(?string $phone): self
  383.     {
  384.         $this->phone $phone;
  385.         return $this;
  386.     }
  387.     public function getPatronymic(): ?string
  388.     {
  389.         return $this->patronymic;
  390.     }
  391.     public function setPatronymic(?string $patronymic): self
  392.     {
  393.         $this->patronymic $patronymic;
  394.         return $this;
  395.     }
  396. }