src/Entity/User.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\DBAL\Types\Types;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Repository\UserRepository;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  12. #[ORM\Entity(repositoryClassUserRepository::class)]
  13. // #[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false)]
  14. class User extends RootEntity implements UserInterfacePasswordAuthenticatedUserInterface
  15. {
  16.   #[ORM\Id]
  17.   #[ORM\GeneratedValue]
  18.   #[ORM\Column(type'integer')]
  19.   #[Groups(['api_index''reg_success''api_user'])]
  20.   private $id;
  21.   #[ORM\Column(type'string'length180uniquetrue)]
  22.   #[Groups(['reg_success''api_user'])]
  23.   private $email;
  24.   #[ORM\Column(type'string'length180)]
  25.   #[Groups(['reg_success''api_user'])]
  26.   private $newEmail;
  27.   #[ORM\Column(type'string'nullabletrue)]
  28.   #[Groups(['api_user'])]
  29.   private $firstName;
  30.   #[ORM\Column(type'string'nullabletrue)]
  31.   #[Groups(['api_user'])]
  32.   private $lastName;
  33.   #[ORM\Column(type'string'nullabletrue)]
  34.   #[Groups(['api_user'])]
  35.   private $middleName;
  36.   #[ORM\Column(type'string'nullabletrue)]
  37.   #[Groups(['api_index''api_user'])]
  38.   private $phone;
  39.   #[ORM\Column(type'date_immutable'nullabletrue)]
  40.   #[Groups(['api_user'])]
  41.   private $birthday;
  42.   #[ORM\Column(type'integer'nullabletrue)]
  43.   #[Groups(['api_user'])]
  44.   private $age;
  45.   #[ORM\Column(type'integer'nullabletrue)]
  46.   #[Groups(['api_user'])]
  47.   private $height;
  48.   #[ORM\Column(type'json'nullabletrue)]
  49.   #[Groups(['api_user'])]
  50.   private $meta = [];
  51.   const ALLOW_META = [
  52.     'age' => 'number',
  53.     'height' => 'number',
  54.     'birthday' => 'date',
  55.     'phone' => 'phone',
  56.   ];
  57.   #[Groups(['api_user'])]
  58.   public $metaTemplate = [
  59.     [
  60.       'name' => 'birthday',
  61.       'pattern' => 'date',
  62.       'label' => 'Дата рождения',
  63.     ],
  64.     [
  65.       'name' => 'phone',
  66.       'pattern' => '+{7} 000 000-00-00',
  67.       'label' => 'Телефон',
  68.     ],
  69.     [
  70.       'name' => 'height',
  71.       'pattern' => '00[0]',
  72.       'label' => 'Рост, см',
  73.     ],
  74.     [
  75.       'name' => 'age',
  76.       'pattern' => '00[0]',
  77.       'label' => 'Возраст',
  78.     ],
  79.   ];
  80.   #[ORM\Column(type'json')]
  81.   private $roles = [];
  82.   const SYS_ROLES = [
  83.     'Администратор' => 'ROLE_ADMIN',
  84.     'Пользователь' => 'ROLE_USER',
  85.   ];
  86.   #[Groups(['api_user'])]
  87.   public function getTextRoles()
  88.   {
  89.     $db array_flip($this::SYS_ROLES);
  90.     $roles array_map(function ($r) use ($db) {
  91.       return isset($db[$r]) ? $db[$r] : $r;
  92.     }, $this->roles);
  93.     return array_values(array_filter($roles, fn ($r) => !in_array($r, ['Пользователь'])));
  94.   }
  95.   #[ORM\Column(type'string')]
  96.   private $password;
  97.   // Для смены пароля админом
  98.   public $plainPassword;
  99.   #[ORM\Column(type'string'nullabletrue)]
  100.   private $code;
  101.   #[ORM\Column(type'integer')]
  102.   private $status 0;
  103.   #[ORM\Column(type'string'nullabletrue)]
  104.   #[Groups(['api_user'])]
  105.   private $verificationStatus 'no';
  106.   // 'no', 'pending', 'failed', 'success'
  107.   const USER_STATUS = [
  108.     'Новый' => 0,
  109.     'Подтвержден Email' => 10,
  110.     'Активен' => 20,
  111.     'Забанен' => -10,
  112.   ];
  113.   #[ORM\OneToMany(mappedBy'user'targetEntityDocument::class)]
  114.   private Collection $documents;
  115.   #[ORM\ManyToMany(targetEntityRole::class, mappedBy'users')]
  116.   private Collection $accessRoles;
  117.   public function __construct()
  118.   {
  119.     $this->documents = new ArrayCollection();
  120.     $this->accessRoles = new ArrayCollection();
  121.   }
  122.   #[Groups(['api_user'])]
  123.   public function getFullName()
  124.   {
  125.     if ($this->firstName && $this->lastName) return $this->firstName " " $this->lastName;
  126.     return $this->email;
  127.   }
  128.   public function __toString()
  129.   {
  130.     return '#' $this->id ': ' $this->getFullName();
  131.   }
  132.   /**
  133.    * Returning a salt is only needed, if you are not using a modern
  134.    * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  135.    *
  136.    * @see UserInterface
  137.    */
  138.   public function getSalt(): ?string
  139.   {
  140.     return null;
  141.   }
  142.   /**
  143.    * @see UserInterface
  144.    */
  145.   public function eraseCredentials()
  146.   {
  147.     // If you store any temporary, sensitive data on the user, clear it here
  148.     // $this->plainPassword = null;
  149.   }
  150.   /**
  151.    * A visual identifier that represents this user.
  152.    *
  153.    * @see UserInterface
  154.    */
  155.   public function getUserIdentifier(): string
  156.   {
  157.     return (string) $this->email;
  158.   }
  159.   /**
  160.    * @see UserInterface
  161.    */
  162.   public function getRoles(): array
  163.   {
  164.     $roles $this->roles;
  165.     // guarantee every user at least has ROLE_USER
  166.     $roles[] = 'ROLE_USER';
  167.     return array_unique($roles);
  168.   }
  169.   /**
  170.    * @see PasswordAuthenticatedUserInterface
  171.    */
  172.   public function getPassword(): string
  173.   {
  174.     return $this->password;
  175.   }
  176.   public function getId(): ?int
  177.   {
  178.     return $this->id;
  179.   }
  180.   public function getEmail(): ?string
  181.   {
  182.     return $this->email;
  183.   }
  184.   public function setEmail(string $email): self
  185.   {
  186.     $this->email $email;
  187.     return $this;
  188.   }
  189.   public function getFirstName(): ?string
  190.   {
  191.     return $this->firstName;
  192.   }
  193.   public function setFirstName(?string $firstName): self
  194.   {
  195.     $this->firstName $firstName;
  196.     return $this;
  197.   }
  198.   public function getLastName(): ?string
  199.   {
  200.     return $this->lastName;
  201.   }
  202.   public function setLastName(?string $lastName): self
  203.   {
  204.     $this->lastName $lastName;
  205.     return $this;
  206.   }
  207.   public function getMiddleName(): ?string
  208.   {
  209.     return $this->middleName;
  210.   }
  211.   public function setMiddleName(?string $middleName): self
  212.   {
  213.     $this->middleName $middleName;
  214.     return $this;
  215.   }
  216.   public function getPhone(): ?string
  217.   {
  218.     return $this->phone;
  219.   }
  220.   public function setPhone(?string $phone): self
  221.   {
  222.     $this->phone $phone;
  223.     return $this;
  224.   }
  225.   public function getAge(): ?int
  226.   {
  227.     return $this->age;
  228.   }
  229.   public function setAge(?int $age): self
  230.   {
  231.     $this->age $age;
  232.     return $this;
  233.   }
  234.   public function getHeight(): ?int
  235.   {
  236.     return $this->height;
  237.   }
  238.   public function setHeight(?int $height): self
  239.   {
  240.     $this->height $height;
  241.     return $this;
  242.   }
  243.   public function getMeta(): array
  244.   {
  245.     return $this->meta;
  246.   }
  247.   public function setMeta(?array $meta): self
  248.   {
  249.     $this->meta $meta;
  250.     return $this;
  251.   }
  252.   public function setRoles(array $roles): self
  253.   {
  254.     $this->roles $roles;
  255.     return $this;
  256.   }
  257.   public function setPassword(string $password): self
  258.   {
  259.     $this->password $password;
  260.     return $this;
  261.   }
  262.   public function getCode(): ?string
  263.   {
  264.     return $this->code;
  265.   }
  266.   public function setCode(?string $code): self
  267.   {
  268.     $this->code $code;
  269.     return $this;
  270.   }
  271.   public function getStatus(): ?int
  272.   {
  273.     return $this->status;
  274.   }
  275.   public function setStatus(int $status): self
  276.   {
  277.     $this->status $status;
  278.     return $this;
  279.   }
  280.   /**
  281.    * @return Collection<int, Document>
  282.    */
  283.   public function getDocuments(): Collection
  284.   {
  285.     return $this->documents;
  286.   }
  287.   public function addDocument(Document $document): self
  288.   {
  289.     if (!$this->documents->contains($document)) {
  290.       $this->documents->add($document);
  291.       $document->setUser($this);
  292.     }
  293.     return $this;
  294.   }
  295.   public function removeDocument(Document $document): self
  296.   {
  297.     if ($this->documents->removeElement($document)) {
  298.       // set the owning side to null (unless already changed)
  299.       if ($document->getUser() === $this) {
  300.         $document->setUser(null);
  301.       }
  302.     }
  303.     return $this;
  304.   }
  305.   /**
  306.    * @return Collection<int, Role>
  307.    */
  308.   public function getAccessRoles(): Collection
  309.   {
  310.     return $this->accessRoles;
  311.   }
  312.   public function addAccessRole(Role $accessRole): self
  313.   {
  314.     if (!$this->accessRoles->contains($accessRole)) {
  315.       $this->accessRoles->add($accessRole);
  316.       $accessRole->addUser($this);
  317.     }
  318.     return $this;
  319.   }
  320.   public function removeAccessRole(Role $accessRole): self
  321.   {
  322.     if ($this->accessRoles->removeElement($accessRole)) {
  323.       $accessRole->removeUser($this);
  324.     }
  325.     return $this;
  326.   }
  327.   public function getNewEmail(): ?string
  328.   {
  329.     return $this->newEmail;
  330.   }
  331.   public function setNewEmail(string $newEmail): self
  332.   {
  333.     $this->newEmail $newEmail;
  334.     return $this;
  335.   }
  336.   public function getBirthday(): ?\DateTimeImmutable
  337.   {
  338.     return $this->birthday;
  339.   }
  340.   public function setBirthday(?\DateTimeImmutable $birthday): self
  341.   {
  342.     $this->birthday $birthday;
  343.     return $this;
  344.   }
  345.   public function getVerificationStatus(): ?string
  346.   {
  347.     return $this->verificationStatus;
  348.   }
  349.   public function setVerificationStatus(?string $verificationStatus): self
  350.   {
  351.     $this->verificationStatus $verificationStatus;
  352.     return $this;
  353.   }
  354. }