User.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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\Propel;
  11. use FOS\UserBundle\Model\GroupableInterface;
  12. use FOS\UserBundle\Model\UserInterface;
  13. use FOS\UserBundle\Propel\om\BaseUser;
  14. class User extends BaseUser implements UserInterface, GroupableInterface
  15. {
  16. /**
  17. * Plain password. Used when changing the password. Must not be persisted.
  18. *
  19. * @var string
  20. */
  21. protected $plainPassword;
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. if ($this->isNew()) {
  26. $this->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36));
  27. }
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function serialize()
  33. {
  34. return serialize(
  35. array(
  36. $this->id,
  37. $this->username,
  38. $this->salt,
  39. $this->password,
  40. $this->expired,
  41. $this->locked,
  42. $this->credentials_expired,
  43. $this->enabled,
  44. $this->_new,
  45. )
  46. );
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function unserialize($serialized)
  52. {
  53. $data = unserialize($serialized);
  54. // add a few extra elements in the array to ensure that we have enough keys when unserializing
  55. // older data which does not include all properties.
  56. $data = array_merge($data, array_fill(0, 1, null));
  57. list(
  58. $this->id,
  59. $this->username,
  60. $this->salt,
  61. $this->password,
  62. $this->expired,
  63. $this->locked,
  64. $this->credentials_expired,
  65. $this->enabled,
  66. $this->_new
  67. ) = $data;
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function eraseCredentials()
  73. {
  74. $this->plainPassword = null;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function setPlainPassword($plainPassword)
  80. {
  81. $this->plainPassword = $plainPassword;
  82. return $this;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function getPlainPassword()
  88. {
  89. return $this->plainPassword;
  90. }
  91. /**
  92. * Returns the user roles
  93. *
  94. * Implements SecurityUserInterface
  95. *
  96. * @return array The roles
  97. */
  98. public function getRoles()
  99. {
  100. $roles = parent::getRoles();
  101. foreach ($this->getGroups() as $group) {
  102. $roles = array_merge($roles, $group->getRoles());
  103. }
  104. // we need to make sure to have at least one role
  105. $roles[] = static::ROLE_DEFAULT;
  106. return array_unique($roles);
  107. }
  108. /**
  109. * Adds a role to the user.
  110. *
  111. * @param string $role
  112. *
  113. * @return User
  114. */
  115. public function addRole($role)
  116. {
  117. $role = strtoupper($role);
  118. if ($role === static::ROLE_DEFAULT) {
  119. return $this;
  120. }
  121. parent::addRole($role);
  122. return $this;
  123. }
  124. public function hasRole($value)
  125. {
  126. return parent::hasRole(strtoupper($value));
  127. }
  128. public function removeRole($value)
  129. {
  130. return parent::removeRole(strtoupper($value));
  131. }
  132. public function setRoles(array $v)
  133. {
  134. foreach ($v as $i => $role) {
  135. $v[$i] = strtoupper($role);
  136. }
  137. return parent::setRoles($v);
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function isAccountNonExpired()
  143. {
  144. if (true === $this->getExpired()) {
  145. return false;
  146. }
  147. if (null !== $this->getExpiresAt() && $this->getExpiresAt()->getTimestamp() < time()) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * {@inheritDoc}
  154. */
  155. public function isAccountNonLocked()
  156. {
  157. return !$this->getLocked();
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public function isCredentialsNonExpired()
  163. {
  164. if (true === $this->getCredentialsExpired()) {
  165. return false;
  166. }
  167. if (null !== $this->getCredentialsExpireAt() && $this->getCredentialsExpireAt()->getTimestamp() < time()) {
  168. return false;
  169. }
  170. return true;
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public function isEnabled()
  176. {
  177. return $this->getEnabled();
  178. }
  179. /**
  180. * {@inheritDoc}
  181. */
  182. public function isSuperAdmin()
  183. {
  184. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  185. }
  186. /**
  187. * {@inheritDoc}
  188. */
  189. public function isUser(UserInterface $user = null)
  190. {
  191. return null !== $user && $this->getId() === $user->getId();
  192. }
  193. /**
  194. * {@inheritDoc}
  195. */
  196. public function setSuperAdmin($boolean)
  197. {
  198. if ($boolean) {
  199. $this->addRole(static::ROLE_SUPER_ADMIN);
  200. } else {
  201. $this->removeRole(static::ROLE_SUPER_ADMIN);
  202. }
  203. return $this;
  204. }
  205. /**
  206. * {@inheritDoc}
  207. */
  208. public function isPasswordRequestNonExpired($ttl)
  209. {
  210. return $this->getPasswordRequestedAt() instanceof \DateTime &&
  211. $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  212. }
  213. /**
  214. * Gets the name of the groups which includes the user.
  215. *
  216. * @return array
  217. */
  218. public function getGroupNames()
  219. {
  220. $names = array();
  221. foreach ($this->getGroups() as $group) {
  222. $names[] = $group->getName();
  223. }
  224. return $names;
  225. }
  226. /**
  227. * Indicates whether the user belongs to the specified group or not.
  228. *
  229. * @param string $name Name of the group
  230. *
  231. * @return Boolean
  232. */
  233. public function hasGroup($name)
  234. {
  235. return in_array($name, $this->getGroupNames());
  236. }
  237. }