<?php
namespace App\Entity\Data;
use App\Repository\Data\UserRepository;
use App\Entity\System\Country;
use App\Service\Helper;
use App\Traits\TimestampableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Validator as AppAssert;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
* @ORM\HasLifecycleCallbacks()
* @AppAssert\UserEmail(groups={"register", "profile"})
*/
class User implements UserInterface, EquatableInterface, PasswordAuthenticatedUserInterface
{
use TimestampableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\NotNull(message="validate.field.empty")
* @Assert\Email()
*/
private ?string $email;
/**
* @ORM\Column(type="json")
*/
private array $roles;
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private string $password;
/**
* @var string|null Plain password
* @Assert\NotNull(groups={"register", "recover_password"}, message="validate.field.empty")
* @AppAssert\PasswordStrength(min=8, is_pwned=true, rules={"digit", "special", "lower_letter", "upper_letter"})
*/
private ?string $newPassword;
/**
* @ORM\ManyToOne(targetEntity=UserGroup::class, inversedBy="users")
*/
private $userGroup;
/**
* @ORM\OneToMany(targetEntity=Company::class, mappedBy="user")
*/
private $companies;
/**
* @ORM\ManyToOne(targetEntity=Referer::class, inversedBy="users")
*/
private $referer;
/**
* @ORM\Column(type="string", length=5, nullable=true)
*/
private $locale;
/**
* @ORM\Column(type="string", length=20, nullable=true)
* @Assert\NotNull(message="validate.field.empty", groups="register2")
* @AppAssert\Phone(message="validate.format.wrong", groups="register2")
*/
private $mobile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $securityQuestion1;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $securityAnswer1;
private $newSecurityAnswer1;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $securityQuestion2;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $securityAnswer2;
private $newSecurityAnswer2;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email(groups="register2")
* @AppAssert\UserEmailAlternative(groups="register2")
*/
private $emailAlternative;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $lastIp;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastGeoCity;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $lastGeoCountryId;
/** @var Country */
private $lastGeoCountry;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastLoginOn;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $deletedOn;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $failedLoginCount;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastSecurityType;
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $salutation;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotNull(groups={"profile"})
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotNull(groups={"profile"})
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $emailConfirmationCode;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $emailConfirmationExpiryOn;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isEmailConfirmed;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $recoverPasswordCode;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $recoverPasswordCodeExpiryOn;
/**
* @var string|null
*
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $emailResendRequestCode;
/**
* @var \DateTime|null
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $emailResendRequestCodeExpiryOn;
/**
* @ORM\Column(type="string", length=10, nullable=true)
*/
private $otp;
/**
* @Assert\Expression("this.getOtp() === value", groups="register3")
* @Assert\NotNull(groups="register3")
*/
private $otpNew;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $maidenName;
/**
* @ORM\Column(type="string", length=1, nullable=true)
*/
private $gender;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $birthDate;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $birthCountryId;
/** @var Country */
private $birthCountry;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $birthPlace;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $nationalityId;
/** @var Country */
private $nationality;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $nationalityFormerId;
/** @var Country */
private $nationalityFormer;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $nationalityAdditionalId;
/** @var Country */
private $nationalityAdditional;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $localeAdditional;
/**
* User constructor.
*/
public function __construct()
{
$this->roles = [];
$this->isEmailConfirmed = false;
$this->companies = new ArrayCollection();
}
public function isEqualTo(UserInterface $user): bool
{
if (!$user instanceof User) {
return false;
}
if ($this->id !== $user->getId()) {
return false;
}
return true;
}
/**
* @ORM\PostLoad()
*/
public function loadLinkedEntities($event) {
// $s = '';
}
/**
* @ORM\PostPersist()
*/
public function saveLinkedEntities(LifecycleEventArgs $event) {
// $em = $event->getEntityManager();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = strtolower($email);
return $this;
}
public function getUrlEmail() {
return base64_encode($this->email);
}
public static function urlEmailDecode($email) : string {
return base64_decode($email);
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function addRole($role) {
if (strpos($role, 'ROLE_') !== 0) {
$role = 'ROLE_' . $role;
}
$this->roles[] = $role;
}
public function removeRole($role) {
if (strpos($role, 'ROLE_') !== 0) {
$role = 'ROLE_' . $role;
}
if (in_array($role, $this->roles)) {
$key = array_search($role, $this->roles);
if ($key !== false) {
unset($this->roles[$key]);
}
}
$this->roles = array_values($this->roles);
}
public function hasRole($role) {
if (strpos($role, 'ROLE_') !== 0) {
$role = 'ROLE_' . $role;
}
return in_array($role, $this->roles);
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
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 UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getUserGroup(): ?UserGroup
{
return $this->userGroup;
}
public function setUserGroup(?UserGroup $userGroup): self
{
$this->userGroup = $userGroup;
return $this;
}
/**
* @return Collection|self[]
*/
public function getCompanies(): Collection
{
return $this->companies;
}
public function addCompany(Company $company): self
{
if (!$this->companies->contains($company)) {
$this->companies[] = $company;
$company->setUser($this);
}
return $this;
}
public function removeCompany(Company $company): self
{
if ($this->companies->removeElement($company)) {
// set the owning side to null (unless already changed)
if ($company->getUser() === $this) {
$company->setUser(null);
}
}
return $this;
}
public function getReferer(): ?Referer
{
return $this->referer;
}
public function setUserReferer(?Referer $referer): self
{
$this->referer = $referer;
return $this;
}
public function getLocale(): ?string
{
return $this->locale;
}
public function setLocale(?string $locale): self
{
$this->locale = $locale;
return $this;
}
public function getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(?string $mobile): self
{
$this->mobile = $mobile;
return $this;
}
public function getSecurityQuestion1(): ?string
{
return $this->securityQuestion1;
}
public function setSecurityQuestion1(?string $securityQuestion1): self
{
$this->securityQuestion1 = $securityQuestion1;
return $this;
}
public function getSecurityAnswer1(): ?string
{
return $this->securityAnswer1;
}
public function setSecurityAnswer1(?string $securityAnswer1): self
{
$this->securityAnswer1 = $securityAnswer1;
return $this;
}
/**
* @return mixed
*/
public function getNewSecurityAnswer1()
{
return $this->newSecurityAnswer1;
}
/**
* @param mixed $newSecurityAnswer1
*/
public function setNewSecurityAnswer1($newSecurityAnswer1): void
{
$this->newSecurityAnswer1 = $newSecurityAnswer1;
}
public function getSecurityQuestion2(): ?string
{
return $this->securityQuestion2;
}
public function setSecurityQuestion2(?string $securityQuestion2): self
{
$this->securityQuestion2 = $securityQuestion2;
return $this;
}
public function getSecurityAnswer2(): ?string
{
return $this->securityAnswer2;
}
public function setSecurityAnswer2(?string $securityAnswer2): self
{
$this->securityAnswer2 = $securityAnswer2;
return $this;
}
/**
* @return mixed
*/
public function getNewSecurityAnswer2()
{
return $this->newSecurityAnswer2;
}
/**
* @param mixed $newSecurityAnswer2
*/
public function setNewSecurityAnswer2($newSecurityAnswer2): void
{
$this->newSecurityAnswer2 = $newSecurityAnswer2;
}
public function getEmailAlternative(): ?string
{
return $this->emailAlternative;
}
public function setEmailAlternative(?string $emailAlternative): self
{
$this->emailAlternative = strtolower($emailAlternative);
return $this;
}
public function getLastIp(): ?string
{
return $this->lastIp;
}
public function setLastIp(?string $lastIp): self
{
$this->lastIp = $lastIp;
return $this;
}
public function getLastGeoCity(): ?string
{
return $this->lastGeoCity;
}
public function setLastGeoCity(?string $lastGeoCity): self
{
$this->lastGeoCity = $lastGeoCity;
return $this;
}
public function getLastGeoCountry(): ?Country
{
return $this->lastGeoCountry;
}
public function setLastGeoCountry(?string $lastGeoCountry): self
{
$this->lastGeoCountry = $lastGeoCountry;
return $this;
}
public function getLastLoginOn(): ?\DateTimeInterface
{
return $this->lastLoginOn;
}
public function setLastLoginOn(?\DateTimeInterface $lastLoginOn): self
{
$this->lastLoginOn = $lastLoginOn;
return $this;
}
public function getDeletedOn(): ?\DateTimeInterface
{
return $this->deletedOn;
}
public function setDeletedOn(?\DateTimeInterface $deletedOn): self
{
$this->deletedOn = $deletedOn;
return $this;
}
public function getFailedLoginCount(): ?int
{
return $this->failedLoginCount;
}
public function setFailedLoginCount(?int $failedLoginCount): self
{
$this->failedLoginCount = $failedLoginCount;
return $this;
}
public function getLastSecurityType(): ?string
{
return $this->lastSecurityType;
}
public function setLastSecurityType(?string $lastSecurityType): self
{
$this->lastSecurityType = $lastSecurityType;
return $this;
}
public function getSalutation(): ?string
{
return $this->salutation;
}
public function setSalutation(?string $salutation): self
{
$this->salutation = $salutation;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getFullName() {
return $this->firstName . ' ' . $this->lastName;
}
/**
* @return string
*/
public function getNewPassword(): ?string
{
return $this->newPassword;
}
/**
* @param string $newPassword
*/
public function setNewPassword(?string $newPassword): void
{
$this->newPassword = $newPassword;
}
public function getEmailConfirmationCode(): ?string
{
return $this->emailConfirmationCode;
}
public function setEmailConfirmationCode(?string $emailConfirmationCode): self
{
$this->emailConfirmationCode = $emailConfirmationCode;
return $this;
}
public function getEmailConfirmationExpiryOn(): ?\DateTimeInterface
{
return $this->emailConfirmationExpiryOn;
}
public function setEmailConfirmationExpiryOn(?\DateTimeInterface $emailConfirmationExpiryOn): self
{
$this->emailConfirmationExpiryOn = $emailConfirmationExpiryOn;
return $this;
}
/**
* @return mixed
*/
public function getIsEmailConfirmed()
{
return $this->isEmailConfirmed ?? false;
}
/**
* @param mixed $isEmailConfirmed
*/
public function setIsEmailConfirmed($isEmailConfirmed): void
{
$this->isEmailConfirmed = $isEmailConfirmed;
}
public function prepareEmailConfirmation() {
$this->setIsEmailConfirmed(false);
$this->emailConfirmationCode = Helper::generateString();
$datNow = new \DateTime();
$datNow->add(new \DateInterval('P1D'));
$this->emailConfirmationExpiryOn = $datNow;
}
public function getRecoverPasswordCode(): ?string
{
return $this->recoverPasswordCode;
}
public function setRecoverPasswordCode(?string $recoverPasswordCode): self
{
$this->recoverPasswordCode = $recoverPasswordCode;
return $this;
}
public function getRecoverPasswordCodeExpiryOn(): ?\DateTimeInterface
{
return $this->recoverPasswordCodeExpiryOn;
}
public function setRecoverPasswordCodeExpiryOn(?\DateTimeInterface $recoverPasswordCodeExpiryOn): self
{
$this->recoverPasswordCodeExpiryOn = $recoverPasswordCodeExpiryOn;
return $this;
}
public function getOtp(): ?string
{
return $this->otp;
}
public function setOtp(?string $otp): self
{
$this->otp = $otp;
return $this;
}
public function getOtpNew(): ?string
{
return $this->otpNew;
}
public function setOtpNew(?string $otpNew): self
{
$this->otpNew = $otpNew;
return $this;
}
/**
* @return mixed
*/
public function getEmailResendRequestCode()
{
return $this->emailResendRequestCode;
}
/**
* @param mixed $emailResendRequestCode
*/
public function setEmailResendRequestCode($emailResendRequestCode): void
{
$this->emailResendRequestCode = $emailResendRequestCode;
}
/**
* @return \DateTime|null
*/
public function getEmailResendRequestCodeExpiryOn(): ?\DateTime
{
return $this->emailResendRequestCodeExpiryOn;
}
/**
* @param \DateTime|null $emailResendRequestCodeExpiryOn
*/
public function setEmailResendRequestCodeExpiryOn(?\DateTime $emailResendRequestCodeExpiryOn): void
{
$this->emailResendRequestCodeExpiryOn = $emailResendRequestCodeExpiryOn;
}
public function getMaidenName(): ?string
{
return $this->maidenName;
}
public function setMaidenName(?string $maidenName): self
{
$this->maidenName = $maidenName;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(?string $gender): self
{
$this->gender = $gender;
return $this;
}
public function toArray() {
return [
'id' => $this->id,
'salutation' => $this->salutation,
'title' => $this->title,
'firstName' => $this->firstName,
'lastName' => $this->lastName,
'maidenName' => $this->maidenName,
'email' => $this->email,
'mobile' => $this->mobile,
'emailAlternative' => $this->emailAlternative,
'securityQuestion1' => $this->securityQuestion1,
'securityQuestion2' => $this->securityQuestion2,
'gender' => $this->gender,
'birthDate' => $this->birthDate,
'birthCountryId' => $this->birthCountryId,
'birthPlace' => $this->birthPlace,
'nationalityId' => $this->nationalityId,
'formerNationalityId'=> $this->nationalityFormerId,
'additionalNationalityId' => $this->nationalityAdditionalId,
'locale' => $this->locale,
'additionalLocale' => $this->localeAdditional
];
}
public function getBirthDate(): ?\DateTimeInterface
{
return $this->birthDate;
}
public function setBirthDate(?\DateTimeInterface $birthDate): self
{
$this->birthDate = $birthDate;
return $this;
}
public function getBirthCountry(): ?Country
{
return $this->birthCountry;
}
public function setBirthCountry(?Country $birthCountry): self
{
$this->birthCountry = $birthCountry;
return $this;
}
public function getBirthPlace(): ?string
{
return $this->birthPlace;
}
public function setBirthPlace(?string $birthPlace): self
{
$this->birthPlace = $birthPlace;
return $this;
}
public function getNationality(): ?Country
{
return $this->nationality;
}
public function setNationality(?Country $nationality): self
{
$this->nationality = $nationality;
return $this;
}
public function getNationalityFormer(): ?Country
{
return $this->nationalityFormer;
}
public function setNationalityFormer(?Country $nationalityFormer): self
{
$this->nationalityFormer = $nationalityFormer;
return $this;
}
public function getNationalityAdditional(): ?Country
{
return $this->nationalityAdditional;
}
public function setNationalityAdditional(?Country $nationalityAdditional): self
{
$this->nationalityAdditional = $nationalityAdditional;
return $this;
}
public function getLocaleAdditional(): ?string
{
return $this->localeAdditional;
}
public function setLocaleAdditional(?string $localeAdditional): self
{
$this->localeAdditional = $localeAdditional;
return $this;
}
public function getUserIdentifier(): string
{
return $this->email;
}
}