src/Entity/Eg/Admin/User.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Eg\Admin;
  3. use App\Entity\DictionaryImport;
  4. use App\Entity\Eg\Osm\City;
  5. use App\Entity\Eg\Osm\Organization;
  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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. #[ORM\Table(name: 'eg_admin_user')]
  14. #[ORM\Entity(repositoryClass: UserRepository::class)]
  15. #[ORM\HasLifecycleCallbacks]
  16. #[UniqueEntity(fields: ['username'], message: 'user.is_already.with_this_username')]
  17. #[UniqueEntity(fields: ['email'], message: 'user.is_already.with_this_email')]
  18. #[ORM\UniqueConstraint(name: 'uniq_e524bf7592fc23a8', columns: ['username_canonical'])]
  19. #[ORM\UniqueConstraint(name: 'uniq_e524bf75c05fb297', columns: ['confirmation_token'])]
  20. #[ORM\UniqueConstraint(name: 'uniq_e524bf75a0d96fbf', columns: ['email_canonical'])]
  21. class User implements UserInterface, PasswordAuthenticatedUserInterface
  22. {
  23.     private const SYSTEM_USERS = [
  24.         'root',
  25.         'visitor',
  26.     ];
  27.     private const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
  28.     private const ROLE_DEFAULT = 'ROLE_USER';
  29.     #[ORM\Id]
  30.     #[ORM\GeneratedValue(strategy: 'IDENTITY')]
  31.     #[ORM\Column(type: 'integer')]
  32.     private ?int $id = null;
  33.     /**
  34.      * @var string
  35.      */
  36.     #[ORM\Column(type: 'string', length: 180, unique: true)]
  37.     private $username;
  38.     /**
  39.      * @var string
  40.      */
  41.     #[ORM\Column(type: 'string', length: 180, nullable: false)]
  42.     private $usernameCanonical;
  43.     /**
  44.      * @var string
  45.      */
  46.     #[ORM\Column(type: 'string', length: 180, nullable: false)]
  47.     private $email;
  48.     /**
  49.      * @var string
  50.      */
  51.     #[ORM\Column(type: 'string', length: 180, nullable: false)]
  52.     private $emailCanonical;
  53.     /**
  54.      * @var bool
  55.      */
  56.     #[ORM\Column(type: 'boolean', nullable: false)]
  57.     private $enabled;
  58.     /**
  59.      * @var string|null
  60.      */
  61.     #[ORM\Column(type: 'string', length: 255, nullable: true)]
  62.     private $salt;
  63.     /**
  64.      * @var string
  65.      */
  66.     #[ORM\Column(type: 'string', length: 255, nullable: false)]
  67.     private $password;
  68.     /**
  69.      * @var \DateTimeInterface|null
  70.      */
  71.     #[ORM\Column(type: 'datetime', nullable: true)]
  72.     private $lastLogin;
  73.     /**
  74.      * @var string|null
  75.      */
  76.     #[ORM\Column(type: 'string', length: 180, nullable: true)]
  77.     private $confirmationToken;
  78.     /**
  79.      * @var \DateTimeInterface|null
  80.      */
  81.     #[ORM\Column(type: 'datetime', nullable: true)]
  82.     private $passwordRequestedAt;
  83.     /**
  84.      * @var array
  85.      */
  86.     #[ORM\Column(type: 'array', nullable: false)]
  87.     private $roles = [];
  88.     /**
  89.      * @var string|null
  90.      */
  91.     #[ORM\Column(type: 'string', length: 24, nullable: true)]
  92.     private $startingEntity;
  93.     /**
  94.      * @var string|null
  95.      */
  96.     #[ORM\Column(type: 'string', length: 12, nullable: true)]
  97.     private $startingAction;
  98.     /**
  99.      * @var int|null
  100.      */
  101.     #[ORM\Column(type: 'integer', nullable: true)]
  102.     private $startingId;
  103.     /**
  104.      * @var string
  105.      */
  106.     #[ORM\Column(type: 'string', length: 7, nullable: false)]
  107.     private $color;
  108.     /**
  109.      * @var \DateTimeInterface|null
  110.      */
  111.     #[ORM\Column(type: 'datetime', nullable: true)]
  112.     private $deletedAt;
  113.     /** @var Collection<int, Organization> */
  114.     #[ORM\ManyToMany(targetEntity: Organization::class, inversedBy: 'users')]
  115.     #[ORM\JoinTable(name: 'eg_admin_user_organization_join')]
  116.     private Collection $organizations;
  117.     /**
  118.      * @var Collection
  119.      */
  120.     #[ORM\JoinTable(name: 'eg_admin_user_group_join')]
  121.     #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
  122.     #[ORM\InverseJoinColumn(name: 'group_id', referencedColumnName: 'id')]
  123.     #[ORM\ManyToMany(targetEntity: UserGroup::class, inversedBy: 'users')]
  124.     private $groups;
  125.     /** @var Collection<int, City> */
  126.     #[ORM\ManyToMany(targetEntity: City::class, inversedBy: 'users')]
  127.     #[ORM\JoinTable(name: 'eg_osm_user_city_join')]
  128.     private Collection $cities;
  129.     #[ORM\Column(type: 'datetimetz', nullable: true)]
  130.     private $createdAt;
  131.     #[ORM\Column(type: 'datetimetz', nullable: true)]
  132.     private $updatedAt;
  133.     #[ORM\OneToMany(targetEntity: DictionaryImport::class, mappedBy: 'createdBy')]
  134.     private $dictionaryImports;
  135.     public static function getSystemUsers(): array
  136.     {
  137.         return self::SYSTEM_USERS;
  138.     }
  139.     public static function getRoleSuperAdmin(): string
  140.     {
  141.         return self::ROLE_SUPER_ADMIN;
  142.     }
  143.     public function __construct()
  144.     {
  145.         $this->organizations = new ArrayCollection();
  146.         $this->groups = new ArrayCollection();
  147.         $this->cities = new ArrayCollection();
  148.         $now = new \DateTime();
  149.         $this->setCreatedAt($now);
  150.         $this->setUpdatedAt($now);
  151.         $this->dictionaryImports = new ArrayCollection();
  152.     }
  153.     public function __toString(): string
  154.     {
  155.         return $this->getUserIdentifier();
  156.     }
  157.     #[ORM\PreUpdate]
  158.     public function onPreUpdate(): void
  159.     {
  160.         $this->setUpdatedAt(new \DateTime());
  161.     }
  162.     /**
  163.      * Metoda do kanonizowania stringów (zabezpieczania pól)
  164.      */
  165.     public function canonicalize($string)
  166.     {
  167.         if (null === $string) {
  168.             return;
  169.         }
  170.         $encoding = mb_detect_encoding($string);
  171.         $result = $encoding
  172.             ? mb_convert_case($string, MB_CASE_LOWER, $encoding)
  173.             : mb_convert_case($string, MB_CASE_LOWER);
  174.         return $result;
  175.     }
  176.     public function isGranted(string $role, bool $checkSuperAdmin = true): bool
  177.     {
  178.         if ($this->hasRole($role) || $this->hasRole('ROLE_SUPER_ADMIN') && $checkSuperAdmin) {
  179.             return true;
  180.         }
  181.         foreach ($this->getGroups() as $group) {
  182.             if ($group->hasRole($role) || $group->hasRole('ROLE_SUPER_ADMIN') && $checkSuperAdmin) {
  183.                 return true;
  184.             }
  185.         }
  186.         return false;
  187.     }
  188.     public function hasRole(string $role): bool
  189.     {
  190.         return \in_array(\strtoupper($role), $this->getRoles(), true);
  191.     }
  192.     public function getId(): ?int
  193.     {
  194.         return $this->id;
  195.     }
  196.     /**
  197.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  198.      */
  199.     public function getUsername(): ?string
  200.     {
  201.         return $this->username;
  202.     }
  203.     public function setUsername(string $username): self
  204.     {
  205.         $this->username = $username;
  206.         $this->setUsernameCanonical(
  207.             $this->canonicalize($username)
  208.         );
  209.         return $this;
  210.     }
  211.     public function getUsernameCanonical(): ?string
  212.     {
  213.         return $this->usernameCanonical;
  214.     }
  215.     public function setUsernameCanonical(string $usernameCanonical): self
  216.     {
  217.         $this->usernameCanonical = $usernameCanonical;
  218.         return $this;
  219.     }
  220.     public function getEmail(): ?string
  221.     {
  222.         return $this->email;
  223.     }
  224.     public function setEmail(string $email): self
  225.     {
  226.         $this->email = $email;
  227.         $this->setEmailCanonical(
  228.             $this->canonicalize($email)
  229.         );
  230.         return $this;
  231.     }
  232.     public function getEmailCanonical(): ?string
  233.     {
  234.         return $this->emailCanonical;
  235.     }
  236.     public function setEmailCanonical(string $emailCanonical): self
  237.     {
  238.         $this->emailCanonical = $emailCanonical;
  239.         return $this;
  240.     }
  241.     public function getEnabled(): ?bool
  242.     {
  243.         return $this->enabled;
  244.     }
  245.     public function setEnabled(bool $enabled): self
  246.     {
  247.         $this->enabled = $enabled;
  248.         return $this;
  249.     }
  250.     /**
  251.      * Returning a salt is only needed, if you are not using a modern
  252.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  253.      *
  254.      * @see UserInterface
  255.      */
  256.     public function getSalt(): ?string
  257.     {
  258.         return null;
  259.     }
  260.     /**
  261.      * @see PasswordAuthenticatedUserInterface
  262.      */
  263.     public function getPassword(): ?string
  264.     {
  265.         return $this->password;
  266.     }
  267.     public function setPassword(string $password): self
  268.     {
  269.         $this->password = $password;
  270.         return $this;
  271.     }
  272.     public function getLastLogin(): ?\DateTimeInterface
  273.     {
  274.         return $this->lastLogin;
  275.     }
  276.     public function setLastLogin(?\DateTimeInterface $lastLogin): self
  277.     {
  278.         $this->lastLogin = $lastLogin;
  279.         return $this;
  280.     }
  281.     public function getConfirmationToken(): ?string
  282.     {
  283.         return $this->confirmationToken;
  284.     }
  285.     public function setConfirmationToken(?string $confirmationToken): self
  286.     {
  287.         $this->confirmationToken = $confirmationToken;
  288.         return $this;
  289.     }
  290.     public function getPasswordRequestedAt(): ?\DateTimeInterface
  291.     {
  292.         return $this->passwordRequestedAt;
  293.     }
  294.     public function setPasswordRequestedAt(?\DateTimeInterface $passwordRequestedAt): self
  295.     {
  296.         $this->passwordRequestedAt = $passwordRequestedAt;
  297.         return $this;
  298.     }
  299.     /**
  300.      * @see UserInterface
  301.      */
  302.     public function getRoles(): ?array
  303.     {
  304.         $roles = $this->roles;
  305.         foreach ($this->getGroups() as $group) {
  306.             $roles = \array_merge($roles, $group->getRoles());
  307.         }
  308.         // guarantee every user at least has ROLE_USER
  309.         $roles[] = self::ROLE_DEFAULT;
  310.         return \array_unique($roles);
  311.     }
  312.     public function addRole(string $role): self
  313.     {
  314.         if (!in_array($role, $this->roles)) {
  315.             $this->roles[] = $role;
  316.         }
  317.         return $this;
  318.     }
  319.     public function setRoles(array $roles): self
  320.     {
  321.         $this->roles = $roles;
  322.         return $this;
  323.     }
  324.     public function getStartingEntity(): ?string
  325.     {
  326.         return $this->startingEntity;
  327.     }
  328.     public function setStartingEntity(?string $startingEntity): self
  329.     {
  330.         $this->startingEntity = $startingEntity;
  331.         return $this;
  332.     }
  333.     public function getStartingAction(): ?string
  334.     {
  335.         return $this->startingAction;
  336.     }
  337.     public function setStartingAction(?string $startingAction): self
  338.     {
  339.         $this->startingAction = $startingAction;
  340.         return $this;
  341.     }
  342.     public function getStartingId(): ?int
  343.     {
  344.         return $this->startingId;
  345.     }
  346.     public function setStartingId(?int $startingId): self
  347.     {
  348.         $this->startingId = $startingId;
  349.         return $this;
  350.     }
  351.     public function getColor(): ?string
  352.     {
  353.         return $this->color;
  354.     }
  355.     public function setColor(string $color): self
  356.     {
  357.         $this->color = $color;
  358.         return $this;
  359.     }
  360.     public function getDeletedAt(): ?\DateTimeInterface
  361.     {
  362.         return $this->deletedAt;
  363.     }
  364.     public function setDeletedAt(?\DateTimeInterface $deletedAt): self
  365.     {
  366.         $this->deletedAt = $deletedAt;
  367.         return $this;
  368.     }
  369.     /** @return Collection<int, Organization> */
  370.     public function getOrganizations(): Collection
  371.     {
  372.         return $this->organizations;
  373.     }
  374.     /**
  375.      * @return Collection<int, UserGroup>
  376.      */
  377.     public function getGroups(): Collection
  378.     {
  379.         return $this->groups;
  380.     }
  381.     public function addGroup(UserGroup $group): self
  382.     {
  383.         if (!$this->groups->contains($group)) {
  384.             $this->groups[] = $group;
  385.         }
  386.         return $this;
  387.     }
  388.     public function removeGroup(UserGroup $group): self
  389.     {
  390.         $this->groups->removeElement($group);
  391.         return $this;
  392.     }
  393.     /** @return Collection<int, City> */
  394.     public function getCities(): Collection
  395.     {
  396.         return $this->cities;
  397.     }
  398.     /**
  399.      * @see UserInterface
  400.      */
  401.     public function eraseCredentials()
  402.     {
  403.         // If you store any temporary, sensitive data on the user, clear it here
  404.         //
  405.     }
  406.     /**
  407.      * A visual identifier that represents this user.
  408.      *
  409.      * @see UserInterface
  410.      */
  411.     public function getUserIdentifier(): string
  412.     {
  413.         return (string) $this->username;
  414.     }
  415.     public function getCreatedAt(): ?\DateTimeInterface
  416.     {
  417.         return $this->createdAt;
  418.     }
  419.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  420.     {
  421.         $this->createdAt = $createdAt;
  422.         return $this;
  423.     }
  424.     public function getUpdatedAt(): ?\DateTimeInterface
  425.     {
  426.         return $this->updatedAt;
  427.     }
  428.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  429.     {
  430.         $this->updatedAt = $updatedAt;
  431.         return $this;
  432.     }
  433.     public function isSuperAdmin(): bool
  434.     {
  435.         return $this->hasRole('ROLE_SUPER_ADMIN');
  436.     }
  437.     /**
  438.      * @return Collection<int, DictionaryImport>
  439.      */
  440.     public function getDictionaryImports(): Collection
  441.     {
  442.         return $this->dictionaryImports;
  443.     }
  444.     public function addDictionaryImport(DictionaryImport $dictionaryImport): self
  445.     {
  446.         if (!$this->dictionaryImports->contains($dictionaryImport)) {
  447.             $this->dictionaryImports[] = $dictionaryImport;
  448.             $dictionaryImport->setCreatedBy($this);
  449.         }
  450.         return $this;
  451.     }
  452. }