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.      * @return string
  201.      */
  202.     public function getPassword(): ?string
  203.     {
  204.         return $this->password;
  205.     }
  206.     /**
  207.      * Set locale.
  208.      *
  209.      * @param string $locale
  210.      *
  211.      * @return self
  212.      */
  213.     public function setLocale($locale)
  214.     {
  215.         $this->locale $locale;
  216.         return $this;
  217.     }
  218.     /**
  219.      * Get locale.
  220.      *
  221.      * @return string
  222.      */
  223.     public function getLocale()
  224.     {
  225.         return $this->locale;
  226.     }
  227.     /**
  228.      * Set salt.
  229.      *
  230.      * @param string $salt
  231.      *
  232.      * @return self
  233.      */
  234.     public function setSalt($salt)
  235.     {
  236.         $this->salt $salt;
  237.         return $this;
  238.     }
  239.     /**
  240.      * Get salt.
  241.      *
  242.      * @deprecated The salt functionality was deprecated in Sulu 2.5 and will be removed in Sulu 3.0
  243.      *             Modern password algorithm do not longer require a salt.
  244.      *
  245.      * @return string
  246.      */
  247.     public function getSalt()
  248.     {
  249.         return $this->salt;
  250.     }
  251.     /**
  252.      * Set privateKey.
  253.      *
  254.      * @param string|null $privateKey
  255.      *
  256.      * @return self
  257.      */
  258.     public function setPrivateKey($privateKey)
  259.     {
  260.         $this->privateKey $privateKey;
  261.         return $this;
  262.     }
  263.     /**
  264.      * Get privateKey.
  265.      *
  266.      * @return string|null
  267.      */
  268.     public function getPrivateKey()
  269.     {
  270.         return $this->privateKey;
  271.     }
  272.     /**
  273.      * Removes the password of the user.
  274.      */
  275.     public function eraseCredentials()
  276.     {
  277.     }
  278.     /**
  279.      * Set apiKey.
  280.      *
  281.      * @param string|null $apiKey
  282.      *
  283.      * @return self
  284.      */
  285.     public function setApiKey($apiKey)
  286.     {
  287.         $this->apiKey $apiKey;
  288.         return $this;
  289.     }
  290.     /**
  291.      * Get apiKey.
  292.      *
  293.      * @return string|null
  294.      */
  295.     public function getApiKey()
  296.     {
  297.         return $this->apiKey;
  298.     }
  299.     /**
  300.      * Set locked.
  301.      *
  302.      * @param bool $locked
  303.      *
  304.      * @return self
  305.      */
  306.     public function setLocked($locked)
  307.     {
  308.         $this->locked $locked;
  309.         return $this;
  310.     }
  311.     public function getLocked()
  312.     {
  313.         return $this->locked;
  314.     }
  315.     /**
  316.      * Set enabled.
  317.      *
  318.      * @param bool $enabled
  319.      *
  320.      * @return self
  321.      */
  322.     public function setEnabled($enabled)
  323.     {
  324.         $this->enabled $enabled;
  325.         return $this;
  326.     }
  327.     public function getEnabled()
  328.     {
  329.         return $this->enabled;
  330.     }
  331.     /**
  332.      * Set lastLogin.
  333.      *
  334.      * @param \DateTime|null $lastLogin
  335.      *
  336.      * @return self
  337.      */
  338.     public function setLastLogin($lastLogin)
  339.     {
  340.         $this->lastLogin $lastLogin;
  341.         return $this;
  342.     }
  343.     /**
  344.      * Get lastLogin.
  345.      *
  346.      * @return \DateTime|null
  347.      */
  348.     public function getLastLogin()
  349.     {
  350.         return $this->lastLogin;
  351.     }
  352.     /**
  353.      * Set confirmationKey.
  354.      *
  355.      * @param string|null $confirmationKey
  356.      *
  357.      * @return self
  358.      */
  359.     public function setConfirmationKey($confirmationKey)
  360.     {
  361.         $this->confirmationKey $confirmationKey;
  362.         return $this;
  363.     }
  364.     /**
  365.      * Get confirmationKey.
  366.      *
  367.      * @return string|null
  368.      */
  369.     public function getConfirmationKey()
  370.     {
  371.         return $this->confirmationKey;
  372.     }
  373.     /**
  374.      * Set passwordResetToken.
  375.      *
  376.      * @param string|null $passwordResetToken
  377.      *
  378.      * @return self
  379.      */
  380.     public function setPasswordResetToken($passwordResetToken)
  381.     {
  382.         $this->passwordResetToken $passwordResetToken;
  383.         return $this;
  384.     }
  385.     /**
  386.      * Get passwordResetToken.
  387.      *
  388.      * @return string|null
  389.      */
  390.     public function getPasswordResetToken()
  391.     {
  392.         return $this->passwordResetToken;
  393.     }
  394.     /**
  395.      * Set email.
  396.      *
  397.      * @param string|null $email
  398.      *
  399.      * @return self
  400.      */
  401.     public function setEmail($email)
  402.     {
  403.         $this->email $email;
  404.         return $this;
  405.     }
  406.     /**
  407.      * Get email.
  408.      *
  409.      * @return string|null
  410.      */
  411.     public function getEmail()
  412.     {
  413.         return $this->email;
  414.     }
  415.     /**
  416.      * Set tokenExpiresAt.
  417.      *
  418.      * @param \DateTime|null $passwordResetTokenExpiresAt
  419.      *
  420.      * @return self
  421.      */
  422.     public function setPasswordResetTokenExpiresAt($passwordResetTokenExpiresAt)
  423.     {
  424.         $this->passwordResetTokenExpiresAt $passwordResetTokenExpiresAt;
  425.         return $this;
  426.     }
  427.     /**
  428.      * Get passwordResetTokenExpiresAt.
  429.      *
  430.      * @return \DateTime|null
  431.      */
  432.     public function getPasswordResetTokenExpiresAt()
  433.     {
  434.         return $this->passwordResetTokenExpiresAt;
  435.     }
  436.     /**
  437.      * Set passwordResetTokenEmailsSent.
  438.      *
  439.      * @param int|null $passwordResetTokenEmailsSent
  440.      *
  441.      * @return self
  442.      */
  443.     public function setPasswordResetTokenEmailsSent($passwordResetTokenEmailsSent)
  444.     {
  445.         $this->passwordResetTokenEmailsSent $passwordResetTokenEmailsSent;
  446.         return $this;
  447.     }
  448.     /**
  449.      * Get passwordResetTokenEmailsSent.
  450.      *
  451.      * @return int|null
  452.      */
  453.     public function getPasswordResetTokenEmailsSent()
  454.     {
  455.         return $this->passwordResetTokenEmailsSent;
  456.     }
  457.     public function isEqualTo(SymfonyUserInterface $user): bool
  458.     {
  459.         if (!$user instanceof self) {
  460.             return false;
  461.         }
  462.         return $this->id === $user->getId()
  463.             && $this->password === $user->getPassword()
  464.             && $this->salt === $user->getSalt()
  465.             && $this->username === $user->getUsername()
  466.             && $this->locked === $user->getLocked()
  467.             && $this->enabled === $user->getEnabled();
  468.     }
  469.     /**
  470.      * Add userRoles.
  471.      *
  472.      * @return self
  473.      */
  474.     public function addUserRole(UserRole $userRoles)
  475.     {
  476.         $this->userRoles[] = $userRoles;
  477.         return $this;
  478.     }
  479.     /**
  480.      * Remove userRoles.
  481.      */
  482.     public function removeUserRole(UserRole $userRoles)
  483.     {
  484.         $this->userRoles->removeElement($userRoles);
  485.     }
  486.     /**
  487.      * Get userRoles.
  488.      *
  489.      * @return ArrayCollection
  490.      */
  491.     public function getUserRoles()
  492.     {
  493.         return $this->userRoles;
  494.     }
  495.     /**
  496.      * @VirtualProperty
  497.      * @Groups({"frontend"})
  498.      */
  499.     public function getRoles(): array
  500.     {
  501.         $roles = ['ROLE_USER'];
  502.         foreach ($this->getUserRoles() as $userRole) {
  503.             /* @var UserRole $userRole */
  504.             $roles[] = $userRole->getRole()->getIdentifier();
  505.         }
  506.         return $roles;
  507.     }
  508.     public function getRoleObjects()
  509.     {
  510.         $roles = [];
  511.         foreach ($this->getUserRoles() as $userRole) {
  512.             $roles[] = $userRole->getRole();
  513.         }
  514.         return $roles;
  515.     }
  516.     /**
  517.      * Add userGroups.
  518.      *
  519.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  520.      *
  521.      * @return self
  522.      */
  523.     public function addUserGroup(UserGroup $userGroups)
  524.     {
  525.         $this->userGroups[] = $userGroups;
  526.         return $this;
  527.     }
  528.     /**
  529.      * Remove userGroups.
  530.      *
  531.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  532.      */
  533.     public function removeUserGroup(UserGroup $userGroups)
  534.     {
  535.         $this->userGroups->removeElement($userGroups);
  536.     }
  537.     /**
  538.      * Get userGroups.
  539.      *
  540.      * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  541.      *
  542.      * @return ArrayCollection
  543.      */
  544.     public function getUserGroups()
  545.     {
  546.         return $this->userGroups;
  547.     }
  548.     /**
  549.      * Add userSettings.
  550.      *
  551.      * @return self
  552.      */
  553.     public function addUserSetting(UserSetting $userSettings)
  554.     {
  555.         $this->userSettings[] = $userSettings;
  556.         return $this;
  557.     }
  558.     /**
  559.      * Remove userSettings.
  560.      */
  561.     public function removeUserSetting(UserSetting $userSettings)
  562.     {
  563.         $this->userSettings->removeElement($userSettings);
  564.     }
  565.     /**
  566.      * Get userSettings.
  567.      *
  568.      * @return Collection|UserSetting[]
  569.      */
  570.     public function getUserSettings()
  571.     {
  572.         return $this->userSettings;
  573.     }
  574.     /**
  575.      * @VirtualProperty
  576.      * @Groups({"frontend"})
  577.      */
  578.     public function getSettings()
  579.     {
  580.         $userSettingValues = [];
  581.         foreach ($this->userSettings as $userSetting) {
  582.             $userSettingValues[$userSetting->getKey()] = \json_decode($userSetting->getValue(), true);
  583.         }
  584.         return $userSettingValues;
  585.     }
  586.     /**
  587.      * Set contact.
  588.      *
  589.      * @param ContactInterface $contact
  590.      *
  591.      * @return self
  592.      */
  593.     public function setContact(ContactInterface $contact null)
  594.     {
  595.         $this->contact $contact;
  596.         return $this;
  597.     }
  598.     /**
  599.      * Get contact.
  600.      *
  601.      * @return ContactInterface
  602.      */
  603.     public function getContact()
  604.     {
  605.         return $this->contact;
  606.     }
  607.     /**
  608.      * @VirtualProperty
  609.      * @SerializedName("fullName")
  610.      * @Groups({"frontend", "fullUser"})
  611.      *
  612.      * @return string
  613.      */
  614.     public function getFullName()
  615.     {
  616.         return null !== $this->getContact() ?
  617.             $this->getContact()->getFullName() : $this->getUsername();
  618.     }
  619.     /**
  620.      * @VirtualProperty
  621.      * @Groups({"profile"})
  622.      *
  623.      * @return string
  624.      */
  625.     public function getFirstName()
  626.     {
  627.         return $this->contact->getFirstName();
  628.     }
  629.     /**
  630.      * Set firstName.
  631.      *
  632.      * @return $this
  633.      */
  634.     public function setFirstName($firstName)
  635.     {
  636.         $this->contact->setFirstName($firstName);
  637.         return $this;
  638.     }
  639.     /**
  640.      * @VirtualProperty
  641.      * @Groups({"profile"})
  642.      *
  643.      * @return string
  644.      */
  645.     public function getLastName()
  646.     {
  647.         return $this->contact->getLastName();
  648.     }
  649.     /**
  650.      * Set lastName.
  651.      *
  652.      * @return $this
  653.      */
  654.     public function setLastName($lastName)
  655.     {
  656.         $this->contact->setLastName($lastName);
  657.         return $this;
  658.     }
  659. }