<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* User
*
* @ORM\Table(name="User")
* @ORM\Entity(repositoryClass="App\Entity\UserRepository")
* @UniqueEntity(
* fields="email",
* message="Cette adresse email est déjà utilisée.",
* groups={"registration", "update"}
* )
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_PRESTATAIRE = 'ROLE_PRESTATAIRE';
public static function createWithId($id)
{
$instance = new self();
$instance->id = $id;
return $instance;
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
* @Assert\Email(
* message="Veuillez entrer une adresse email valide."
* )
* @Assert\NotBlank(
* message="Veuillez renseigner ce champ.",
* groups={"registration","request_reset", "update"}
* )
*/
private $email;
/**
* @var boolean
*
* @ORM\Column(name="is_email_valid", type="boolean")
*/
private $emailValid;
/**
* @var string
*
* @ORM\Column(name="email_validation_code", type="string", length=255, nullable=true)
*/
private $emailValidationCode;
/**
* @var string
*/
private $rawEmailValidationCode;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var string
* @Assert\NotBlank(
* message="Veuillez renseigner ce champ.",
* groups={"registration", "reset"}
* )
*/
private $rawPassword;
/**
* @var string
*
* @ORM\Column(name="reset_code", type="string", length=255, nullable=true)
*/
private $resetCode;
/**
* @var string
*
* @ORM\Column(name="roles", type="string", type="simple_array")
*/
private $roles;
/**
* @var Prestataire
*
* @ORM\OneToOne(targetEntity="Prestataire", mappedBy="user", cascade={"remove"})
*/
private $prestataire;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
public function __construct()
{
$this->roles = array(self::ROLE_PRESTATAIRE);
$this->emailValid = false;
}
public function getUserIdentifier(): string
{
return (string) $this->id;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @return boolean
*/
public function isEmailValid()
{
return $this->emailValid;
}
/**
* @param boolean $emailValid
*/
public function setEmailValid($emailValid)
{
$this->emailValid = $emailValid;
}
/**
* Set emailValidationCode
*
* @param string $emailValidationCode
*
* @return Prestataire
*/
public function setEmailValidationCode($emailValidationCode)
{
$this->emailValidationCode = $emailValidationCode;
return $this;
}
/**
* Get emailValidationCode
*
* @return string
*/
public function getEmailValidationCode()
{
return $this->emailValidationCode;
}
/**
* @return string
*/
public function getRawEmailValidationCode()
{
return $this->rawEmailValidationCode;
}
/**
* @param string $rawEmailValidationCode
*/
public function setRawEmailValidationCode($rawEmailValidationCode)
{
$this->rawEmailValidationCode = $rawEmailValidationCode;
}
/**
* Returns the username used to authenticate the user.
*
* @return string The username
*/
public function getUsername()
{
return $this->getEmail();
}
/**
* Set pass
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get pass
*
* @return string|null
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* Set resetCode
*
* @param string $resetCode
* @return User
*/
public function setResetCode($resetCode)
{
$this->resetCode = $resetCode;
return $this;
}
/**
* Get resetCode
*
* @return string
*/
public function getResetCode()
{
return $this->resetCode;
}
public function isGranted(string $role): bool
{
if (in_array($role, $this->getRoles())):
return true;
else: return false; endif;
}
/**
* Set role
*
* @param string $roles
*
* @return User
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get role
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
public function hasRole($role)
{
return array_search($role, $this->getRoles()) !== false;
}
public function addRole($role)
{
$roles = $this->getRoles();
$roles[] = $role;
$this->setRoles(array_unique($roles));
}
public function removeRole($role)
{
$roles = $this->getRoles();
$offset = array_search($role, $roles);
if ($offset !== false) {
unset($roles[$offset]);
$this->setRoles($roles);
}
}
public function isPrestataire()
{
return $this->hasRole(self::ROLE_PRESTATAIRE);
}
public function isAdmin()
{
return $this->hasRole(self::ROLE_ADMIN);
}
/**
* @return Prestataire
*/
public function getPrestataire()
{
return $this->prestataire;
}
/**
* @param Prestataire $prestataire
*/
public function setPrestataire($prestataire)
{
$this->prestataire = $prestataire;
return $this;
}
/**
* Checks whether the user's account has expired.
*
* Internally, if this method returns false, the authentication system
* will throw an AccountExpiredException and prevent login.
*
* @return bool true if the user's account is non expired, false otherwise
*
* @see AccountExpiredException
*/
public function isAccountNonExpired()
{
return true;
}
/**
* Checks whether the user is locked.
*
* Internally, if this method returns false, the authentication system
* will throw a LockedException and prevent login.
*
* @return bool true if the user is not locked, false otherwise
*
* @see LockedException
*/
public function isAccountNonLocked()
{
return true;
}
/**
* Checks whether the user's credentials (password) has expired.
*
* Internally, if this method returns false, the authentication system
* will throw a CredentialsExpiredException and prevent login.
*
* @return bool true if the user's credentials are non expired, false otherwise
*
* @see CredentialsExpiredException
*/
public function isCredentialsNonExpired()
{
return true;
}
/**
* Checks whether the user is enabled.
*
* Internally, if this method returns false, the authentication system
* will throw a DisabledException and prevent login.
*
* @return bool true if the user is enabled, false otherwise
*
* @see DisabledException
*/
public function isEnabled()
{
if ($this->isPrestataire() && $this->getPrestataire()) {
return $this->getPrestataire()->isValidated();
}
return true;
}
/**
* Returns the salt that was originally used to encode the password.
*
* This can return null if the password was not encoded using a salt.
*
* @return string|null The salt
*/
public function getSalt()
{
return $this->getPassword();
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
}
/**
* Get emailValid
*
* @return boolean
*/
public function getEmailValid()
{
return $this->emailValid;
}
public function getRawPassword()
{
return $this->rawPassword;
}
public function setRawPassword($password)
{
$this->rawPassword = $password;
return $this;
}
public function __toString()
{
return $this->email;
}
public function __serialize(): array
{
return [
'id' => $this->id,
'email' => $this->email,
'password' => $this->password,
];
}
public function __unserialize(array $data): void
{
$this->id = $data['id'];
$this->email = $data['email'];
$this->password = $data['password'];
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
}