<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface as EmailTwoFactorInterface;
use Scheb\TwoFactorBundle\Model\Totp\TotpConfiguration;
use Scheb\TwoFactorBundle\Model\Totp\TotpConfigurationInterface;
use Scheb\TwoFactorBundle\Model\Totp\TwoFactorInterface as TotpTwoFactorInterface;
use Scheb\TwoFactorBundle\Model\TrustedDeviceInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(['email'])]
#[UniqueEntity(['username'])]
class User implements UserInterface,
PasswordAuthenticatedUserInterface,
EmailTwoFactorInterface,
TotpTwoFactorInterface,
TrustedDeviceInterface
{
final const USER_SUPER_ADMIN_ID = 1;
final const USER_DEFAULT_ID = 2;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Assert\Email]
#[Assert\NotNull]
private ?string $email;
#[ORM\Column(length: 255)]
// #[Assert\Length(
// min: 3,
// max: 4096,
// minMessage: " the username must have minimum {{ limit }} characters")]
// #[Assert\NotNull]
private ?string $username = null;
#[ORM\Column(type: 'json')]
private array $roles = [];
#[ORM\Column(type: 'string')]
#[Assert\Length(min: 6, minMessage: 'Your password should be at least {{ limit }} characters')]
private ?string $password= null;
#[ORM\OneToOne(inversedBy: 'user', targetEntity: Personal::class, cascade: ['persist', 'remove'])]
private ?Personal $personal;
#[ORM\Column(type: 'boolean')]
private bool $isVerified = false;
#[ORM\Column(nullable: false)]
private ?bool $active = null;
#[ORM\ManyToOne(cascade: ['persist'])]
private ?Customer $customer = null;
#[ORM\Column(length: 255,nullable: true)]
private ?string $google_id = null;
// =============================================
// Two-Factor Authentication Fields
// =============================================
/**
* Temporary 6-digit code sent via email for 2FA
*/
#[ORM\Column(type: 'string', length: 6, nullable: true)]
private ?string $emailAuthCode = null;
/**
* Base32 encoded TOTP secret for authenticator apps
*/
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $totpSecret = null;
/**
* Selected 2FA method: 'email', 'totp', or null (disabled)
*/
#[ORM\Column(type: 'string', length: 10, nullable: true)]
private ?string $twoFactorMethod = null;
/**
* Transient (non-persisted) field set from session during login flow.
*/
private ?string $sessionTwoFactorMethod = null;
/**
* Version counter for trusted device invalidation
* Increment this to invalidate all trusted devices for this user
*/
#[ORM\Column(type: 'integer', options: ['default' => 0])]
private int $trustedTokenVersion = 0;
public function __construct()
{
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string)$this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function getRole(): string
{
if ($this->roles && count($this->roles) > 0) {
return $this->roles[0];
}
return '';
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getPersonal(): ?Personal
{
return $this->personal;
}
public function setPersonal(?Personal $personal): self
{
$this->personal = $personal;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): self
{
$this->customer = $customer;
return $this;
}
public function __toString(): string
{
return $this->username;
}
public function getGoogleId(): ?string
{
return $this->google_id;
}
public function setGoogleId(string $google_id): static
{
$this->google_id = $google_id;
return $this;
}
// =============================================
// Email Two-Factor Authentication Methods
// =============================================
/**
* Return the email address to which the authentication code is sent
*/
public function getEmailAuthRecipient(): string
{
return $this->email;
}
/**
* Return the authentication code
*/
public function getEmailAuthCode(): ?string
{
return $this->emailAuthCode;
}
/**
* Set the authentication code
*/
public function setEmailAuthCode(?string $authCode): void
{
$this->emailAuthCode = $authCode;
}
/**
* Check if the user has email 2FA enabled.
* Session method takes priority over DB method.
*/
public function isEmailAuthEnabled(): bool
{
return $this->sessionTwoFactorMethod === 'email' || $this->twoFactorMethod === 'email';
}
// =============================================
// TOTP Two-Factor Authentication Methods
// =============================================
/**
* Check if the user has TOTP 2FA enabled.
* Session method takes priority over DB method.
*/
public function isTotpAuthenticationEnabled(): bool
{
return ($this->sessionTwoFactorMethod === 'totp' || $this->twoFactorMethod === 'totp')
&& $this->totpSecret !== null;
}
/**
* Return the username shown in authenticator apps
*/
public function getTotpAuthenticationUsername(): string
{
return $this->email;
}
/**
* Get TOTP configuration for the user
*/
public function getTotpAuthenticationConfiguration(): ?TotpConfigurationInterface
{
if (!$this->totpSecret) {
return null;
}
return new TotpConfiguration(
$this->totpSecret,
TotpConfiguration::ALGORITHM_SHA1,
30,
6
);
}
/**
* Get the TOTP secret
*/
public function getTotpSecret(): ?string
{
return $this->totpSecret;
}
/**
* Set the TOTP secret
*/
public function setTotpSecret(?string $totpSecret): self
{
$this->totpSecret = $totpSecret;
return $this;
}
// =============================================
// Two-Factor Method Management
// =============================================
/**
* Get the currently active 2FA method
*/
public function getTwoFactorMethod(): ?string
{
return $this->twoFactorMethod;
}
/**
* Set the 2FA method ('email', 'totp', or null to disable)
*/
public function setTwoFactorMethod(?string $method): self
{
$this->twoFactorMethod = $method;
return $this;
}
/**
* Check if user has any 2FA method enabled.
* Session method takes priority over DB method.
*/
public function isTwoFactorEnabled(): bool
{
return $this->sessionTwoFactorMethod !== null || $this->twoFactorMethod !== null;
}
// =============================================
// Session Two-Factor Method (transient)
// =============================================
public function getSessionTwoFactorMethod(): ?string
{
return $this->sessionTwoFactorMethod;
}
public function setSessionTwoFactorMethod(?string $method): self
{
$this->sessionTwoFactorMethod = $method;
return $this;
}
// =============================================
// Trusted Device Methods
// =============================================
/**
* Get trusted device token version
* Used by scheb/2fa-trusted-device to validate trusted cookies
*/
public function getTrustedTokenVersion(): int
{
return $this->trustedTokenVersion;
}
/**
* Increment token version to invalidate all trusted devices
*/
public function invalidateTrustedDevices(): self
{
$this->trustedTokenVersion++;
return $this;
}
/**
* Set trusted token version (mainly for persistence)
*/
public function setTrustedTokenVersion(int $version): self
{
$this->trustedTokenVersion = $version;
return $this;
}
}