src/Entity/Commons/OAuth2/User.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Commons\OAuth2;
  3. use App\Entity\CommonEntityInterface;
  4. use App\Metadata\Annotation as Cdp;
  5. use App\Validator\Constraints as AppAssert;
  6. use DateTimeImmutable;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use FOS\UserBundle\Model\UserInterface;
  9. use Gedmo\Timestampable\Traits\TimestampableEntity;
  10. use League\OAuth2\Server\Entities\UserEntityInterface;
  11. use Serializable;
  12. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15.  * @ORM\Table("oauth2__user")
  16.  * @ORM\Entity
  17.  *
  18.  * @Cdp\Schema(
  19.  *      singular = "Utilisateur",
  20.  *      plural = "Utilisateurs",
  21.  *      frozenColumns = {"id", "email", "connectedAt"},
  22.  *      linkColumns = {"id", "email", "connectedAt"},
  23.  *      defaultOrder = {"email", "ASC"},
  24.  *      defaultColumns = {
  25.  *          "id",
  26.  *          "email",
  27.  *          "connectedAt",
  28.  *      },
  29.  *      searchFields = {"email"},
  30.  *      tabs={
  31.  *          @Cdp\FormView\Tab("infos_generales", label="Informations générales", fieldsets={
  32.  *              @Cdp\FormView\Fieldset("infos_generales_1"),
  33.  *          }),
  34.  *      }
  35.  * )
  36.  *
  37.  * @UniqueEntity(
  38.  *     fields={"email"},
  39.  *     errorPath="email",
  40.  *     message="Un compte existe déjà avec cet email"
  41.  * )
  42.  */
  43. class User implements UserInterfaceUserEntityInterfaceCommonEntityInterfaceSerializable
  44. {
  45.     use TimestampableEntity;
  46.     /**
  47.      * @var int
  48.      *
  49.      * @Cdp\Field(type="integer")
  50.      *
  51.      * @ORM\Id
  52.      * @ORM\Column(type="integer")
  53.      * @ORM\GeneratedValue(strategy="AUTO")
  54.      */
  55.     protected $id;
  56.     /**
  57.      * @var string
  58.      *
  59.      * @Cdp\Field(
  60.      *     type="email",
  61.      *     label="Courriel",
  62.      *     formTab="infos_generales",
  63.      *     formFieldset="infos_generales_1",
  64.      *     formFieldsetPosition=1,
  65.      * )
  66.      * @Assert\Email
  67.      *
  68.      * @ORM\Column(type="string", length=255, unique=true)
  69.      */
  70.     protected $email;
  71.     /**
  72.      * @var string
  73.      *
  74.      * @ORM\Column(type="string", length=255, nullable=true)
  75.      */
  76.     protected $password;
  77.     /**
  78.      * @var string
  79.      *
  80.      * @Assert\NotBlank(groups={"reset_password"})
  81.      * @AppAssert\Password(groups={"reset_password"})
  82.      */
  83.     protected $plainPassword;
  84.     /**
  85.      * @var DateTimeImmutable
  86.      *
  87.      * @Cdp\Field(
  88.      *     type="date",
  89.      *     label="Date de dernière connexion",
  90.      *     required=true,
  91.      *     onForm=false,
  92.      *     formTab="infos_generales",
  93.      *     formFieldset="infos_generales_1",
  94.      *     formFieldsetPosition=2,
  95.      * )
  96.      * @Assert\Date
  97.      *
  98.      * @ORM\Column(type="datetime_immutable", nullable=true)
  99.      */
  100.     protected $connectedAt;
  101.     /**
  102.      * @var DateTimeImmutable
  103.      *
  104.      * @Cdp\Field(
  105.      *     type="date",
  106.      *     label="Date de désactivation",
  107.      *     required=true,
  108.      *     formTab="infos_generales",
  109.      *     formFieldset="infos_generales_1",
  110.      *     formFieldsetPosition=3,
  111.      * )
  112.      * @Assert\Date
  113.      *
  114.      * @ORM\Column(type="datetime_immutable", nullable=true)
  115.      */
  116.     protected $disabledAt;
  117.     /**
  118.      * @var DateTimeImmutable
  119.      *
  120.      * @ORM\Column(type="datetime_immutable", nullable=true)
  121.      */
  122.     protected $passwordRequestedAt;
  123.     /**
  124.      * @var string
  125.      *
  126.      * @ORM\Column(type="string", length=255, unique=true, nullable=true)
  127.      */
  128.     protected $passwordResetToken;
  129.     /**
  130.      * @var bool
  131.      *
  132.      * @ORM\Column(type="boolean", nullable=true, options={"default" : 0})
  133.      */
  134.     protected $bridgeServiceUser false;
  135.     public function __toString()
  136.     {
  137.         return (string) $this->email;
  138.     }
  139.     ////////////////////////////// UserInterface ////////////////////////////
  140.     /**
  141.      * Returns the user unique id.
  142.      *
  143.      * @return mixed
  144.      */
  145.     public function getId()
  146.     {
  147.         return $this->id;
  148.     }
  149.     /**
  150.      * Sets the username.
  151.      *
  152.      * @param string $username
  153.      *
  154.      * @return static
  155.      */
  156.     public function setUsername($username)
  157.     {
  158.         return $this->setEmail($username);
  159.     }
  160.     /**
  161.      * Gets the canonical username in search and sort queries.
  162.      *
  163.      * @return string
  164.      */
  165.     public function getUsernameCanonical()
  166.     {
  167.         return $this->getEmail();
  168.     }
  169.     /**
  170.      * Sets the canonical username.
  171.      *
  172.      * @param string $usernameCanonical
  173.      *
  174.      * @return static
  175.      */
  176.     public function setUsernameCanonical($usernameCanonical)
  177.     {
  178.         return $this->setEmail($usernameCanonical);
  179.     }
  180.     /**
  181.      * @param string|null $salt
  182.      *
  183.      * @return static
  184.      */
  185.     public function setSalt($salt)
  186.     {
  187.         return null;
  188.     }
  189.     /**
  190.      * Gets email.
  191.      *
  192.      * @return string
  193.      */
  194.     public function getEmail()
  195.     {
  196.         return $this->email;
  197.     }
  198.     /**
  199.      * Sets the email.
  200.      *
  201.      * @param string $email
  202.      *
  203.      * @return static
  204.      */
  205.     public function setEmail($email)
  206.     {
  207.         $this->email $email;
  208.         return $this;
  209.     }
  210.     /**
  211.      * Gets the canonical email in search and sort queries.
  212.      *
  213.      * @return string
  214.      */
  215.     public function getEmailCanonical()
  216.     {
  217.         return $this->getEmail();
  218.     }
  219.     /**
  220.      * Sets the canonical email.
  221.      *
  222.      * @param string $emailCanonical
  223.      *
  224.      * @return static
  225.      */
  226.     public function setEmailCanonical($emailCanonical)
  227.     {
  228.         return $this->setEmail($emailCanonical);
  229.     }
  230.     /**
  231.      * Gets the plain password.
  232.      *
  233.      * @return string
  234.      */
  235.     public function getPlainPassword()
  236.     {
  237.         return $this->plainPassword;
  238.     }
  239.     /**
  240.      * Sets the plain password.
  241.      *
  242.      * @param string $password
  243.      *
  244.      * @return static
  245.      */
  246.     public function setPlainPassword($password)
  247.     {
  248.         $this->plainPassword $password;
  249.         return $this;
  250.     }
  251.     /**
  252.      * Sets the hashed password.
  253.      *
  254.      * @param string $password
  255.      *
  256.      * @return static
  257.      */
  258.     public function setPassword($password)
  259.     {
  260.         $this->password $password;
  261.         return $this;
  262.     }
  263.     /**
  264.      * Tells if the the given user has the super admin role.
  265.      *
  266.      * @return bool
  267.      */
  268.     public function isSuperAdmin()
  269.     {
  270.         // TODO: Implement setSuperAdmin() method.
  271.         return true;
  272.     }
  273.     /**
  274.      * @param bool $boolean
  275.      *
  276.      * @return static
  277.      */
  278.     public function setEnabled($enable)
  279.     {
  280.         if (true === $enable) {
  281.             $this->disabledAt null;
  282.         }
  283.         if (false === $enable) {
  284.             $this->disabledAt = new DateTimeImmutable();
  285.         }
  286.         return $this;
  287.     }
  288.     /**
  289.      * Sets the super admin status.
  290.      *
  291.      * @param bool $boolean
  292.      *
  293.      * @return static
  294.      */
  295.     public function setSuperAdmin($boolean)
  296.     {
  297.         // TODO: Implement setSuperAdmin() method.
  298.         return $this;
  299.     }
  300.     /**
  301.      * Gets the confirmation token.
  302.      *
  303.      * @return string|null
  304.      */
  305.     public function getConfirmationToken()
  306.     {
  307.         return $this->passwordResetToken;
  308.     }
  309.     /**
  310.      * Sets the confirmation token.
  311.      *
  312.      * @param string|null $confirmationToken
  313.      *
  314.      * @return static
  315.      */
  316.     public function setConfirmationToken($confirmationToken)
  317.     {
  318.         $this->passwordResetToken $confirmationToken;
  319.         return $this;
  320.     }
  321.     /**
  322.      * Sets the timestamp that the user requested a password reset.
  323.      *
  324.      * @return static
  325.      */
  326.     public function setPasswordRequestedAt(\DateTime $date null)
  327.     {
  328.         if ($date instanceof \DateTime) {
  329.             $this->passwordRequestedAt DateTimeImmutable::createFromMutable($date);
  330.         } else {
  331.             $this->passwordRequestedAt null;
  332.         }
  333.     }
  334.     /**
  335.      * Checks whether the password reset request has expired.
  336.      *
  337.      * @param int $ttl Requests older than this many seconds will be considered expired
  338.      *
  339.      * @return bool
  340.      */
  341.     public function isPasswordRequestNonExpired($ttl)
  342.     {
  343.         return false;
  344.     }
  345.     /**
  346.      * Sets the last login time.
  347.      *
  348.      * @return static
  349.      */
  350.     public function setLastLogin(\DateTime $time null)
  351.     {
  352.         if ($time instanceof \DateTime) {
  353.             $this->connectedAt DateTimeImmutable::createFromMutable($time);
  354.         } else {
  355.             $this->connectedAt null;
  356.         }
  357.     }
  358.     /**
  359.      * Never use this to check if this user has access to anything!
  360.      *
  361.      * Use the AuthorizationChecker, or an implementation of AccessDecisionManager
  362.      * instead, e.g.
  363.      *
  364.      *         $authorizationChecker->isGranted('ROLE_USER');
  365.      *
  366.      * @param string $role
  367.      *
  368.      * @return bool
  369.      */
  370.     public function hasRole($role)
  371.     {
  372.         // TODO: Implement hasRole() method.
  373.         return true;
  374.     }
  375.     /**
  376.      * Sets the roles of the user.
  377.      *
  378.      * This overwrites any previous roles.
  379.      *
  380.      * @return static
  381.      */
  382.     public function setRoles(array $roles)
  383.     {
  384.         // TODO: Implement setRoles() method.
  385.         return $this;
  386.     }
  387.     /**
  388.      * Adds a role to the user.
  389.      *
  390.      * @param string $role
  391.      *
  392.      * @return static
  393.      */
  394.     public function addRole($role)
  395.     {
  396.         // TODO: Implement addRole() method.
  397.         return $this;
  398.     }
  399.     /**
  400.      * Removes a role to the user.
  401.      *
  402.      * @param string $role
  403.      *
  404.      * @return static
  405.      */
  406.     public function removeRole($role)
  407.     {
  408.         // TODO: Implement removeRole() method.
  409.         return $this;
  410.     }
  411.     /**
  412.      * Checks whether the user's account has expired.
  413.      *
  414.      * Internally, if this method returns false, the authentication system
  415.      * will throw an AccountExpiredException and prevent login.
  416.      *
  417.      * @return bool true if the user's account is non expired, false otherwise
  418.      *
  419.      * @see AccountExpiredException
  420.      */
  421.     public function isAccountNonExpired()
  422.     {
  423.         return true;
  424.     }
  425.     /**
  426.      * Checks whether the user is locked.
  427.      *
  428.      * Internally, if this method returns false, the authentication system
  429.      * will throw a LockedException and prevent login.
  430.      *
  431.      * @return bool true if the user is not locked, false otherwise
  432.      *
  433.      * @see LockedException
  434.      */
  435.     public function isAccountNonLocked()
  436.     {
  437.         return true;
  438.     }
  439.     /**
  440.      * Checks whether the user's credentials (password) has expired.
  441.      *
  442.      * Internally, if this method returns false, the authentication system
  443.      * will throw a CredentialsExpiredException and prevent login.
  444.      *
  445.      * @return bool true if the user's credentials are non expired, false otherwise
  446.      *
  447.      * @see CredentialsExpiredException
  448.      */
  449.     public function isCredentialsNonExpired()
  450.     {
  451.         return true;
  452.     }
  453.     /**
  454.      * Checks whether the user is enabled.
  455.      *
  456.      * Internally, if this method returns false, the authentication system
  457.      * will throw a DisabledException and prevent login.
  458.      *
  459.      * @return bool true if the user is enabled, false otherwise
  460.      *
  461.      * @see DisabledException
  462.      */
  463.     public function isEnabled()
  464.     {
  465.         return null === $this->disabledAt;
  466.     }
  467.     /**
  468.      * Returns the roles granted to the user.
  469.      *
  470.      * @return (Role|string)[] The user roles
  471.      */
  472.     public function getRoles()
  473.     {
  474.         return [
  475.             'ROLE_USER',
  476.         ];
  477.     }
  478.     /**
  479.      * Returns the password used to authenticate the user.
  480.      *
  481.      * This should be the encoded password. On authentication, a plain-text
  482.      * password will be salted, encoded, and then compared to this value.
  483.      *
  484.      * @return string|null The encoded password if any
  485.      */
  486.     public function getPassword():?string
  487.     {
  488.         return $this->password;
  489.     }
  490.     /**
  491.      * Returns the salt that was originally used to encode the password.
  492.      *
  493.      * This can return null if the password was not encoded using a salt.
  494.      *
  495.      * @return string|null The salt
  496.      */
  497.     public function getSalt()
  498.     {
  499.         return null;
  500.     }
  501.     /**
  502.      * Returns the username used to authenticate the user.
  503.      *
  504.      * @return string The username
  505.      */
  506.     public function getUsername()
  507.     {
  508.         return $this->getEmail();
  509.     }
  510.     /**
  511.      * Removes sensitive data from the user.
  512.      *
  513.      * This is important if, at any given point, sensitive information like
  514.      * the plain-text password is stored on this object.
  515.      */
  516.     public function eraseCredentials()
  517.     {
  518.         $this->plainPassword null;
  519.     }
  520.     public function serialize()
  521.     {
  522.         return serialize([
  523.             $this->id,
  524.             $this->email,
  525.             $this->password,
  526.         ]);
  527.     }
  528.     public function unserialize($data)
  529.     {
  530.         list(
  531.             $this->id,
  532.             $this->email,
  533.             $this->password
  534.         ) = unserialize($data);
  535.     }
  536.     /**
  537.      * @return string
  538.      */
  539.     public function getPasswordResetToken()
  540.     {
  541.         return $this->passwordResetToken;
  542.     }
  543.     /**
  544.      * @param string $passwordResetToken
  545.      *
  546.      * @return self
  547.      */
  548.     public function setPasswordResetToken($passwordResetToken)
  549.     {
  550.         $this->passwordResetToken $passwordResetToken;
  551.         return $this;
  552.     }
  553.     /**
  554.      * @return DateTimeImmutable
  555.      */
  556.     public function getConnectedAt()
  557.     {
  558.         return $this->connectedAt;
  559.     }
  560.     /**
  561.      * @return self
  562.      */
  563.     public function setConnectedAt(DateTimeImmutable $connectedAt)
  564.     {
  565.         $this->connectedAt $connectedAt;
  566.         return $this;
  567.     }
  568.     /**
  569.      * @return DateTimeImmutable
  570.      */
  571.     public function getDisabledAt()
  572.     {
  573.         return $this->disabledAt;
  574.     }
  575.     /**
  576.      * @return self
  577.      */
  578.     public function setDisabledAt(DateTimeImmutable $disabledAt)
  579.     {
  580.         $this->disabledAt $disabledAt;
  581.         return $this;
  582.     }
  583.     /**
  584.      * Needed for Oauth2
  585.      */
  586.     public function getIdentifier()
  587.     {
  588.         return $this->getEmail();
  589.     }
  590.     /**
  591.      * @return bool
  592.      */
  593.     public function isBridgeServiceUser()
  594.     {
  595.         return $this->bridgeServiceUser;
  596.     }
  597.     /**
  598.      * @param bool $bridgeServiceUser
  599.      *
  600.      * @return self
  601.      */
  602.     public function setBridgeServiceUser($bridgeServiceUser)
  603.     {
  604.         $this->bridgeServiceUser $bridgeServiceUser;
  605.         return $this;
  606.     }
  607. }