src/Entity/User.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Symfony\Component\Security\Core\User\UserInterface;
  4. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\{CollectionArrayCollection};
  7. use App\Repository\UserRepository;
  8. /**
  9.  * User
  10.  *
  11.  * @UniqueEntity({"email"}, message="Email пользователя должен быть уникальным")
  12.  */
  13. #[ORM\Table('user')]
  14. #[ORM\Entity(UserRepository::class)]
  15. class User implements UserInterface
  16. {
  17.     public const SALT 'motivitydigital';
  18.     public const ROLE_ADMIN 'ROLE_ADMIN';
  19.     public const ROLE_USER 'ROLE_USER';
  20.     #[ORM\Column('id''integer')]
  21.     #[ORM\Id]
  22.     #[ORM\GeneratedValue('IDENTITY')]
  23.     private ?int $id null;
  24.     #[ORM\Column('email''string'255uniquetrue)]
  25.     private ?string $email null;
  26.     #[ORM\Column('password''text'255nullabletrue)]
  27.     private ?string $password null;
  28.     #[ORM\Column('first_name''text'255)]
  29.     private ?string $firstName null;
  30.     #[ORM\Column('last_name''text'255)]
  31.     private ?string $lastName null;
  32.     #[ORM\Column('middle_name''text'255nullabletrue)]
  33.     private ?string $middleName null;
  34.     #[ORM\Column('deleted''boolean')]
  35.     private bool $deleted false;
  36.     #[ORM\Column('active''boolean')]
  37.     private bool $active true;
  38.     #[ORM\ManyToMany(Role::class)]
  39.     #[ORM\JoinTable('usergroup')]
  40.     #[ORM\JoinColumn('user_id')]
  41.     #[ORM\InverseJoinColumn('group_id')]
  42.     private ?Collection $groups null;
  43.     #[ORM\Column('created''date_immutable'nullabletrue)]
  44.     private ?\DateTimeImmutable $created;
  45.     public function __construct()
  46.     {
  47.         $this->created = new \DateTimeImmutable();
  48.         $this->groups = new ArrayCollection();
  49.     }
  50.     public function getSalt(): string
  51.     {
  52.         return self::SALT;
  53.     }
  54.     public function getUsername(): ?string
  55.     {
  56.         return $this->email;
  57.     }
  58.     public function eraseCredentials()
  59.     {
  60.         // TODO: Implement eraseCredentials() method.
  61.     }
  62.     public function getId(): ?int
  63.     {
  64.         return $this->id;
  65.     }
  66.     public function setId(int $id): User
  67.     {
  68.         $this->id $id;
  69.         return $this;
  70.     }
  71.     public function getEmail(): ?string
  72.     {
  73.         return $this->email;
  74.     }
  75.     public function setEmail(?string $email): User
  76.     {
  77.         $this->email $email;
  78.         return $this;
  79.     }
  80.     public function getPassword(): ?string
  81.     {
  82.         return $this->password;
  83.     }
  84.     public function setPassword(?string $password): User
  85.     {
  86.         if ($password) {
  87.             $this->password md5($password self::SALT);
  88.         }
  89.         return $this;
  90.     }
  91.     public function getUserRolesSystemNames(): array
  92.     {
  93.         $result = [];
  94.         foreach ($this->groups as $role) {
  95.             $result[] = $role->getSystemName();
  96.         }
  97.         return $result;
  98.     }
  99.     public function getGroups(): ?Collection
  100.     {
  101.         return $this->groups;
  102.     }
  103.     /**
  104.      * @todo multiple roles
  105.      */
  106.     public function getGroup(): Role
  107.     {
  108.         return $this->groups->current();
  109.     }
  110.     public function setGroups(Collection $groups): User
  111.     {
  112.         $this->groups $groups;
  113.         return $this;
  114.     }
  115.     /**
  116.      * @see UserInterface
  117.      */
  118.     public function getRoles(): array
  119.     {
  120.         return [self::ROLE_ADMINself::ROLE_USER];
  121.     }
  122.     public function setRoles(Collection $roles): User
  123.     {
  124.         $this->groups $roles;
  125.         return $this;
  126.     }
  127.     public function getFirstName(): ?string
  128.     {
  129.         return $this->firstName;
  130.     }
  131.     public function setFirstName(?string $firstName): User
  132.     {
  133.         $this->firstName $firstName;
  134.         return $this;
  135.     }
  136.     public function getLastName(): ?string
  137.     {
  138.         return $this->lastName;
  139.     }
  140.     public function setLastName(?string $lastName): User
  141.     {
  142.         $this->lastName $lastName;
  143.         return $this;
  144.     }
  145.     public function getMiddleName(): ?string
  146.     {
  147.         return $this->middleName;
  148.     }
  149.     public function setMiddleName(?string $middleName): User
  150.     {
  151.         $this->middleName $middleName;
  152.         return $this;
  153.     }
  154.     public function isDeleted(): bool
  155.     {
  156.         return $this->deleted;
  157.     }
  158.     public function setDeleted(bool $deleted): User
  159.     {
  160.         $this->deleted $deleted;
  161.         return $this;
  162.     }
  163.     public function isActive(): bool
  164.     {
  165.         return $this->active;
  166.     }
  167.     public function setActive(bool $active): User
  168.     {
  169.         $this->active $active;
  170.         return $this;
  171.     }
  172.     public function getCreated(): ?\DateTimeImmutable
  173.     {
  174.         return $this->created;
  175.     }
  176.     public function setCreated(?\DateTimeImmutable $created): User
  177.     {
  178.         $this->created $created;
  179.         return $this;
  180.     }
  181.     public function getFullName(): string
  182.     {
  183.         return "{$this->firstName} {$this->lastName}";
  184.     }
  185.     public function getFullTitle(): string
  186.     {
  187.         return $this->getFullName() . ' - ' $this->email;
  188.     }
  189.     public function getUserIdentifier()
  190.     {
  191.         return $this->id;
  192.     }
  193. }