src/Entity/User.php line 16

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Serializer\Annotation\Ignore;
  11. #[ORM\Entity(repositoryClassUserRepository::class)]
  12. #[UniqueEntity(fields: ['email'], message'Пользователь с таким E-mail адресом уже зарегистрирован')]
  13. class User implements UserInterfacePasswordAuthenticatedUserInterface
  14. {
  15.     public const ROLE_NAMES = [
  16.                        'ROLE_ROOT' => 'Руководитель',
  17.                        'ROLE_ADMIN' => 'Администратор',
  18.                        'ROLE_USER' => 'Пользователь',
  19.                        'ROLE_GUEST' => 'Гость'
  20.                    ];
  21.     private ?string $role '';
  22.     
  23.     #[ORM\Id]
  24.     #[ORM\GeneratedValue]
  25.     #[ORM\Column]
  26.     private ?int $id null;
  27.     #[ORM\Column(length180uniquetrue)]
  28.     private ?string $email null;
  29.     #[ORM\Column]
  30.     private array $roles = [];
  31.     /**
  32.      * @var string The hashed password
  33.      */
  34.     #[ORM\Column]
  35.     private ?string $password null;
  36.     
  37.     #[ORM\Column(nullabletrue)]
  38.     private ?string $vkToken null;
  39.     
  40.     #[ORM\Column(nullabletrue)]
  41.     private ?string $vkId null;
  42.     #[ORM\Column(type'boolean')]
  43.     private $isVerified false;
  44.     #[ORM\Column(options: ['default' => 0])]
  45.     private ?float $balance null;
  46.     #[ORM\OneToMany(mappedBy'user'targetEntityBillingHistory::class)]
  47.     #[Ignore]
  48.     private Collection $billingHistories;
  49.     #[ORM\OneToMany(mappedBy'user'targetEntityBillingTransaction::class)]
  50.     #[Ignore]
  51.     private Collection $billingTransactions;
  52.     #[ORM\OneToMany(mappedBy'user'targetEntityUserProject::class)]
  53.     #[Ignore]
  54.     private Collection $userProjects;
  55.     
  56.     #[ORM\OneToMany(mappedBy'user'targetEntityUserPlatform::class)]
  57.     #[Ignore]
  58.     private Collection $userPlatforms;
  59.     
  60.     #[ORM\OneToMany(mappedBy'user'targetEntityUserOrd::class)]
  61.     #[Ignore]
  62.     private Collection $userOrd;
  63.     public function __construct()
  64.     {
  65.         $this->billingHistories = new ArrayCollection();
  66.         $this->billingTransactions = new ArrayCollection();
  67.         $this->userProjects = new ArrayCollection();
  68.         $this->userPlatforms = new ArrayCollection();
  69.         $this->userOrd = new ArrayCollection();
  70.     }
  71.     public function getId(): ?int
  72.     {
  73.         return $this->id;
  74.     }
  75.     public function getEmail(): ?string
  76.     {
  77.         return $this->email;
  78.     }
  79.     public function setEmail(string $email): self
  80.     {
  81.         $this->email $email;
  82.         return $this;
  83.     }
  84.     /**
  85.      * A visual identifier that represents this user.
  86.      *
  87.      * @see UserInterface
  88.      */
  89.     public function getUserIdentifier(): string
  90.     {
  91.         return (string) $this->email;
  92.     }
  93.     /**
  94.      * @see UserInterface
  95.      */
  96.     public function getRoles(): array
  97.     {
  98.         $roles $this->roles;
  99.         // guarantee every user at least has ROLE_USER
  100.         $roles[] = 'ROLE_USER';
  101.         return array_unique($roles);
  102.     }
  103.     public function setRoles(array $roles): self
  104.     {
  105.         $this->roles $roles;
  106.         return $this;
  107.     }
  108.     
  109.     public function getRolesName(): string
  110.                    {
  111.                        $rolesName = [];
  112.                        foreach ($this->getRoles() as $role$rolesName[] = self::ROLE_NAMES[$role];
  113.                        
  114.                        return implode(', '$rolesName);
  115.                    }
  116.     
  117.     /**
  118.      * @param string $roleName
  119.      * @return bool
  120.      */
  121.     public function hasRole(string $roleName): bool
  122.     {
  123.         if (in_array($roleName$this->roles)) return true;
  124.         return false;
  125.     }
  126.     public function getRole(): bool
  127.     {
  128.         return $this->role;
  129.     }
  130.     
  131.     /**
  132.      * @return bool
  133.      */
  134.     public function isAdmin(): bool
  135.     {
  136.         if (in_array('ROLE_ADMIN'$this->roles)) return true;
  137.         if (in_array('ROLE_ROOT'$this->roles)) return true;
  138.         return false;
  139.     }
  140.     /**
  141.      * @see PasswordAuthenticatedUserInterface
  142.      */
  143.     public function getPassword(): string
  144.     {
  145.         return $this->password;
  146.     }
  147.     public function setPassword(string $password): self
  148.     {
  149.         $this->password $password;
  150.         return $this;
  151.     }
  152.     /**
  153.      * @see UserInterface
  154.      */
  155.     public function eraseCredentials()
  156.     {
  157.         // If you store any temporary, sensitive data on the user, clear it here
  158.         // $this->plainPassword = null;
  159.     }
  160.     public function isVerified(): bool
  161.     {
  162.         return $this->isVerified;
  163.     }
  164.     public function setIsVerified(bool $isVerified): self
  165.     {
  166.         $this->isVerified $isVerified;
  167.         return $this;
  168.     }
  169.     public function getBalance(): ?float
  170.     {
  171.         return $this->balance;
  172.     }
  173.     public function setBalance(float $balance): self
  174.     {
  175.         $this->balance $balance;
  176.         return $this;
  177.     }
  178.     /**
  179.      * @return Collection<int, BillingHistory>
  180.      */
  181.     public function getBillingHistories(): Collection
  182.     {
  183.         return $this->billingHistories;
  184.     }
  185.     public function addBillingHistory(BillingHistory $billingHistory): self
  186.     {
  187.         if (!$this->billingHistories->contains($billingHistory)) {
  188.             $this->billingHistories->add($billingHistory);
  189.             $billingHistory->setUser($this);
  190.         }
  191.         return $this;
  192.     }
  193.     public function removeBillingHistory(BillingHistory $billingHistory): self
  194.     {
  195.         if ($this->billingHistories->removeElement($billingHistory)) {
  196.             // set the owning side to null (unless already changed)
  197.             if ($billingHistory->getUser() === $this) {
  198.                 $billingHistory->setUser(null);
  199.             }
  200.         }
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return Collection<int, BillingTransaction>
  205.      */
  206.     public function getBillingTransactions(): Collection
  207.     {
  208.         return $this->billingTransactions;
  209.     }
  210.     public function addBillingTransaction(BillingTransaction $billingTransaction): self
  211.     {
  212.         if (!$this->billingTransactions->contains($billingTransaction)) {
  213.             $this->billingTransactions->add($billingTransaction);
  214.             $billingTransaction->setUser($this);
  215.         }
  216.         return $this;
  217.     }
  218.     public function removeBillingTransaction(BillingTransaction $billingTransaction): self
  219.     {
  220.         if ($this->billingTransactions->removeElement($billingTransaction)) {
  221.             // set the owning side to null (unless already changed)
  222.             if ($billingTransaction->getUser() === $this) {
  223.                 $billingTransaction->setUser(null);
  224.             }
  225.         }
  226.         return $this;
  227.     }
  228.     /**
  229.      * @return Collection<int, UserProject>
  230.      */
  231.     public function getUserProjects(): Collection
  232.     {
  233.         return $this->userProjects;
  234.     }
  235.     public function addUserProject(UserProject $userProject): self
  236.     {
  237.         if (!$this->userProjects->contains($userProject)) {
  238.             $this->userProjects->add($userProject);
  239.             $userProject->setUser($this);
  240.         }
  241.         return $this;
  242.     }
  243.     public function removeUserProject(UserProject $userProject): self
  244.     {
  245.         if ($this->userProjects->removeElement($userProject)) {
  246.             // set the owning side to null (unless already changed)
  247.             if ($userProject->getUser() === $this) {
  248.                 $userProject->setUser(null);
  249.             }
  250.         }
  251.         return $this;
  252.     }
  253.     #[Ignore]
  254.     public function getMainUserProject(): UserProject
  255.     {
  256.         foreach ($this->userProjects as $project) {
  257.             if ($project->isMain()) {
  258.                 return $project;
  259.             }
  260.         }
  261.     }
  262.     
  263.     /**
  264.      * @return string|null
  265.      */
  266.     public function getVkToken(): ?string
  267.     {
  268.         return $this->vkToken;
  269.     }
  270.     
  271.     /**
  272.      * @param string|null $vkToken
  273.      */
  274.     public function setVkToken(?string $vkToken): void
  275.     {
  276.         $this->vkToken $vkToken;
  277.     }
  278.     
  279.     /**
  280.      * @return string|null
  281.      */
  282.     public function getVkId(): ?string
  283.     {
  284.         return $this->vkId;
  285.     }
  286.     
  287.     /**
  288.      * @param string|null $vkId
  289.      */
  290.     public function setVkId(?string $vkId): void
  291.     {
  292.         $this->vkId $vkId;
  293.     }
  294.     
  295.     /**
  296.      * @return Collection
  297.      */
  298.     public function getUserPlatforms(): Collection
  299.     {
  300.         return $this->userPlatforms;
  301.     }
  302.     
  303.     /**
  304.      * @param Collection $userPlatforms
  305.      */
  306.     public function setUserPlatforms(Collection $userPlatforms): void
  307.     {
  308.         $this->userPlatforms $userPlatforms;
  309.     }
  310.     
  311.     public function addUserPlatform(UserPlatform $userPlatform): self
  312.     {
  313.         if (!$this->userPlatforms->contains($userPlatform)) {
  314.             $this->userPlatforms->add($userPlatform);
  315.             $userPlatform->setUser($this);
  316.         }
  317.         
  318.         return $this;
  319.     }
  320.     
  321.     public function removeUserPlatform(UserPlatform $userPlatform): self
  322.     {
  323.         if ($this->userPlatforms->removeElement($userPlatform)) {
  324.             if ($userPlatform->getUser() === $this) {
  325.                 $userPlatform->setUser(null);
  326.             }
  327.         }
  328.         
  329.         return $this;
  330.     }
  331.     
  332.     /**
  333.      * @return Collection
  334.      */
  335.     public function getUserOrd(): Collection
  336.     {
  337.         return $this->userOrd;
  338.     }
  339.     
  340.     /**
  341.      * @param Collection $userOrd
  342.      */
  343.     public function setUserOrd(Collection $userOrd): void
  344.     {
  345.         $this->userOrd $userOrd;
  346.     }
  347.     
  348.     public function addUserOrd(UserOrd $userOrd): self
  349.     {
  350.         if (!$this->userOrd->contains($userOrd)) {
  351.             $this->userOrd->add($userOrd);
  352.             $userOrd->setUser($this);
  353.         }
  354.         
  355.         return $this;
  356.     }
  357.     
  358.     public function removeUserOrd(UserOrd $userOrd): self
  359.     {
  360.         if ($this->userOrd->removeElement($userOrd)) {
  361.             if ($userOrd->getUser() === $this) {
  362.                 $userOrd->setUser(null);
  363.             }
  364.         }
  365.         
  366.         return $this;
  367.     }
  368. }