Guest User

Untitled

a guest
Nov 23rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * This file is part of the FOSUserBundle package.
  5. *
  6. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace FOS\UserBundle\Model;
  13.  
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16.  
  17. /**
  18. * Storage agnostic user object.
  19. *
  20. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. */
  23. abstract class User implements UserInterface, GroupableInterface
  24. {
  25. /**
  26. * @var mixed
  27. */
  28. protected $id;
  29.  
  30. /**
  31. * @var string
  32. */
  33. protected $username;
  34.  
  35. /**
  36. * @var string
  37. */
  38. protected $usernameCanonical;
  39.  
  40. /**
  41. * @var string
  42. */
  43. protected $email;
  44.  
  45. /**
  46. * @var string
  47. */
  48. protected $emailCanonical;
  49.  
  50. /**
  51. * @var bool
  52. */
  53. protected $enabled;
  54.  
  55. /**
  56. * The salt to use for hashing.
  57. *
  58. * @var string
  59. */
  60. protected $salt;
  61.  
  62. /**
  63. * Encrypted password. Must be persisted.
  64. *
  65. * @var string
  66. */
  67. protected $password;
  68.  
  69. /**
  70. * Plain password. Used for model validation. Must not be persisted.
  71. *
  72. * @var string
  73. */
  74. protected $plainPassword;
  75.  
  76. /**
  77. * @var \DateTime
  78. */
  79. protected $lastLogin;
  80.  
  81. /**
  82. * Random string sent to the user email address in order to verify it.
  83. *
  84. * @var string
  85. */
  86. protected $confirmationToken;
  87.  
  88. /**
  89. * @var \DateTime
  90. */
  91. protected $passwordRequestedAt;
  92.  
  93. /**
  94. * @var Collection
  95. */
  96. protected $groups;
  97.  
  98. /**
  99. * @var array
  100. */
  101. protected $roles;
  102.  
  103. /**
  104. * User constructor.
  105. */
  106. public function __construct()
  107. {
  108. $this->enabled = false;
  109. $this->roles = array();
  110. }
  111.  
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function addRole($role)
  116. {
  117. $role = strtoupper($role);
  118. if ($role === static::ROLE_DEFAULT) {
  119. return $this;
  120. }
  121.  
  122. if (!in_array($role, $this->roles, true)) {
  123. $this->roles[] = $role;
  124. }
  125.  
  126. return $this;
  127. }
  128.  
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function serialize()
  133. {
  134. return serialize(array(
  135. $this->password,
  136. $this->salt,
  137. $this->usernameCanonical,
  138. $this->username,
  139. $this->enabled,
  140. $this->id,
  141. $this->email,
  142. $this->emailCanonical,
  143. ));
  144. }
  145.  
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function unserialize($serialized)
  150. {
  151. $data = unserialize($serialized);
  152.  
  153. if (13 === count($data)) {
  154. // Unserializing a User object from 1.3.x
  155. unset($data[4], $data[5], $data[6], $data[9], $data[10]);
  156. $data = array_values($data);
  157. } elseif (11 === count($data)) {
  158. // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
  159. unset($data[4], $data[7], $data[8]);
  160. $data = array_values($data);
  161. }
  162.  
  163. list(
  164. $this->password,
  165. $this->salt,
  166. $this->usernameCanonical,
  167. $this->username,
  168. $this->enabled,
  169. $this->id,
  170. $this->email,
  171. $this->emailCanonical
  172. ) = $data;
  173. }
  174.  
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function eraseCredentials()
  179. {
  180. $this->plainPassword = null;
  181. }
  182.  
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function getId()
  187. {
  188. return $this->id;
  189. }
  190.  
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function getUsername()
  195. {
  196. return $this->username;
  197. }
  198.  
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getUsernameCanonical()
  203. {
  204. return $this->usernameCanonical;
  205. }
  206.  
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function getSalt()
  211. {
  212. return $this->salt;
  213. }
  214.  
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function getEmail()
  219. {
  220. return $this->email;
  221. }
  222.  
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getEmailCanonical()
  227. {
  228. return $this->emailCanonical;
  229. }
  230.  
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function getPassword()
  235. {
  236. return $this->password;
  237. }
  238.  
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function getPlainPassword()
  243. {
  244. return $this->plainPassword;
  245. }
  246.  
  247. /**
  248. * Gets the last login time.
  249. *
  250. * @return \DateTime
  251. */
  252. public function getLastLogin()
  253. {
  254. return $this->lastLogin;
  255. }
  256.  
  257. /**
  258. * {@inheritdoc}
  259. */
  260. public function getConfirmationToken()
  261. {
  262. return $this->confirmationToken;
  263. }
  264.  
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function getRoles()
  269. {
  270. $roles = $this->roles;
  271.  
  272. foreach ($this->getGroups() as $group) {
  273. $roles = array_merge($roles, $group->getRoles());
  274. }
  275.  
  276. // we need to make sure to have at least one role
  277. $roles[] = static::ROLE_DEFAULT;
  278.  
  279. return array_unique($roles);
  280. }
  281.  
  282. /**
  283. * {@inheritdoc}
  284. */
  285. public function hasRole($role)
  286. {
  287. return in_array(strtoupper($role), $this->getRoles(), true);
  288. }
  289.  
  290. /**
  291. * {@inheritdoc}
  292. */
  293. public function isAccountNonExpired()
  294. {
  295. return true;
  296. }
  297.  
  298. /**
  299. * {@inheritdoc}
  300. */
  301. public function isAccountNonLocked()
  302. {
  303. return true;
  304. }
  305.  
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function isCredentialsNonExpired()
  310. {
  311. return true;
  312. }
  313.  
  314. public function isEnabled()
  315. {
  316. return $this->enabled;
  317. }
  318.  
  319. /**
  320. * {@inheritdoc}
  321. */
  322. public function isSuperAdmin()
  323. {
  324. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  325. }
  326.  
  327. /**
  328. * {@inheritdoc}
  329. */
  330. public function removeRole($role)
  331. {
  332. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  333. unset($this->roles[$key]);
  334. $this->roles = array_values($this->roles);
  335. }
  336.  
  337. return $this;
  338. }
  339.  
  340. /**
  341. * {@inheritdoc}
  342. */
  343. public function setUsername($username)
  344. {
  345. $this->username = $username;
  346.  
  347. return $this;
  348. }
  349.  
  350. /**
  351. * {@inheritdoc}
  352. */
  353. public function setUsernameCanonical($usernameCanonical)
  354. {
  355. $this->usernameCanonical = $usernameCanonical;
  356.  
  357. return $this;
  358. }
  359.  
  360. /**
  361. * {@inheritdoc}
  362. */
  363. public function setSalt($salt)
  364. {
  365. $this->salt = $salt;
  366.  
  367. return $this;
  368. }
  369.  
  370. /**
  371. * {@inheritdoc}
  372. */
  373. public function setEmail($email)
  374. {
  375. $this->email = $email;
  376.  
  377. return $this;
  378. }
  379.  
  380. /**
  381. * {@inheritdoc}
  382. */
  383. public function setEmailCanonical($emailCanonical)
  384. {
  385. $this->emailCanonical = $emailCanonical;
  386.  
  387. return $this;
  388. }
  389.  
  390. /**
  391. * {@inheritdoc}
  392. */
  393. public function setEnabled($boolean)
  394. {
  395. $this->enabled = (bool) $boolean;
  396.  
  397. return $this;
  398. }
  399.  
  400. /**
  401. * {@inheritdoc}
  402. */
  403. public function setPassword($password)
  404. {
  405. $this->password = $password;
  406.  
  407. return $this;
  408. }
  409.  
  410. /**
  411. * {@inheritdoc}
  412. */
  413. public function setSuperAdmin($boolean)
  414. {
  415. if (true === $boolean) {
  416. $this->addRole(static::ROLE_SUPER_ADMIN);
  417. } else {
  418. $this->removeRole(static::ROLE_SUPER_ADMIN);
  419. }
  420.  
  421. return $this;
  422. }
  423.  
  424. /**
  425. * {@inheritdoc}
  426. */
  427. public function setPlainPassword($password)
  428. {
  429. $this->plainPassword = $password;
  430.  
  431. return $this;
  432. }
  433.  
  434. /**
  435. * {@inheritdoc}
  436. */
  437. public function setLastLogin(\DateTime $time = null)
  438. {
  439. $this->lastLogin = $time;
  440.  
  441. return $this;
  442. }
  443.  
  444. /**
  445. * {@inheritdoc}
  446. */
  447. public function setConfirmationToken($confirmationToken)
  448. {
  449. $this->confirmationToken = $confirmationToken;
  450.  
  451. return $this;
  452. }
  453.  
  454. /**
  455. * {@inheritdoc}
  456. */
  457. public function setPasswordRequestedAt(\DateTime $date = null)
  458. {
  459. $this->passwordRequestedAt = $date;
  460.  
  461. return $this;
  462. }
  463.  
  464. /**
  465. * Gets the timestamp that the user requested a password reset.
  466. *
  467. * @return null|\DateTime
  468. */
  469. public function getPasswordRequestedAt()
  470. {
  471. return $this->passwordRequestedAt;
  472. }
  473.  
  474. /**
  475. * {@inheritdoc}
  476. */
  477. public function isPasswordRequestNonExpired($ttl)
  478. {
  479. return $this->getPasswordRequestedAt() instanceof \DateTime &&
  480. $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  481. }
  482.  
  483. /**
  484. * {@inheritdoc}
  485. */
  486. public function setRoles(array $roles)
  487. {
  488. $this->roles = array();
  489.  
  490. foreach ($roles as $role) {
  491. $this->addRole($role);
  492. }
  493.  
  494. return $this;
  495. }
  496.  
  497. /**
  498. * {@inheritdoc}
  499. */
  500. public function getGroups()
  501. {
  502. return $this->groups ?: $this->groups = new ArrayCollection();
  503. }
  504.  
  505. /**
  506. * {@inheritdoc}
  507. */
  508. public function getGroupNames()
  509. {
  510. $names = array();
  511. foreach ($this->getGroups() as $group) {
  512. $names[] = $group->getName();
  513. }
  514.  
  515. return $names;
  516. }
  517.  
  518. /**
  519. * {@inheritdoc}
  520. */
  521. public function hasGroup($name)
  522. {
  523. return in_array($name, $this->getGroupNames());
  524. }
  525.  
  526. /**
  527. * {@inheritdoc}
  528. */
  529. public function addGroup(GroupInterface $group)
  530. {
  531. if (!$this->getGroups()->contains($group)) {
  532. $this->getGroups()->add($group);
  533. }
  534.  
  535. return $this;
  536. }
  537.  
  538. /**
  539. * {@inheritdoc}
  540. */
  541. public function removeGroup(GroupInterface $group)
  542. {
  543. if ($this->getGroups()->contains($group)) {
  544. $this->getGroups()->removeElement($group);
  545. }
  546.  
  547. return $this;
  548. }
  549.  
  550. /**
  551. * @return string
  552. */
  553. public function __toString()
  554. {
  555. return (string) $this->getUsername();
  556. }
  557. }
Add Comment
Please, Sign In to add comment