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