PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/friendsofsymfony/user-bundle/Model/User.php

https://gitlab.com/galaxy-pidev/AllForDealWeb
PHP | 595 lines | 328 code | 105 blank | 162 comment | 13 complexity | 83cfa6459222ad3765561a71f7817abb MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Model;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. /**
  14. * Storage agnostic user object
  15. *
  16. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. abstract class User implements UserInterface, GroupableInterface
  20. {
  21. protected $id;
  22. /**
  23. * @var string
  24. */
  25. protected $username;
  26. /**
  27. * @var string
  28. */
  29. protected $usernameCanonical;
  30. /**
  31. * @var string
  32. */
  33. protected $email;
  34. /**
  35. * @var string
  36. */
  37. protected $emailCanonical;
  38. /**
  39. * @var boolean
  40. */
  41. protected $enabled;
  42. /**
  43. * The salt to use for hashing
  44. *
  45. * @var string
  46. */
  47. protected $salt;
  48. /**
  49. * Encrypted password. Must be persisted.
  50. *
  51. * @var string
  52. */
  53. protected $password;
  54. /**
  55. * Plain password. Used for model validation. Must not be persisted.
  56. *
  57. * @var string
  58. */
  59. protected $plainPassword;
  60. /**
  61. * @var \DateTime
  62. */
  63. protected $lastLogin;
  64. /**
  65. * Random string sent to the user email address in order to verify it
  66. *
  67. * @var string
  68. */
  69. protected $confirmationToken;
  70. /**
  71. * @var \DateTime
  72. */
  73. protected $passwordRequestedAt;
  74. /**
  75. * @var Collection
  76. */
  77. protected $groups;
  78. /**
  79. * @var boolean
  80. */
  81. protected $locked;
  82. /**
  83. * @var boolean
  84. */
  85. protected $expired;
  86. /**
  87. * @var \DateTime
  88. */
  89. protected $expiresAt;
  90. /**
  91. * @var array
  92. */
  93. protected $roles;
  94. /**
  95. * @var boolean
  96. */
  97. protected $credentialsExpired;
  98. /**
  99. * @var \DateTime
  100. */
  101. protected $credentialsExpireAt;
  102. public function __construct()
  103. {
  104. $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  105. $this->enabled = false;
  106. $this->locked = false;
  107. $this->expired = false;
  108. $this->roles = array();
  109. $this->credentialsExpired = false;
  110. }
  111. public function addRole($role)
  112. {
  113. $role = strtoupper($role);
  114. if ($role === static::ROLE_DEFAULT) {
  115. return $this;
  116. }
  117. if (!in_array($role, $this->roles, true)) {
  118. $this->roles[] = $role;
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Serializes the user.
  124. *
  125. * The serialized data have to contain the fields used during check for
  126. * changes and the id.
  127. *
  128. * @return string
  129. */
  130. public function serialize()
  131. {
  132. return serialize(array(
  133. $this->password,
  134. $this->salt,
  135. $this->usernameCanonical,
  136. $this->username,
  137. $this->expired,
  138. $this->locked,
  139. $this->credentialsExpired,
  140. $this->enabled,
  141. $this->id,
  142. $this->expiresAt,
  143. $this->credentialsExpireAt,
  144. $this->email,
  145. $this->emailCanonical,
  146. ));
  147. }
  148. /**
  149. * Unserializes the user.
  150. *
  151. * @param string $serialized
  152. */
  153. public function unserialize($serialized)
  154. {
  155. $data = unserialize($serialized);
  156. // add a few extra elements in the array to ensure that we have enough keys when unserializing
  157. // older data which does not include all properties.
  158. $data = array_merge($data, array_fill(0, 2, null));
  159. list(
  160. $this->password,
  161. $this->salt,
  162. $this->usernameCanonical,
  163. $this->username,
  164. $this->expired,
  165. $this->locked,
  166. $this->credentialsExpired,
  167. $this->enabled,
  168. $this->id,
  169. $this->expiresAt,
  170. $this->credentialsExpireAt,
  171. $this->email,
  172. $this->emailCanonical
  173. ) = $data;
  174. }
  175. /**
  176. * Removes sensitive data from the user.
  177. */
  178. public function eraseCredentials()
  179. {
  180. $this->plainPassword = null;
  181. }
  182. /**
  183. * {@inheritDoc}
  184. */
  185. public function getId()
  186. {
  187. return $this->id;
  188. }
  189. public function getUsername()
  190. {
  191. return $this->username;
  192. }
  193. public function getUsernameCanonical()
  194. {
  195. return $this->usernameCanonical;
  196. }
  197. public function getSalt()
  198. {
  199. return $this->salt;
  200. }
  201. public function getEmail()
  202. {
  203. return $this->email;
  204. }
  205. public function getEmailCanonical()
  206. {
  207. return $this->emailCanonical;
  208. }
  209. /**
  210. * Gets the encrypted password.
  211. *
  212. * @return string
  213. */
  214. public function getPassword()
  215. {
  216. return $this->password;
  217. }
  218. public function getPlainPassword()
  219. {
  220. return $this->plainPassword;
  221. }
  222. /**
  223. * Gets the last login time.
  224. *
  225. * @return \DateTime
  226. */
  227. public function getLastLogin()
  228. {
  229. return $this->lastLogin;
  230. }
  231. public function getConfirmationToken()
  232. {
  233. return $this->confirmationToken;
  234. }
  235. /**
  236. * Returns the user roles
  237. *
  238. * @return array The roles
  239. */
  240. public function getRoles()
  241. {
  242. $roles = $this->roles;
  243. foreach ($this->getGroups() as $group) {
  244. $roles = array_merge($roles, $group->getRoles());
  245. }
  246. // we need to make sure to have at least one role
  247. $roles[] = static::ROLE_DEFAULT;
  248. return array_unique($roles);
  249. }
  250. /**
  251. * Never use this to check if this user has access to anything!
  252. *
  253. * Use the SecurityContext, or an implementation of AccessDecisionManager
  254. * instead, e.g.
  255. *
  256. * $securityContext->isGranted('ROLE_USER');
  257. *
  258. * @param string $role
  259. *
  260. * @return boolean
  261. */
  262. public function hasRole($role)
  263. {
  264. return in_array(strtoupper($role), $this->getRoles(), true);
  265. }
  266. public function isAccountNonExpired()
  267. {
  268. if (true === $this->expired) {
  269. return false;
  270. }
  271. if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. public function isAccountNonLocked()
  277. {
  278. return !$this->locked;
  279. }
  280. public function isCredentialsNonExpired()
  281. {
  282. if (true === $this->credentialsExpired) {
  283. return false;
  284. }
  285. if (null !== $this->credentialsExpireAt && $this->credentialsExpireAt->getTimestamp() < time()) {
  286. return false;
  287. }
  288. return true;
  289. }
  290. public function isCredentialsExpired()
  291. {
  292. return !$this->isCredentialsNonExpired();
  293. }
  294. public function isEnabled()
  295. {
  296. return $this->enabled;
  297. }
  298. public function isExpired()
  299. {
  300. return !$this->isAccountNonExpired();
  301. }
  302. public function isLocked()
  303. {
  304. return !$this->isAccountNonLocked();
  305. }
  306. public function isSuperAdmin()
  307. {
  308. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  309. }
  310. public function removeRole($role)
  311. {
  312. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  313. unset($this->roles[$key]);
  314. $this->roles = array_values($this->roles);
  315. }
  316. return $this;
  317. }
  318. public function setUsername($username)
  319. {
  320. $this->username = $username;
  321. return $this;
  322. }
  323. public function setUsernameCanonical($usernameCanonical)
  324. {
  325. $this->usernameCanonical = $usernameCanonical;
  326. return $this;
  327. }
  328. /**
  329. * @param \DateTime $date
  330. *
  331. * @return User
  332. */
  333. public function setCredentialsExpireAt(\DateTime $date = null)
  334. {
  335. $this->credentialsExpireAt = $date;
  336. return $this;
  337. }
  338. /**
  339. * @param boolean $boolean
  340. *
  341. * @return User
  342. */
  343. public function setCredentialsExpired($boolean)
  344. {
  345. $this->credentialsExpired = $boolean;
  346. return $this;
  347. }
  348. public function setEmail($email)
  349. {
  350. $this->email = $email;
  351. return $this;
  352. }
  353. public function setEmailCanonical($emailCanonical)
  354. {
  355. $this->emailCanonical = $emailCanonical;
  356. return $this;
  357. }
  358. public function setEnabled($boolean)
  359. {
  360. $this->enabled = (Boolean) $boolean;
  361. return $this;
  362. }
  363. /**
  364. * Sets this user to expired.
  365. *
  366. * @param Boolean $boolean
  367. *
  368. * @return User
  369. */
  370. public function setExpired($boolean)
  371. {
  372. $this->expired = (Boolean) $boolean;
  373. return $this;
  374. }
  375. /**
  376. * @param \DateTime $date
  377. *
  378. * @return User
  379. */
  380. public function setExpiresAt(\DateTime $date = null)
  381. {
  382. $this->expiresAt = $date;
  383. return $this;
  384. }
  385. public function setPassword($password)
  386. {
  387. $this->password = $password;
  388. return $this;
  389. }
  390. public function setSuperAdmin($boolean)
  391. {
  392. if (true === $boolean) {
  393. $this->addRole(static::ROLE_SUPER_ADMIN);
  394. } else {
  395. $this->removeRole(static::ROLE_SUPER_ADMIN);
  396. }
  397. return $this;
  398. }
  399. public function setPlainPassword($password)
  400. {
  401. $this->plainPassword = $password;
  402. return $this;
  403. }
  404. public function setLastLogin(\DateTime $time = null)
  405. {
  406. $this->lastLogin = $time;
  407. return $this;
  408. }
  409. public function setLocked($boolean)
  410. {
  411. $this->locked = $boolean;
  412. return $this;
  413. }
  414. public function setConfirmationToken($confirmationToken)
  415. {
  416. $this->confirmationToken = $confirmationToken;
  417. return $this;
  418. }
  419. public function setPasswordRequestedAt(\DateTime $date = null)
  420. {
  421. $this->passwordRequestedAt = $date;
  422. return $this;
  423. }
  424. /**
  425. * Gets the timestamp that the user requested a password reset.
  426. *
  427. * @return null|\DateTime
  428. */
  429. public function getPasswordRequestedAt()
  430. {
  431. return $this->passwordRequestedAt;
  432. }
  433. public function isPasswordRequestNonExpired($ttl)
  434. {
  435. return $this->getPasswordRequestedAt() instanceof \DateTime &&
  436. $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  437. }
  438. public function setRoles(array $roles)
  439. {
  440. $this->roles = array();
  441. foreach ($roles as $role) {
  442. $this->addRole($role);
  443. }
  444. return $this;
  445. }
  446. /**
  447. * Gets the groups granted to the user.
  448. *
  449. * @return Collection
  450. */
  451. public function getGroups()
  452. {
  453. return $this->groups ?: $this->groups = new ArrayCollection();
  454. }
  455. public function getGroupNames()
  456. {
  457. $names = array();
  458. foreach ($this->getGroups() as $group) {
  459. $names[] = $group->getName();
  460. }
  461. return $names;
  462. }
  463. /**
  464. * @param string $name
  465. *
  466. * @return boolean
  467. */
  468. public function hasGroup($name)
  469. {
  470. return in_array($name, $this->getGroupNames());
  471. }
  472. public function addGroup(GroupInterface $group)
  473. {
  474. if (!$this->getGroups()->contains($group)) {
  475. $this->getGroups()->add($group);
  476. }
  477. return $this;
  478. }
  479. public function removeGroup(GroupInterface $group)
  480. {
  481. if ($this->getGroups()->contains($group)) {
  482. $this->getGroups()->removeElement($group);
  483. }
  484. return $this;
  485. }
  486. public function __toString()
  487. {
  488. return (string) $this->getUsername();
  489. }
  490. }