<?php
namespace App\Entity;
use App\Repository\UserRepository;
use App\Util\Arr;
use Doctrine\Common\Collections\{ArrayCollection, Collection};
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\{PasswordAuthenticatedUserInterface, UserInterface};
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class), ORM\Table(name: 'users')]
#[UniqueEntity(fields: ['username'], message: 'A user account already exists with this username!')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, \Stringable
{
#[ORM\Id, ORM\GeneratedValue, ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 50, unique: true), Assert\NotBlank, Assert\Length(min: 2, max: 50)]
private $username;
#[ORM\Column(type: 'string')]
private $password;
#[ORM\Column(type: 'string', nullable: true)]
private $password_old1;
#[ORM\Column(type: 'string', nullable: true)]
private $password_old2;
#[ORM\Column(type: 'string')]
private $password_md5;
#[ORM\Column(type: 'boolean')]
private bool $isActive = false;
#[ORM\Column(type: 'string', nullable: true)]
private $token;
#[ORM\OneToOne(targetEntity: 'StaffProfile', inversedBy: 'user', cascade: ['persist', 'remove']), ORM\JoinColumn(nullable: true)]
private $profile;
#[ORM\Column(type: 'json')]
private array $roles = [];
#[ORM\Column(type: 'datetime', nullable: true)]
private $lastLogin;
#[ORM\Column(type: 'string', nullable: true)]
private $authCode;
#[Gedmo\Timestampable(on: 'create'), ORM\Column(type: 'datetime')]
private $createdAt;
#[Gedmo\Timestampable(on: 'update'), ORM\Column(type: 'datetime')]
private $updatedAt;
#[ORM\OneToMany(targetEntity: UserLoginHistory::class, mappedBy: 'user', orphanRemoval: true)]
private $loginHistory;
public function __construct()
{
$this->loginHistory = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUserIdentifier(): string
{
return $this->username;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getPasswordOld1(): ?string
{
return $this->password_old1;
}
public function setPasswordOld1(string $password_old1): self
{
$this->password_old1 = $password_old1;
return $this;
}
public function getPasswordOld2(): ?string
{
return $this->password_old2;
}
public function setPasswordOld2(string $password_old2): self
{
$this->password_old2 = $password_old2;
return $this;
}
public function getPasswordMd5(): ?string
{
return $this->password_md5;
}
public function setPasswordMd5(string $password_md5): self
{
$this->password_md5 = $password_md5;
return $this;
}
public function getSalt(): ?string
{
return null;
}
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(?string $token): self
{
$this->token = $token;
return $this;
}
public function getProfile(): ?StaffProfile
{
return $this->profile;
}
public function setProfile(StaffProfile $profile): self
{
$this->profile = $profile;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function getRoles(): array
{
$roles = $this->roles;
return $roles;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getLastLogin(): ?\DateTimeInterface
{
return $this->lastLogin;
}
public function setLastLogin(?\DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
/**
* @return Collection|UserLoginHistory[]
*/
public function getLoginHistory(): Collection
{
return $this->loginHistory;
}
public function addLoginHistory(UserLoginHistory $loginHistory): self
{
if (!$this->loginHistory->contains($loginHistory)) {
$this->loginHistory[] = $loginHistory;
$loginHistory->setUser($this);
}
return $this;
}
public function removeLoginHistory(UserLoginHistory $loginHistory): self
{
if ($this->loginHistory->removeElement($loginHistory)) {
if ($loginHistory->getUser() === $this) {
$loginHistory->setUser(null);
}
}
return $this;
}
public function getEmailAuthCode(): string
{
if (null === $this->authCode) {
throw new \LogicException('The email authentication code was not set');
}
return $this->authCode;
}
public function setEmailAuthCode(string $authCode): void
{
$this->authCode = $authCode;
}
public function getRole(): string
{
$roles = $this->roles;
$roleDisplay = (new Arr())->getUserRolesArr(true);
return is_array($roleDisplay) && isset($roles[0]) ? $roleDisplay[$roles[0]] : '';
}
public function getHospital()
{
return $this->profile->getHospital();
}
public function getHospitalType()
{
return $this->profile->getHospital()->getType();
}
public function isEmailAuthEnabled(): bool
{
return true;
}
public function getEmailAuthRecipient(): string
{
return $this->profile->getEmail();
}
public function __toString(): string
{
return $this->profile->getFullName();
}
public function getAuthCode(): ?string
{
return $this->authCode;
}
public function setAuthCode(?string $authCode): self
{
$this->authCode = $authCode;
return $this;
}
public function __serialize(): array
{
return [$this->id, $this->username, $this->password];
}
public function __unserialize(array $data): void
{
[$this->id, $this->username, $this->password] = $data;
}
}