vendor/sulu/sulu/src/Sulu/Bundle/SecurityBundle/Entity/User.php line 35

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\SecurityBundle\Entity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use JMS\Serializer\Annotation\ExclusionPolicy;
  14. use JMS\Serializer\Annotation\Expose;
  15. use JMS\Serializer\Annotation\Groups;
  16. use JMS\Serializer\Annotation\SerializedName;
  17. use JMS\Serializer\Annotation\VirtualProperty;
  18. use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
  19. use Sulu\Bundle\CoreBundle\Entity\ApiEntity;
  20. use Sulu\Bundle\SecurityBundle\Entity\TwoFactor\TwoFactorTrait;
  21. use Sulu\Component\Persistence\Model\AuditableInterface;
  22. use Sulu\Component\Persistence\Model\AuditableTrait;
  23. use Sulu\Component\Security\Authentication\UserInterface;
  24. use Symfony\Component\Security\Core\User\EquatableInterface;
  25. use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
  26. /**
  27.  * User.
  28.  *
  29.  * @ExclusionPolicy("all")
  30.  */
  31. class User extends ApiEntity implements UserInterfaceEquatableInterfaceAuditableInterfacePasswordAuthenticatedUserInterface
  32. {
  33.     use AuditableTrait;
  34.     use TwoFactorTrait;
  35.     /**
  36.      * @var int
  37.      *
  38.      * @Expose
  39.      * @Groups({"frontend", "fullUser"})
  40.      */
  41.     protected $id;
  42.     /**
  43.      * @var string
  44.      *
  45.      * @Expose
  46.      * @Groups({"fullUser", "profile"})
  47.      */
  48.     protected $username;
  49.     /**
  50.      * @var string|null
  51.      *
  52.      * @Expose
  53.      * @Groups({"fullUser", "profile"})
  54.      */
  55.     protected $email;
  56.     /**
  57.      * @var string
  58.      */
  59.     protected $password;
  60.     /**
  61.      * @var string
  62.      *
  63.      * @Expose
  64.      * @Groups({"frontend", "fullUser", "profile"})
  65.      */
  66.     protected $locale;
  67.     /**
  68.      * @var string
  69.      */
  70.     protected $salt;
  71.     /**
  72.      * @var string|null
  73.      *
  74.      * @Expose
  75.      */
  76.     protected $privateKey;
  77.     /**
  78.      * @var string|null
  79.      */
  80.     protected $apiKey;
  81.     /**
  82.      * @var bool
  83.      *
  84.      * @Expose
  85.      */
  86.     protected $locked false;
  87.     /**
  88.      * @var bool
  89.      *
  90.      * @Expose
  91.      */
  92.     protected $enabled true;
  93.     /**
  94.      * @var \DateTime|null
  95.      */
  96.     protected $lastLogin;
  97.     /**
  98.      * @var string|null
  99.      */
  100.     protected $confirmationKey;
  101.     /**
  102.      * @var string|null
  103.      */
  104.     protected $passwordResetToken;
  105.     /**
  106.      * @var \DateTime|null
  107.      */
  108.     private $passwordResetTokenExpiresAt;
  109.     /**
  110.      * @var int|null
  111.      */
  112.     private $passwordResetTokenEmailsSent;
  113.     /**
  114.      * @var ContactInterface
  115.      *
  116.      * @Expose
  117.      * @Groups({"frontend", "fullUser"})
  118.      */
  119.     protected $contact;
  120.     /**
  121.      * @var Collection|UserRole[]
  122.      *
  123.      * @Expose
  124.      */
  125.     protected $userRoles;
  126.     /**
  127.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  128.      *
  129.      * @var Collection|UserGroup[]
  130.      *
  131.      * @Expose
  132.      */
  133.     protected $userGroups;
  134.     /**
  135.      * @var Collection|UserSetting[]
  136.      */
  137.     protected $userSettings;
  138.     /**
  139.      * Constructor.
  140.      */
  141.     public function __construct()
  142.     {
  143.         $this->apiKey \md5(\uniqid());
  144.         $this->userRoles = new ArrayCollection();
  145.         $this->userGroups = new ArrayCollection();
  146.         $this->userSettings = new ArrayCollection();
  147.     }
  148.     /**
  149.      * Get id.
  150.      *
  151.      * @return int
  152.      */
  153.     public function getId()
  154.     {
  155.         return $this->id;
  156.     }
  157.     /**
  158.      * Set username.
  159.      *
  160.      * @param string $username
  161.      *
  162.      * @return self
  163.      */
  164.     public function setUsername($username)
  165.     {
  166.         $this->username $username;
  167.         return $this;
  168.     }
  169.     /**
  170.      * Get username.
  171.      *
  172.      * @SerializedName("username")
  173.      * @Groups({"frontend", "fullUser"})
  174.      *
  175.      * @return string
  176.      */
  177.     public function getUsername()
  178.     {
  179.         return $this->username;
  180.     }
  181.     public function getUserIdentifier(): string
  182.     {
  183.         return $this->username;
  184.     }
  185.     /**
  186.      * Set password.
  187.      *
  188.      * @param string $password
  189.      *
  190.      * @return self
  191.      */
  192.     public function setPassword($password)
  193.     {
  194.         $this->password $password;
  195.         return $this;
  196.     }
  197.     /**
  198.      * Get password.
  199.      */
  200.     public function getPassword(): ?string
  201.     {
  202.         return $this->password;
  203.     }
  204.     /**
  205.      * Set locale.
  206.      *
  207.      * @param string $locale
  208.      *
  209.      * @return self
  210.      */
  211.     public function setLocale($locale)
  212.     {
  213.         $this->locale $locale;
  214.         return $this;
  215.     }
  216.     /**
  217.      * Get locale.
  218.      *
  219.      * @return string
  220.      */
  221.     public function getLocale()
  222.     {
  223.         return $this->locale;
  224.     }
  225.     /**
  226.      * Set salt.
  227.      *
  228.      * @param string $salt
  229.      *
  230.      * @return self
  231.      */
  232.     public function setSalt($salt)
  233.     {
  234.         $this->salt $salt;
  235.         return $this;
  236.     }
  237.     /**
  238.      * Get salt.
  239.      *
  240.      * @deprecated The salt functionality was deprecated in Sulu 2.5 and will be removed in Sulu 3.0
  241.      *             Modern password algorithm do not longer require a salt.
  242.      *
  243.      * @return string
  244.      */
  245.     public function getSalt()
  246.     {
  247.         return $this->salt;
  248.     }
  249.     /**
  250.      * Set privateKey.
  251.      *
  252.      * @param string|null $privateKey
  253.      *
  254.      * @return self
  255.      */
  256.     public function setPrivateKey($privateKey)
  257.     {
  258.         $this->privateKey $privateKey;
  259.         return $this;
  260.     }
  261.     /**
  262.      * Get privateKey.
  263.      *
  264.      * @return string|null
  265.      */
  266.     public function getPrivateKey()
  267.     {
  268.         return $this->privateKey;
  269.     }
  270.     /**
  271.      * Removes the password of the user.
  272.      */
  273.     public function eraseCredentials()
  274.     {
  275.     }
  276.     /**
  277.      * Set apiKey.
  278.      *
  279.      * @param string|null $apiKey
  280.      *
  281.      * @return self
  282.      */
  283.     public function setApiKey($apiKey)
  284.     {
  285.         $this->apiKey $apiKey;
  286.         return $this;
  287.     }
  288.     /**
  289.      * Get apiKey.
  290.      *
  291.      * @return string|null
  292.      */
  293.     public function getApiKey()
  294.     {
  295.         return $this->apiKey;
  296.     }
  297.     /**
  298.      * Set locked.
  299.      *
  300.      * @param bool $locked
  301.      *
  302.      * @return self
  303.      */
  304.     public function setLocked($locked)
  305.     {
  306.         $this->locked $locked;
  307.         return $this;
  308.     }
  309.     public function getLocked()
  310.     {
  311.         return $this->locked;
  312.     }
  313.     /**
  314.      * Set enabled.
  315.      *
  316.      * @param bool $enabled
  317.      *
  318.      * @return self
  319.      */
  320.     public function setEnabled($enabled)
  321.     {
  322.         $this->enabled $enabled;
  323.         return $this;
  324.     }
  325.     public function getEnabled()
  326.     {
  327.         return $this->enabled;
  328.     }
  329.     /**
  330.      * Set lastLogin.
  331.      *
  332.      * @param \DateTime|null $lastLogin
  333.      *
  334.      * @return self
  335.      */
  336.     public function setLastLogin($lastLogin)
  337.     {
  338.         $this->lastLogin $lastLogin;
  339.         return $this;
  340.     }
  341.     /**
  342.      * Get lastLogin.
  343.      *
  344.      * @return \DateTime|null
  345.      */
  346.     public function getLastLogin()
  347.     {
  348.         return $this->lastLogin;
  349.     }
  350.     /**
  351.      * Set confirmationKey.
  352.      *
  353.      * @param string|null $confirmationKey
  354.      *
  355.      * @return self
  356.      */
  357.     public function setConfirmationKey($confirmationKey)
  358.     {
  359.         $this->confirmationKey $confirmationKey;
  360.         return $this;
  361.     }
  362.     /**
  363.      * Get confirmationKey.
  364.      *
  365.      * @return string|null
  366.      */
  367.     public function getConfirmationKey()
  368.     {
  369.         return $this->confirmationKey;
  370.     }
  371.     /**
  372.      * Set passwordResetToken.
  373.      *
  374.      * @param string|null $passwordResetToken
  375.      *
  376.      * @return self
  377.      */
  378.     public function setPasswordResetToken($passwordResetToken)
  379.     {
  380.         $this->passwordResetToken $passwordResetToken;
  381.         return $this;
  382.     }
  383.     /**
  384.      * Get passwordResetToken.
  385.      *
  386.      * @return string|null
  387.      */
  388.     public function getPasswordResetToken()
  389.     {
  390.         return $this->passwordResetToken;
  391.     }
  392.     /**
  393.      * Set email.
  394.      *
  395.      * @param string|null $email
  396.      *
  397.      * @return self
  398.      */
  399.     public function setEmail($email)
  400.     {
  401.         $this->email $email;
  402.         return $this;
  403.     }
  404.     /**
  405.      * Get email.
  406.      *
  407.      * @return string|null
  408.      */
  409.     public function getEmail()
  410.     {
  411.         return $this->email;
  412.     }
  413.     /**
  414.      * Set tokenExpiresAt.
  415.      *
  416.      * @param \DateTime|null $passwordResetTokenExpiresAt
  417.      *
  418.      * @return self
  419.      */
  420.     public function setPasswordResetTokenExpiresAt($passwordResetTokenExpiresAt)
  421.     {
  422.         $this->passwordResetTokenExpiresAt $passwordResetTokenExpiresAt;
  423.         return $this;
  424.     }
  425.     /**
  426.      * Get passwordResetTokenExpiresAt.
  427.      *
  428.      * @return \DateTime|null
  429.      */
  430.     public function getPasswordResetTokenExpiresAt()
  431.     {
  432.         return $this->passwordResetTokenExpiresAt;
  433.     }
  434.     /**
  435.      * Set passwordResetTokenEmailsSent.
  436.      *
  437.      * @param int|null $passwordResetTokenEmailsSent
  438.      *
  439.      * @return self
  440.      */
  441.     public function setPasswordResetTokenEmailsSent($passwordResetTokenEmailsSent)
  442.     {
  443.         $this->passwordResetTokenEmailsSent $passwordResetTokenEmailsSent;
  444.         return $this;
  445.     }
  446.     /**
  447.      * Get passwordResetTokenEmailsSent.
  448.      *
  449.      * @return int|null
  450.      */
  451.     public function getPasswordResetTokenEmailsSent()
  452.     {
  453.         return $this->passwordResetTokenEmailsSent;
  454.     }
  455.     public function isEqualTo(SymfonyUserInterface $user): bool
  456.     {
  457.         if (!$user instanceof self) {
  458.             return false;
  459.         }
  460.         return $this->id === $user->getId()
  461.             && $this->password === $user->getPassword()
  462.             && $this->salt === $user->getSalt()
  463.             && $this->username === $user->getUsername()
  464.             && $this->locked === $user->getLocked()
  465.             && $this->enabled === $user->getEnabled();
  466.     }
  467.     /**
  468.      * Add userRoles.
  469.      *
  470.      * @return self
  471.      */
  472.     public function addUserRole(UserRole $userRoles)
  473.     {
  474.         $this->userRoles[] = $userRoles;
  475.         return $this;
  476.     }
  477.     /**
  478.      * Remove userRoles.
  479.      */
  480.     public function removeUserRole(UserRole $userRoles)
  481.     {
  482.         $this->userRoles->removeElement($userRoles);
  483.     }
  484.     /**
  485.      * Get userRoles.
  486.      *
  487.      * @return ArrayCollection
  488.      */
  489.     public function getUserRoles()
  490.     {
  491.         return $this->userRoles;
  492.     }
  493.     /**
  494.      * @VirtualProperty
  495.      * @Groups({"frontend"})
  496.      */
  497.     public function getRoles(): array
  498.     {
  499.         $roles = ['ROLE_USER'];
  500.         foreach ($this->getUserRoles() as $userRole) {
  501.             /* @var UserRole $userRole */
  502.             $roles[] = $userRole->getRole()->getIdentifier();
  503.         }
  504.         return $roles;
  505.     }
  506.     public function getRoleObjects()
  507.     {
  508.         $roles = [];
  509.         foreach ($this->getUserRoles() as $userRole) {
  510.             $roles[] = $userRole->getRole();
  511.         }
  512.         return $roles;
  513.     }
  514.     /**
  515.      * Add userGroups.
  516.      *
  517.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  518.      *
  519.      * @return self
  520.      */
  521.     public function addUserGroup(UserGroup $userGroups)
  522.     {
  523.         $this->userGroups[] = $userGroups;
  524.         return $this;
  525.     }
  526.     /**
  527.      * Remove userGroups.
  528.      *
  529.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  530.      */
  531.     public function removeUserGroup(UserGroup $userGroups)
  532.     {
  533.         $this->userGroups->removeElement($userGroups);
  534.     }
  535.     /**
  536.      * Get userGroups.
  537.      *
  538.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  539.      *
  540.      * @return ArrayCollection
  541.      */
  542.     public function getUserGroups()
  543.     {
  544.         return $this->userGroups;
  545.     }
  546.     /**
  547.      * Add userSettings.
  548.      *
  549.      * @return self
  550.      */
  551.     public function addUserSetting(UserSetting $userSettings)
  552.     {
  553.         $this->userSettings[] = $userSettings;
  554.         return $this;
  555.     }
  556.     /**
  557.      * Remove userSettings.
  558.      */
  559.     public function removeUserSetting(UserSetting $userSettings)
  560.     {
  561.         $this->userSettings->removeElement($userSettings);
  562.     }
  563.     /**
  564.      * Get userSettings.
  565.      *
  566.      * @return Collection|UserSetting[]
  567.      */
  568.     public function getUserSettings()
  569.     {
  570.         return $this->userSettings;
  571.     }
  572.     /**
  573.      * @VirtualProperty
  574.      * @Groups({"frontend"})
  575.      */
  576.     public function getSettings()
  577.     {
  578.         $userSettingValues = [];
  579.         foreach ($this->userSettings as $userSetting) {
  580.             $userSettingValues[$userSetting->getKey()] = \json_decode($userSetting->getValue(), true);
  581.         }
  582.         return $userSettingValues;
  583.     }
  584.     /**
  585.      * Set contact.
  586.      *
  587.      * @return self
  588.      */
  589.     public function setContact(?ContactInterface $contact null)
  590.     {
  591.         $this->contact $contact;
  592.         return $this;
  593.     }
  594.     /**
  595.      * Get contact.
  596.      *
  597.      * @return ContactInterface
  598.      */
  599.     public function getContact()
  600.     {
  601.         return $this->contact;
  602.     }
  603.     /**
  604.      * @VirtualProperty
  605.      * @SerializedName("fullName")
  606.      * @Groups({"frontend", "fullUser"})
  607.      *
  608.      * @return string
  609.      */
  610.     public function getFullName()
  611.     {
  612.         return null !== $this->getContact() ?
  613.             $this->getContact()->getFullName() : $this->getUsername();
  614.     }
  615.     /**
  616.      * @VirtualProperty
  617.      * @Groups({"profile"})
  618.      *
  619.      * @return string
  620.      */
  621.     public function getFirstName()
  622.     {
  623.         return $this->contact->getFirstName();
  624.     }
  625.     /**
  626.      * Set firstName.
  627.      *
  628.      * @return $this
  629.      */
  630.     public function setFirstName($firstName)
  631.     {
  632.         $this->contact->setFirstName($firstName);
  633.         return $this;
  634.     }
  635.     /**
  636.      * @VirtualProperty
  637.      * @Groups({"profile"})
  638.      *
  639.      * @return string
  640.      */
  641.     public function getLastName()
  642.     {
  643.         return $this->contact->getLastName();
  644.     }
  645.     /**
  646.      * Set lastName.
  647.      *
  648.      * @return $this
  649.      */
  650.     public function setLastName($lastName)
  651.     {
  652.         $this->contact->setLastName($lastName);
  653.         return $this;
  654.     }
  655. }