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=false)
  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.     public function __construct()
  96.     {
  97.         $this->createdAt = new \DateTime();
  98.         $this->devices = new ArrayCollection();
  99.         $this->members = new ArrayCollection();
  100.         $this->contacts = new ArrayCollection();
  101.     }
  102.     public function getId(): ?int
  103.     {
  104.         return $this->id;
  105.     }
  106.     /**
  107.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  108.      */
  109.     public function getUsername(): string
  110.     {
  111.         return (string) $this->username;
  112.     }
  113.     public function setUsername(string $username): self
  114.     {
  115.         $this->username $username;
  116.         return $this;
  117.     }
  118.     /**
  119.      * A visual identifier that represents this user.
  120.      *
  121.      * @see UserInterface
  122.      */
  123.     public function getUserIdentifier(): string
  124.     {
  125.         return (string) $this->username;
  126.     }
  127.     /**
  128.      * @see UserInterface
  129.      */
  130.     public function getRoles(): array
  131.     {
  132.         $roles $this->roles;
  133.         // guarantee every user at least has ROLE_USER
  134.         // $roles[] = 'ROLE_USER';
  135.         return array_unique($roles);
  136.     }
  137.     public function getRolesData(): array
  138.     {
  139.         $rolesData = [];
  140.         foreach ($this->getRoles() as $role) {
  141.             $rolesData[] = [
  142.                 'value' => $role,
  143.                 'text' => Role::getText($role),
  144.             ];
  145.         }
  146.         return $rolesData;
  147.     }
  148.     public function setRoles(array $roles): self
  149.     {
  150.         $this->roles $roles;
  151.         return $this;
  152.     }
  153.     public function isAdmin(): bool
  154.     {
  155.         return in_array(Role::ROLE_ADMIN$this->getRoles(), true);
  156.     }
  157.     public function isModerator(): bool
  158.     {
  159.         return in_array(Role::ROLE_MODERATOR$this->getRoles(), true);
  160.     }
  161.     public function isDeveloper(): bool
  162.     {
  163.         return in_array(Role::ROLE_DEVELOPER$this->getRoles(), true);
  164.     }
  165.     /**
  166.      * @see PasswordAuthenticatedUserInterface
  167.      */
  168.     public function getPassword(): ?string
  169.     {
  170.         return $this->password;
  171.     }
  172.     public function setPassword(?string $password): self
  173.     {
  174.         $this->password $password;
  175.         return $this;
  176.     }
  177.     public function getOldPassword(): ?string
  178.     {
  179.         return $this->oldPassword;
  180.     }
  181.     public function setOldPassword(string $oldPassword): self
  182.     {
  183.         $this->oldPassword $oldPassword;
  184.         return $this;
  185.     }
  186.     public function getNewPassword(): ?string
  187.     {
  188.         return $this->newPassword;
  189.     }
  190.     public function setNewPassword(string $newPassword): self
  191.     {
  192.         $this->newPassword $newPassword;
  193.         return $this;
  194.     }
  195.     /**
  196.      * Returning a salt is only needed, if you are not using a modern
  197.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  198.      *
  199.      * @see UserInterface
  200.      */
  201.     public function getSalt(): ?string
  202.     {
  203.         return null;
  204.     }
  205.     /**
  206.      * @see UserInterface
  207.      */
  208.     public function eraseCredentials()
  209.     {
  210.         // If you store any temporary, sensitive data on the user, clear it here
  211.         // $this->plainPassword = null;
  212.     }
  213.     public function getFirstName(): ?string
  214.     {
  215.         return $this->firstName;
  216.     }
  217.     public function setFirstName(string $firstName): self
  218.     {
  219.         $this->firstName $firstName;
  220.         return $this;
  221.     }
  222.     public function getLastName(): ?string
  223.     {
  224.         return $this->lastName;
  225.     }
  226.     public function setLastName(?string $lastName): self
  227.     {
  228.         $this->lastName $lastName;
  229.         return $this;
  230.     }
  231.     public function getName(): string
  232.     {
  233.         return $this->firstName ' ' $this->lastName;
  234.     }
  235.     public function getSettings(): ?UserSettings
  236.     {
  237.         return $this->settings;
  238.     }
  239.     public function setSettings(UserSettings $settings): self
  240.     {
  241.         $this->settings $settings;
  242.         return $this;
  243.     }
  244.     public function getTelegramUserId(): ?string
  245.     {
  246.         return $this->telegramUserId;
  247.     }
  248.     public function setTelegramUserId(?string $telegramUserId): self
  249.     {
  250.         $this->telegramUserId $telegramUserId;
  251.         return $this;
  252.     }
  253.     public function getTelegramPhone(): ?string
  254.     {
  255.         return $this->telegramPhone;
  256.     }
  257.     public function setTelegramPhone(?string $telegramPhone): self
  258.     {
  259.         $this->telegramPhone $telegramPhone;
  260.         return $this;
  261.     }
  262.     public function getSortId(): ?int
  263.     {
  264.         return $this->sortId;
  265.     }
  266.     public function setSortId(?int $sortId): self
  267.     {
  268.         $this->sortId $sortId;
  269.         return $this;
  270.     }
  271.     public function getStatus()
  272.     {
  273.         return $this->status;
  274.     }
  275.     public function setStatus($status): self
  276.     {
  277.         $this->status $status;
  278.         return $this;
  279.     }
  280.     public function getStatusText(): ?string
  281.     {
  282.         return Status::getText($this->getStatus());
  283.     }
  284.     public function getStatusCssClass($prefix null): ?string
  285.     {
  286.         return Status::getCssClass($this->getStatus(), $prefix);
  287.     }
  288.     public function getCreatedAt(): ?\DateTimeInterface
  289.     {
  290.         return $this->createdAt;
  291.     }
  292.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  293.     {
  294.         $this->createdAt $createdAt;
  295.         return $this;
  296.     }
  297.     /**
  298.      * @return Collection<int, Device>
  299.      */
  300.     public function getDevices(): Collection
  301.     {
  302.         return $this->devices;
  303.     }
  304.     public function addDevice(Device $device): self
  305.     {
  306.         if (!$this->devices->contains($device)) {
  307.             $this->devices[] = $device;
  308.             $device->setUser($this);
  309.         }
  310.         return $this;
  311.     }
  312.     public function removeDevice(Device $device): self
  313.     {
  314.         if ($this->devices->removeElement($device)) {
  315.             // set the owning side to null (unless already changed)
  316.             if ($device->getUser() === $this) {
  317.                 $device->setUser(null);
  318.             }
  319.         }
  320.         return $this;
  321.     }
  322.     /**
  323.      * @return Collection<int, Member>
  324.      */
  325.     public function getMembers(): Collection
  326.     {
  327.         return $this->members;
  328.     }
  329.     public function addMember(Member $member): self
  330.     {
  331.         if (!$this->members->contains($member)) {
  332.             $this->members[] = $member;
  333.             $member->setUser($this);
  334.         }
  335.         return $this;
  336.     }
  337.     public function removeMember(Member $member): self
  338.     {
  339.         if ($this->members->removeElement($member)) {
  340.             // set the owning side to null (unless already changed)
  341.             if ($member->getUser() === $this) {
  342.                 $member->setUser(null);
  343.             }
  344.         }
  345.         return $this;
  346.     }
  347.     /**
  348.      * @return Collection<int, Contact>
  349.      */
  350.     public function getContacts(): Collection
  351.     {
  352.         return $this->contacts;
  353.     }
  354.     public function addContact(Contact $contact): self
  355.     {
  356.         if (!$this->contacts->contains($contact)) {
  357.             $this->contacts[] = $contact;
  358.             $contact->setOwner($this);
  359.         }
  360.         return $this;
  361.     }
  362.     public function removeContact(Contact $contact): self
  363.     {
  364.         if ($this->contacts->removeElement($contact)) {
  365.             // set the owning side to null (unless already changed)
  366.             if ($contact->getOwner() === $this) {
  367.                 $contact->setOwner(null);
  368.             }
  369.         }
  370.         return $this;
  371.     }
  372.     public function getPhone(): ?string
  373.     {
  374.         return $this->phone;
  375.     }
  376.     public function setPhone(?string $phone): self
  377.     {
  378.         $this->phone $phone;
  379.         return $this;
  380.     }
  381. }