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

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