User.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 by the equals method and the username.
  126. *
  127. * @return string
  128. */
  129. public function serialize()
  130. {
  131. return serialize(array(
  132. $this->password,
  133. $this->salt,
  134. $this->usernameCanonical,
  135. $this->username,
  136. $this->expired,
  137. $this->locked,
  138. $this->credentialsExpired,
  139. $this->enabled,
  140. $this->id,
  141. ));
  142. }
  143. /**
  144. * Unserializes the user.
  145. *
  146. * @param string $serialized
  147. */
  148. public function unserialize($serialized)
  149. {
  150. $data = unserialize($serialized);
  151. // add a few extra elements in the array to ensure that we have enough keys when unserializing
  152. // older data which does not include all properties.
  153. $data = array_merge($data, array_fill(0, 2, null));
  154. list(
  155. $this->password,
  156. $this->salt,
  157. $this->usernameCanonical,
  158. $this->username,
  159. $this->expired,
  160. $this->locked,
  161. $this->credentialsExpired,
  162. $this->enabled,
  163. $this->id
  164. ) = $data;
  165. }
  166. /**
  167. * Removes sensitive data from the user.
  168. */
  169. public function eraseCredentials()
  170. {
  171. $this->plainPassword = null;
  172. }
  173. /**
  174. * Returns the user unique id.
  175. *
  176. * @return mixed
  177. */
  178. public function getId()
  179. {
  180. return $this->id;
  181. }
  182. public function getUsername()
  183. {
  184. return $this->username;
  185. }
  186. public function getUsernameCanonical()
  187. {
  188. return $this->usernameCanonical;
  189. }
  190. public function getSalt()
  191. {
  192. return $this->salt;
  193. }
  194. public function getEmail()
  195. {
  196. return $this->email;
  197. }
  198. public function getEmailCanonical()
  199. {
  200. return $this->emailCanonical;
  201. }
  202. /**
  203. * Gets the encrypted password.
  204. *
  205. * @return string
  206. */
  207. public function getPassword()
  208. {
  209. return $this->password;
  210. }
  211. public function getPlainPassword()
  212. {
  213. return $this->plainPassword;
  214. }
  215. /**
  216. * Gets the last login time.
  217. *
  218. * @return \DateTime
  219. */
  220. public function getLastLogin()
  221. {
  222. return $this->lastLogin;
  223. }
  224. public function getConfirmationToken()
  225. {
  226. return $this->confirmationToken;
  227. }
  228. /**
  229. * Returns the user roles
  230. *
  231. * @return array The roles
  232. */
  233. public function getRoles()
  234. {
  235. $roles = $this->roles;
  236. foreach ($this->getGroups() as $group) {
  237. $roles = array_merge($roles, $group->getRoles());
  238. }
  239. // we need to make sure to have at least one role
  240. $roles[] = static::ROLE_DEFAULT;
  241. return array_unique($roles);
  242. }
  243. /**
  244. * Never use this to check if this user has access to anything!
  245. *
  246. * Use the SecurityContext, or an implementation of AccessDecisionManager
  247. * instead, e.g.
  248. *
  249. * $securityContext->isGranted('ROLE_USER');
  250. *
  251. * @param string $role
  252. *
  253. * @return boolean
  254. */
  255. public function hasRole($role)
  256. {
  257. return in_array(strtoupper($role), $this->getRoles(), true);
  258. }
  259. public function isAccountNonExpired()
  260. {
  261. if (true === $this->expired) {
  262. return false;
  263. }
  264. if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) {
  265. return false;
  266. }
  267. return true;
  268. }
  269. public function isAccountNonLocked()
  270. {
  271. return !$this->locked;
  272. }
  273. public function isCredentialsNonExpired()
  274. {
  275. if (true === $this->credentialsExpired) {
  276. return false;
  277. }
  278. if (null !== $this->credentialsExpireAt && $this->credentialsExpireAt->getTimestamp() < time()) {
  279. return false;
  280. }
  281. return true;
  282. }
  283. public function isCredentialsExpired()
  284. {
  285. return !$this->isCredentialsNonExpired();
  286. }
  287. public function isEnabled()
  288. {
  289. return $this->enabled;
  290. }
  291. public function isExpired()
  292. {
  293. return !$this->isAccountNonExpired();
  294. }
  295. public function isLocked()
  296. {
  297. return !$this->isAccountNonLocked();
  298. }
  299. public function isSuperAdmin()
  300. {
  301. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  302. }
  303. public function isUser(UserInterface $user = null)
  304. {
  305. return null !== $user && $this->getId() === $user->getId();
  306. }
  307. public function removeRole($role)
  308. {
  309. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  310. unset($this->roles[$key]);
  311. $this->roles = array_values($this->roles);
  312. }
  313. return $this;
  314. }
  315. public function setUsername($username)
  316. {
  317. $this->username = $username;
  318. return $this;
  319. }
  320. public function setUsernameCanonical($usernameCanonical)
  321. {
  322. $this->usernameCanonical = $usernameCanonical;
  323. return $this;
  324. }
  325. /**
  326. * @param \DateTime $date
  327. *
  328. * @return User
  329. */
  330. public function setCredentialsExpireAt(\DateTime $date)
  331. {
  332. $this->credentialsExpireAt = $date;
  333. return $this;
  334. }
  335. /**
  336. * @param boolean $boolean
  337. *
  338. * @return User
  339. */
  340. public function setCredentialsExpired($boolean)
  341. {
  342. $this->credentialsExpired = $boolean;
  343. return $this;
  344. }
  345. public function setEmail($email)
  346. {
  347. $this->email = $email;
  348. return $this;
  349. }
  350. public function setEmailCanonical($emailCanonical)
  351. {
  352. $this->emailCanonical = $emailCanonical;
  353. return $this;
  354. }
  355. public function setEnabled($boolean)
  356. {
  357. $this->enabled = (Boolean) $boolean;
  358. return $this;
  359. }
  360. /**
  361. * Sets this user to expired.
  362. *
  363. * @param Boolean $boolean
  364. *
  365. * @return User
  366. */
  367. public function setExpired($boolean)
  368. {
  369. $this->expired = (Boolean) $boolean;
  370. return $this;
  371. }
  372. /**
  373. * @param \DateTime $date
  374. *
  375. * @return User
  376. */
  377. public function setExpiresAt(\DateTime $date)
  378. {
  379. $this->expiresAt = $date;
  380. return $this;
  381. }
  382. public function setPassword($password)
  383. {
  384. $this->password = $password;
  385. return $this;
  386. }
  387. public function setSuperAdmin($boolean)
  388. {
  389. if (true === $boolean) {
  390. $this->addRole(static::ROLE_SUPER_ADMIN);
  391. } else {
  392. $this->removeRole(static::ROLE_SUPER_ADMIN);
  393. }
  394. return $this;
  395. }
  396. public function setPlainPassword($password)
  397. {
  398. $this->plainPassword = $password;
  399. return $this;
  400. }
  401. public function setLastLogin(\DateTime $time)
  402. {
  403. $this->lastLogin = $time;
  404. return $this;
  405. }
  406. public function setLocked($boolean)
  407. {
  408. $this->locked = $boolean;
  409. return $this;
  410. }
  411. public function setConfirmationToken($confirmationToken)
  412. {
  413. $this->confirmationToken = $confirmationToken;
  414. return $this;
  415. }
  416. public function setPasswordRequestedAt(\DateTime $date = null)
  417. {
  418. $this->passwordRequestedAt = $date;
  419. return $this;
  420. }
  421. /**
  422. * Gets the timestamp that the user requested a password reset.
  423. *
  424. * @return null|\DateTime
  425. */
  426. public function getPasswordRequestedAt()
  427. {
  428. return $this->passwordRequestedAt;
  429. }
  430. public function isPasswordRequestNonExpired($ttl)
  431. {
  432. return $this->getPasswordRequestedAt() instanceof \DateTime &&
  433. $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  434. }
  435. public function setRoles(array $roles)
  436. {
  437. $this->roles = array();
  438. foreach ($roles as $role) {
  439. $this->addRole($role);
  440. }
  441. return $this;
  442. }
  443. /**
  444. * Gets the groups granted to the user.
  445. *
  446. * @return Collection
  447. */
  448. public function getGroups()
  449. {
  450. return $this->groups ?: $this->groups = new ArrayCollection();
  451. }
  452. public function getGroupNames()
  453. {
  454. $names = array();
  455. foreach ($this->getGroups() as $group) {
  456. $names[] = $group->getName();
  457. }
  458. return $names;
  459. }
  460. public function hasGroup($name)
  461. {
  462. return in_array($name, $this->getGroupNames());
  463. }
  464. public function addGroup(GroupInterface $group)
  465. {
  466. if (!$this->getGroups()->contains($group)) {
  467. $this->getGroups()->add($group);
  468. }
  469. return $this;
  470. }
  471. public function removeGroup(GroupInterface $group)
  472. {
  473. if ($this->getGroups()->contains($group)) {
  474. $this->getGroups()->removeElement($group);
  475. }
  476. return $this;
  477. }
  478. public function __toString()
  479. {
  480. return (string) $this->getUsername();
  481. }
  482. }