<?php
namespace App\Entity\Eg\Admin;
use App\Entity\DictionaryImport;
use App\Entity\Eg\Osm\City;
use App\Entity\Eg\Osm\Organization;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Table(name: 'eg_admin_user')]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: ['username'], message: 'user.is_already.with_this_username')]
#[UniqueEntity(fields: ['email'], message: 'user.is_already.with_this_email')]
#[ORM\UniqueConstraint(name: 'uniq_e524bf7592fc23a8', columns: ['username_canonical'])]
#[ORM\UniqueConstraint(name: 'uniq_e524bf75c05fb297', columns: ['confirmation_token'])]
#[ORM\UniqueConstraint(name: 'uniq_e524bf75a0d96fbf', columns: ['email_canonical'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
private const SYSTEM_USERS = [
'root',
'visitor',
];
private const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
private const ROLE_DEFAULT = 'ROLE_USER';
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 180, unique: true)]
private $username;
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 180, nullable: false)]
private $usernameCanonical;
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 180, nullable: false)]
private $email;
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 180, nullable: false)]
private $emailCanonical;
/**
* @var bool
*/
#[ORM\Column(type: 'boolean', nullable: false)]
private $enabled;
/**
* @var string|null
*/
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $salt;
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 255, nullable: false)]
private $password;
/**
* @var \DateTimeInterface|null
*/
#[ORM\Column(type: 'datetime', nullable: true)]
private $lastLogin;
/**
* @var string|null
*/
#[ORM\Column(type: 'string', length: 180, nullable: true)]
private $confirmationToken;
/**
* @var \DateTimeInterface|null
*/
#[ORM\Column(type: 'datetime', nullable: true)]
private $passwordRequestedAt;
/**
* @var array
*/
#[ORM\Column(type: 'array', nullable: false)]
private $roles = [];
/**
* @var string|null
*/
#[ORM\Column(type: 'string', length: 24, nullable: true)]
private $startingEntity;
/**
* @var string|null
*/
#[ORM\Column(type: 'string', length: 12, nullable: true)]
private $startingAction;
/**
* @var int|null
*/
#[ORM\Column(type: 'integer', nullable: true)]
private $startingId;
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 7, nullable: false)]
private $color;
/**
* @var \DateTimeInterface|null
*/
#[ORM\Column(type: 'datetime', nullable: true)]
private $deletedAt;
/** @var Collection<int, Organization> */
#[ORM\ManyToMany(targetEntity: Organization::class, inversedBy: 'users')]
#[ORM\JoinTable(name: 'eg_admin_user_organization_join')]
private Collection $organizations;
/**
* @var Collection
*/
#[ORM\JoinTable(name: 'eg_admin_user_group_join')]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'group_id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: UserGroup::class, inversedBy: 'users')]
private $groups;
/** @var Collection<int, City> */
#[ORM\ManyToMany(targetEntity: City::class, inversedBy: 'users')]
#[ORM\JoinTable(name: 'eg_osm_user_city_join')]
private Collection $cities;
#[ORM\Column(type: 'datetimetz', nullable: true)]
private $createdAt;
#[ORM\Column(type: 'datetimetz', nullable: true)]
private $updatedAt;
#[ORM\OneToMany(targetEntity: DictionaryImport::class, mappedBy: 'createdBy')]
private $dictionaryImports;
public static function getSystemUsers(): array
{
return self::SYSTEM_USERS;
}
public static function getRoleSuperAdmin(): string
{
return self::ROLE_SUPER_ADMIN;
}
public function __construct()
{
$this->organizations = new ArrayCollection();
$this->groups = new ArrayCollection();
$this->cities = new ArrayCollection();
$now = new \DateTime();
$this->setCreatedAt($now);
$this->setUpdatedAt($now);
$this->dictionaryImports = new ArrayCollection();
}
public function __toString(): string
{
return $this->getUserIdentifier();
}
#[ORM\PreUpdate]
public function onPreUpdate(): void
{
$this->setUpdatedAt(new \DateTime());
}
/**
* Metoda do kanonizowania stringów (zabezpieczania pól)
*/
public function canonicalize($string)
{
if (null === $string) {
return;
}
$encoding = mb_detect_encoding($string);
$result = $encoding
? mb_convert_case($string, MB_CASE_LOWER, $encoding)
: mb_convert_case($string, MB_CASE_LOWER);
return $result;
}
public function isGranted(string $role, bool $checkSuperAdmin = true): bool
{
if ($this->hasRole($role) || $this->hasRole('ROLE_SUPER_ADMIN') && $checkSuperAdmin) {
return true;
}
foreach ($this->getGroups() as $group) {
if ($group->hasRole($role) || $group->hasRole('ROLE_SUPER_ADMIN') && $checkSuperAdmin) {
return true;
}
}
return false;
}
public function hasRole(string $role): bool
{
return \in_array(\strtoupper($role), $this->getRoles(), true);
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
$this->setUsernameCanonical(
$this->canonicalize($username)
);
return $this;
}
public function getUsernameCanonical(): ?string
{
return $this->usernameCanonical;
}
public function setUsernameCanonical(string $usernameCanonical): self
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
$this->setEmailCanonical(
$this->canonicalize($email)
);
return $this;
}
public function getEmailCanonical(): ?string
{
return $this->emailCanonical;
}
public function setEmailCanonical(string $emailCanonical): self
{
$this->emailCanonical = $emailCanonical;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getLastLogin(): ?\DateTimeInterface
{
return $this->lastLogin;
}
public function setLastLogin(?\DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function getPasswordRequestedAt(): ?\DateTimeInterface
{
return $this->passwordRequestedAt;
}
public function setPasswordRequestedAt(?\DateTimeInterface $passwordRequestedAt): self
{
$this->passwordRequestedAt = $passwordRequestedAt;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): ?array
{
$roles = $this->roles;
foreach ($this->getGroups() as $group) {
$roles = \array_merge($roles, $group->getRoles());
}
// guarantee every user at least has ROLE_USER
$roles[] = self::ROLE_DEFAULT;
return \array_unique($roles);
}
public function addRole(string $role): self
{
if (!in_array($role, $this->roles)) {
$this->roles[] = $role;
}
return $this;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getStartingEntity(): ?string
{
return $this->startingEntity;
}
public function setStartingEntity(?string $startingEntity): self
{
$this->startingEntity = $startingEntity;
return $this;
}
public function getStartingAction(): ?string
{
return $this->startingAction;
}
public function setStartingAction(?string $startingAction): self
{
$this->startingAction = $startingAction;
return $this;
}
public function getStartingId(): ?int
{
return $this->startingId;
}
public function setStartingId(?int $startingId): self
{
$this->startingId = $startingId;
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(string $color): self
{
$this->color = $color;
return $this;
}
public function getDeletedAt(): ?\DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
/** @return Collection<int, Organization> */
public function getOrganizations(): Collection
{
return $this->organizations;
}
/**
* @return Collection<int, UserGroup>
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(UserGroup $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(UserGroup $group): self
{
$this->groups->removeElement($group);
return $this;
}
/** @return Collection<int, City> */
public function getCities(): Collection
{
return $this->cities;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
//
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function isSuperAdmin(): bool
{
return $this->hasRole('ROLE_SUPER_ADMIN');
}
/**
* @return Collection<int, DictionaryImport>
*/
public function getDictionaryImports(): Collection
{
return $this->dictionaryImports;
}
public function addDictionaryImport(DictionaryImport $dictionaryImport): self
{
if (!$this->dictionaryImports->contains($dictionaryImport)) {
$this->dictionaryImports[] = $dictionaryImport;
$dictionaryImport->setCreatedBy($this);
}
return $this;
}
}