<?php
namespace App\Entity;
use App\Attribute\Searchable;
use Overblog\GraphQLBundle\Annotation\Field;
use App\Repository\UserRepository;
use App\Traits\DateTimeTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: "email", message: "This email is already used.")]
class User extends BasicEntity implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
{
use DateTimeTrait;
public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_CUSTOMER = 'ROLE_CUSTOMER';
public const ROLE_OWNER = 'ROLE_OWNER';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Field(name: "id", type: "int")]
private $id;
#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
#[Field(name: "first_name", type: "string")]
#[Searchable(name: 'first_name')]
private $firstName;
#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
#[Field(name: "last_name", type: "string")]
#[Searchable(name: 'last_name')]
private $lastName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Assert\Email]
#[Field(name: "email", type: "string")]
#[Searchable(name: 'email')]
private $email;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[RollerworksPassword\PasswordStrength(
minStrength: 4,
minLength: 12,
message: "Your password must contain at least 12 alphanumeric characters"
)]
#[Assert\NotCompromisedPassword]
#[Field(name: "password", type: "string")]
private $password;
#[ORM\Column(type: 'string', length: 30, nullable: true)]
#[Field(name: "phone_number", type: "string")]
#[Searchable(name: 'phone_number')]
private $phoneNumber;
#[ORM\Column(type: 'json')]
#[Field(name: "roles", type: "string")]
private $roles = [];
#[ORM\Column(type: 'boolean')]
#[Field(name: "is_actif", type: "boolean")]
private $isActif;
#[ORM\Column(type: 'string', length: 255)]
private $registration_token;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Field(name: "avatar", type: "string")]
private $avatar;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Field(name: "forgotpwd_token", type: "string")]
private $forgotpwd_token;
#[ORM\Column(name: 'googleAuthenticatorSecret', type: 'string', nullable: true)]
private $googleAuthenticatorSecret;
#[ORM\Column(type: 'boolean')]
#[Field(name: "is_enabled_2fa", type: "boolean")]
private $isEnabled2FA;
#[ORM\Column(type: 'boolean')]
#[Field(name: "is_enable_notif", type: "boolean")]
private $isEnableNotif = true;
#[ORM\ManyToMany(targetEntity: Service::class)]
private $notifications;
#[ORM\ManyToMany(targetEntity: Organisation::class, inversedBy: "users")]
#[Field(name: "organisations")]
private $organisations;
#[ORM\Column(options: ['default' => false])]
#[Field(name: "is_deleted", type: "boolean")]
private ?bool $isDeleted = false;
#[ORM\Column(options: ['default' => 0])]
private ?int $attemptRecoverPwd = 0;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $forgotPwdExpireAt = null;
#[ORM\Column(length: 255, nullable: true)]
#[Field(name: "api_token", type: "string")]
private ?string $apiToken = null;
#[ORM\OneToMany(mappedBy: 'calledBy', targetEntity: WebServiceLog::class)]
private Collection $webServiceLogs;
public function __construct()
{
$this->notifications = new ArrayCollection();
$this->organisations = new ArrayCollection();
$this->webServiceLogs = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): User
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): User
{
$this->lastName = $lastName;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): User
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): User
{
$this->password = $password;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(?string $phoneNumber): User
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getRoles(): ?array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getSalt(): ?string
{
return null;
}
public function eraseCredentials()
{
}
public function getUsername(): string
{
return $this->getUserIdentifier();
}
public function getUserIdentifier(): string
{
return $this->email;
}
public function isAdmin(): bool
{
return in_array(self::ROLE_ADMIN, $this->roles);
}
public function isOwner(): bool
{
return in_array(self::ROLE_OWNER, $this->roles);
}
public function isCustomer(): bool
{
return in_array(self::ROLE_CUSTOMER, $this->roles);
}
public function getIsActif(): ?bool
{
return $this->isActif;
}
public function setIsActif(bool $isActif): self
{
$this->isActif = $isActif;
return $this;
}
public function getRegistrationToken(): ?string
{
return $this->registration_token;
}
public function setRegistrationToken(string $registration_token): self
{
$this->registration_token = $registration_token;
return $this;
}
#[ORM\PrePersist]
public function generateAndSaveToken(): void
{
$this->setRegistrationToken(md5(uniqid().$this->email));
}
// public function getAvatar(): ?string
// {
// return $this->avatar;
// }
public function getAvatar(): ?string
{
$path = __DIR__.'/../../public/uploads/avatar/'.$this->avatar; // Path to the image file
if (!empty($this->avatar) && file_exists($path)) {
$type = pathinfo($path, PATHINFO_EXTENSION); // Get the file extension
$data = file_get_contents($path); // Read the file's contents
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); // Convert to base64
return $base64; // Return the base64 encoded string
}
return null; // Return null if the file doesn't exist
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getForgotpwdToken(): ?string
{
return $this->forgotpwd_token;
}
public function setForgotpwdToken(?string $forgotpwd_token): self
{
$this->forgotpwd_token = $forgotpwd_token;
return $this;
}
public function getGoogleAuthenticatorSecret(): ?string
{
return $this->googleAuthenticatorSecret;
}
public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
{
$this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
}
public function isGoogleAuthenticatorEnabled(): bool
{
return $this->getIsEnabled2FA();
}
public function getGoogleAuthenticatorUsername(): string
{
return $this->getUsername();
}
public function getIsEnabled2FA(): ?bool
{
return $this->isEnabled2FA;
}
public function setIsEnabled2FA(bool $isEnabled2FA): self
{
$this->isEnabled2FA = $isEnabled2FA;
return $this;
}
public function getIsEnableNotif(): ?bool
{
return $this->isEnableNotif;
}
public function setIsEnableNotif(bool $isEnableNotif): self
{
$this->isEnableNotif = $isEnableNotif;
return $this;
}
/**
* @return Collection<int, Service>
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Service $notification): self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
}
return $this;
}
public function removeNotification(Service $notification): self
{
$this->notifications->removeElement($notification);
return $this;
}
public function haveNotif(Service $service): bool
{
return $this->notifications->contains($service);
}
/**
* @return Collection<int, Organisation>
*/
public function getOrganisations(): Collection
{
return $this->organisations;
}
public function addOrganisation(Organisation $organisation): self
{
if (!$this->organisations->contains($organisation)) {
$this->organisations[] = $organisation;
}
return $this;
}
public function removeOrganisation(Organisation $organisation): self
{
$this->organisations->removeElement($organisation);
return $this;
}
/**
* @param array $organisations
* @return $this
*/
public function setOrganisations(array $organisations): self
{
$this->organisations = new ArrayCollection();
foreach ($organisations as $organisation) {
$this->addOrganisation($organisation);
}
return $this;
}
public function isIsDeleted(): ?bool
{
return $this->isDeleted;
}
public function setIsDeleted(bool $isDeleted): static
{
$this->isDeleted = $isDeleted;
return $this;
}
public function getAttemptRecoverPwd(): ?int
{
return $this->attemptRecoverPwd;
}
public function setAttemptRecoverPwd(int $attemptRecoverPwd): static
{
$this->attemptRecoverPwd = $attemptRecoverPwd;
return $this;
}
public function getForgotPwdExpireAt(): ?\DateTimeImmutable
{
return $this->forgotPwdExpireAt;
}
public function setForgotPwdExpireAt(?\DateTimeImmutable $forgotPwdExpireAt): static
{
$this->forgotPwdExpireAt = $forgotPwdExpireAt;
return $this;
}
public function getApiToken(): ?string
{
return $this->apiToken;
}
public function setApiToken(string $apiToken): static
{
$this->apiToken = $apiToken;
return $this;
}
/**
* @return Collection<int, WebServiceLog>
*/
public function getWebServiceLogs(): Collection
{
return $this->webServiceLogs;
}
public function addWebServiceLog(WebServiceLog $webServiceLog): static
{
if (!$this->webServiceLogs->contains($webServiceLog)) {
$this->webServiceLogs->add($webServiceLog);
$webServiceLog->setCalledBy($this);
}
return $this;
}
public function removeWebServiceLog(WebServiceLog $webServiceLog): static
{
if ($this->webServiceLogs->removeElement($webServiceLog)) {
// set the owning side to null (unless already changed)
if ($webServiceLog->getCalledBy() === $this) {
$webServiceLog->setCalledBy(null);
}
}
return $this;
}
}