<?php
namespace App\Entity\Commons\OAuth2;
use App\Entity\CommonEntityInterface;
use App\Metadata\Annotation as Cdp;
use App\Validator\Constraints as AppAssert;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\UserInterface;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use League\OAuth2\Server\Entities\UserEntityInterface;
use Serializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table("oauth2__user")
* @ORM\Entity
*
* @Cdp\Schema(
* singular = "Utilisateur",
* plural = "Utilisateurs",
* frozenColumns = {"id", "email", "connectedAt"},
* linkColumns = {"id", "email", "connectedAt"},
* defaultOrder = {"email", "ASC"},
* defaultColumns = {
* "id",
* "email",
* "connectedAt",
* },
* searchFields = {"email"},
* tabs={
* @Cdp\FormView\Tab("infos_generales", label="Informations générales", fieldsets={
* @Cdp\FormView\Fieldset("infos_generales_1"),
* }),
* }
* )
*
* @UniqueEntity(
* fields={"email"},
* errorPath="email",
* message="Un compte existe déjà avec cet email"
* )
*/
class User implements UserInterface, UserEntityInterface, CommonEntityInterface, Serializable
{
use TimestampableEntity;
/**
* @var int
*
* @Cdp\Field(type="integer")
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @Cdp\Field(
* type="email",
* label="Courriel",
* formTab="infos_generales",
* formFieldset="infos_generales_1",
* formFieldsetPosition=1,
* )
* @Assert\Email
*
* @ORM\Column(type="string", length=255, unique=true)
*/
protected $email;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $password;
/**
* @var string
*
* @Assert\NotBlank(groups={"reset_password"})
* @AppAssert\Password(groups={"reset_password"})
*/
protected $plainPassword;
/**
* @var DateTimeImmutable
*
* @Cdp\Field(
* type="date",
* label="Date de dernière connexion",
* required=true,
* onForm=false,
* formTab="infos_generales",
* formFieldset="infos_generales_1",
* formFieldsetPosition=2,
* )
* @Assert\Date
*
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
protected $connectedAt;
/**
* @var DateTimeImmutable
*
* @Cdp\Field(
* type="date",
* label="Date de désactivation",
* required=true,
* formTab="infos_generales",
* formFieldset="infos_generales_1",
* formFieldsetPosition=3,
* )
* @Assert\Date
*
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
protected $disabledAt;
/**
* @var DateTimeImmutable
*
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
protected $passwordRequestedAt;
/**
* @var string
*
* @ORM\Column(type="string", length=255, unique=true, nullable=true)
*/
protected $passwordResetToken;
/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=true, options={"default" : 0})
*/
protected $bridgeServiceUser = false;
public function __toString()
{
return (string) $this->email;
}
////////////////////////////// UserInterface ////////////////////////////
/**
* Returns the user unique id.
*
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* Sets the username.
*
* @param string $username
*
* @return static
*/
public function setUsername($username)
{
return $this->setEmail($username);
}
/**
* Gets the canonical username in search and sort queries.
*
* @return string
*/
public function getUsernameCanonical()
{
return $this->getEmail();
}
/**
* Sets the canonical username.
*
* @param string $usernameCanonical
*
* @return static
*/
public function setUsernameCanonical($usernameCanonical)
{
return $this->setEmail($usernameCanonical);
}
/**
* @param string|null $salt
*
* @return static
*/
public function setSalt($salt)
{
return null;
}
/**
* Gets email.
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Sets the email.
*
* @param string $email
*
* @return static
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Gets the canonical email in search and sort queries.
*
* @return string
*/
public function getEmailCanonical()
{
return $this->getEmail();
}
/**
* Sets the canonical email.
*
* @param string $emailCanonical
*
* @return static
*/
public function setEmailCanonical($emailCanonical)
{
return $this->setEmail($emailCanonical);
}
/**
* Gets the plain password.
*
* @return string
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* Sets the plain password.
*
* @param string $password
*
* @return static
*/
public function setPlainPassword($password)
{
$this->plainPassword = $password;
return $this;
}
/**
* Sets the hashed password.
*
* @param string $password
*
* @return static
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Tells if the the given user has the super admin role.
*
* @return bool
*/
public function isSuperAdmin()
{
// TODO: Implement setSuperAdmin() method.
return true;
}
/**
* @param bool $boolean
*
* @return static
*/
public function setEnabled($enable)
{
if (true === $enable) {
$this->disabledAt = null;
}
if (false === $enable) {
$this->disabledAt = new DateTimeImmutable();
}
return $this;
}
/**
* Sets the super admin status.
*
* @param bool $boolean
*
* @return static
*/
public function setSuperAdmin($boolean)
{
// TODO: Implement setSuperAdmin() method.
return $this;
}
/**
* Gets the confirmation token.
*
* @return string|null
*/
public function getConfirmationToken()
{
return $this->passwordResetToken;
}
/**
* Sets the confirmation token.
*
* @param string|null $confirmationToken
*
* @return static
*/
public function setConfirmationToken($confirmationToken)
{
$this->passwordResetToken = $confirmationToken;
return $this;
}
/**
* Sets the timestamp that the user requested a password reset.
*
* @return static
*/
public function setPasswordRequestedAt(\DateTime $date = null)
{
if ($date instanceof \DateTime) {
$this->passwordRequestedAt = DateTimeImmutable::createFromMutable($date);
} else {
$this->passwordRequestedAt = null;
}
}
/**
* Checks whether the password reset request has expired.
*
* @param int $ttl Requests older than this many seconds will be considered expired
*
* @return bool
*/
public function isPasswordRequestNonExpired($ttl)
{
return false;
}
/**
* Sets the last login time.
*
* @return static
*/
public function setLastLogin(\DateTime $time = null)
{
if ($time instanceof \DateTime) {
$this->connectedAt = DateTimeImmutable::createFromMutable($time);
} else {
$this->connectedAt = null;
}
}
/**
* Never use this to check if this user has access to anything!
*
* Use the AuthorizationChecker, or an implementation of AccessDecisionManager
* instead, e.g.
*
* $authorizationChecker->isGranted('ROLE_USER');
*
* @param string $role
*
* @return bool
*/
public function hasRole($role)
{
// TODO: Implement hasRole() method.
return true;
}
/**
* Sets the roles of the user.
*
* This overwrites any previous roles.
*
* @return static
*/
public function setRoles(array $roles)
{
// TODO: Implement setRoles() method.
return $this;
}
/**
* Adds a role to the user.
*
* @param string $role
*
* @return static
*/
public function addRole($role)
{
// TODO: Implement addRole() method.
return $this;
}
/**
* Removes a role to the user.
*
* @param string $role
*
* @return static
*/
public function removeRole($role)
{
// TODO: Implement removeRole() method.
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()
{
return null === $this->disabledAt;
}
/**
* Returns the roles granted to the user.
*
* @return (Role|string)[] The user roles
*/
public function getRoles()
{
return [
'ROLE_USER',
];
}
/**
* Returns the password used to authenticate the user.
*
* This should be the encoded password. On authentication, a plain-text
* password will be salted, encoded, and then compared to this value.
*
* @return string|null The encoded password if any
*/
public function getPassword():?string
{
return $this->password;
}
/**
* 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 null;
}
/**
* Returns the username used to authenticate the user.
*
* @return string The username
*/
public function getUsername()
{
return $this->getEmail();
}
/**
* 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()
{
$this->plainPassword = null;
}
public function serialize()
{
return serialize([
$this->id,
$this->email,
$this->password,
]);
}
public function unserialize($data)
{
list(
$this->id,
$this->email,
$this->password
) = unserialize($data);
}
/**
* @return string
*/
public function getPasswordResetToken()
{
return $this->passwordResetToken;
}
/**
* @param string $passwordResetToken
*
* @return self
*/
public function setPasswordResetToken($passwordResetToken)
{
$this->passwordResetToken = $passwordResetToken;
return $this;
}
/**
* @return DateTimeImmutable
*/
public function getConnectedAt()
{
return $this->connectedAt;
}
/**
* @return self
*/
public function setConnectedAt(DateTimeImmutable $connectedAt)
{
$this->connectedAt = $connectedAt;
return $this;
}
/**
* @return DateTimeImmutable
*/
public function getDisabledAt()
{
return $this->disabledAt;
}
/**
* @return self
*/
public function setDisabledAt(DateTimeImmutable $disabledAt)
{
$this->disabledAt = $disabledAt;
return $this;
}
/**
* Needed for Oauth2
*/
public function getIdentifier()
{
return $this->getEmail();
}
/**
* @return bool
*/
public function isBridgeServiceUser()
{
return $this->bridgeServiceUser;
}
/**
* @param bool $bridgeServiceUser
*
* @return self
*/
public function setBridgeServiceUser($bridgeServiceUser)
{
$this->bridgeServiceUser = $bridgeServiceUser;
return $this;
}
}