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

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