PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/securityUser.class.php

https://github.com/rollmax/read2read
PHP | 346 lines | 166 code | 42 blank | 138 comment | 9 complexity | c099aa08fa8f7105bfe85fe8b0eb2c79 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. class securityUser extends sfBasicSecurityUser
  3. {
  4. protected $user = null;
  5. /**
  6. * Initializes the securityUser object.
  7. *
  8. * @param sfEventDispatcher $dispatcher The event dispatcher object
  9. * @param sfStorage $storage The session storage object
  10. * @param array $options An array of options
  11. */
  12. public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  13. {
  14. parent::initialize($dispatcher, $storage, $options);
  15. if (!$this->isAuthenticated())
  16. {
  17. // remove user if timeout
  18. $this->getAttributeHolder()->removeNamespace('securityUser');
  19. $this->user = null;
  20. }
  21. }
  22. /**
  23. * Returns the id for authenticated user or null
  24. */
  25. public function getId()
  26. {
  27. if (!$this->isAuthenticated())
  28. {
  29. return null;
  30. }
  31. return $this->getGuardUser()->getId();
  32. }
  33. /**
  34. * Returns the referer uri.
  35. *
  36. * @param string $default The default uri to return
  37. * @return string $referer The referer
  38. */
  39. public function getReferer($default)
  40. {
  41. $referer = $this->getAttribute('referer', $default);
  42. $this->getAttributeHolder()->remove('referer');
  43. return $referer;
  44. }
  45. /**
  46. * Sets the referer.
  47. *
  48. * @param string $referer
  49. */
  50. public function setReferer($referer)
  51. {
  52. if (!$this->hasAttribute('referer'))
  53. {
  54. $this->setAttribute('referer', $referer);
  55. }
  56. }
  57. /**
  58. * Returns whether or not the user has the given credential.
  59. *
  60. * @param string $credential The credential name
  61. * @param boolean $useAnd Whether or not to use an AND condition
  62. * @return boolean
  63. */
  64. public function hasCredential($credential, $useAnd = true)
  65. {
  66. if (empty($credential))
  67. {
  68. return true;
  69. }
  70. if (!$this->getGuardUser())
  71. {
  72. return false;
  73. }
  74. return parent::hasCredential($credential, $useAnd);
  75. }
  76. /**
  77. * Returns whether or not the user is anonymous.
  78. *
  79. * @return boolean
  80. */
  81. public function isAnonymous()
  82. {
  83. return !$this->isAuthenticated();
  84. }
  85. /**
  86. * Signs in the user on the application.
  87. *
  88. * @param User $user The User id
  89. * @param boolean $remember Whether or not to remember the user
  90. * @param Doctrine_Connection $con A Doctrine_Connection object
  91. */
  92. public function signIn($user, $remember = false, $con = null)
  93. {
  94. // signin
  95. $this->setAttribute('user_id', $user->getId(), 'securityUser');
  96. $this->setAuthenticated(true);
  97. $this->clearCredentials();
  98. $this->addCredentials($user->getAllPermissionNames());
  99. // remember?
  100. if ($remember)
  101. {
  102. $expiration_age = sfConfig::get('app_user_remember_key_expiration_age', 14 * 86400);
  103. // remove old keys
  104. Doctrine::getTable('UserRememberKey')->createQuery()
  105. ->delete()
  106. ->where('created_at < ?', date('Y-m-d H:i:s', time() - $expiration_age))
  107. ->execute();
  108. // remove other keys from this user
  109. Doctrine::getTable('UserRememberKey')->createQuery()
  110. ->delete()
  111. ->where('user_id = ?', $user->getId())
  112. ->execute();
  113. // generate new keys
  114. $key = $this->generateRandomKey();
  115. // save key
  116. $rk = new UserRememberKey();
  117. $rk->remember_key=$key;
  118. $rk->User=$user;
  119. $rk->ip_address=$_SERVER['REMOTE_ADDR'];
  120. $rk->save($con);
  121. // make key as a cookie
  122. $remember_cookie = 'remember';
  123. sfContext::getInstance()->getResponse()->setCookie($remember_cookie, $key, time() + $expiration_age);
  124. }
  125. }
  126. function generateRandomKey()
  127. {
  128. return base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  129. }
  130. /**
  131. * Signs out the user.
  132. *
  133. */
  134. public function signOut()
  135. {
  136. $this->getAttributeHolder()->removeNamespace('securityUser');
  137. $this->user = null;
  138. $this->clearCredentials();
  139. $this->setAuthenticated(false);
  140. $expiration_age = sfConfig::get('app_user_remember_key_expiration_age', 14 * 24 * 3600);
  141. $remember_cookie = 'remember';
  142. sfContext::getInstance()->getResponse()->setCookie($remember_cookie, '', time() - $expiration_age);
  143. }
  144. /**
  145. * Returns the related User.
  146. *
  147. * @return User
  148. */
  149. public function getGuardUser()
  150. {
  151. if (!$this->user && $id = $this->getAttribute('user_id', null, 'securityUser'))
  152. {
  153. $this->user = Doctrine::getTable('User')->find($id);
  154. if (!$this->user)
  155. {
  156. // the user does not exist anymore in the database
  157. $this->signOut();
  158. throw new sfException('Этого пользователя больше нет в БД');
  159. }
  160. }
  161. return $this->user;
  162. }
  163. /**
  164. * Returns the string representation of the object.
  165. *
  166. * @return string
  167. */
  168. public function __toString()
  169. {
  170. return $this->getGuardUser()->__toString();
  171. }
  172. /**
  173. * Returns the User object's email.
  174. *
  175. * @return string
  176. */
  177. public function getEmail()
  178. {
  179. return $this->getGuardUser()->getEmail();
  180. }
  181. /**
  182. * Sets the user's password.
  183. *
  184. * @param string $password The password
  185. * @param Doctrine_Collection $con A Doctrine_Connection object
  186. */
  187. public function setPassword($password, $con = null)
  188. {
  189. $this->getGuardUser()->setPassword($password);
  190. $this->getGuardUser()->save($con);
  191. }
  192. /**
  193. * Returns whether or not the given password is valid.
  194. *
  195. * @return boolean
  196. */
  197. public function checkPassword($password)
  198. {
  199. return $this->getGuardUser()->checkPassword($password);
  200. }
  201. /**
  202. * Returns whether or not the user belongs to the given group.
  203. *
  204. * @param string $name The group name
  205. * @return boolean
  206. */
  207. public function hasGroup($name)
  208. {
  209. return $this->getGuardUser() ? $this->getGuardUser()->hasGroup($name) : false;
  210. }
  211. /**
  212. * Returns the user's groups.
  213. *
  214. * @return array|Doctrine_Collection
  215. */
  216. public function getGroups()
  217. {
  218. return $this->getGuardUser() ? $this->getGuardUser()->getGroups() : array();
  219. }
  220. /**
  221. * Returns the user's group names.
  222. *
  223. * @return array
  224. */
  225. public function getGroupNames()
  226. {
  227. return $this->getGuardUser() ? $this->getGuardUser()->getGroupNames() : array();
  228. }
  229. /**
  230. * Returns whether or not the user has the given permission.
  231. *
  232. * @param string $name The permission name
  233. * @return string
  234. */
  235. public function hasPermission($name)
  236. {
  237. return $this->getGuardUser() ? $this->getGuardUser()->hasPermission($name) : false;
  238. }
  239. /**
  240. * Returns the Doctrine_Collection of single UserPermission objects.
  241. *
  242. * @return Doctrine_Collection
  243. */
  244. public function getPermissions()
  245. {
  246. return $this->getGuardUser()->getPermissions();
  247. }
  248. /**
  249. * Returns the array of permissions names.
  250. *
  251. * @return array
  252. */
  253. public function getPermissionNames()
  254. {
  255. return $this->getGuardUser() ? $this->getGuardUser()->getPermissionNames() : array();
  256. }
  257. /**
  258. * Returns the array of all permissions.
  259. *
  260. * @return array
  261. */
  262. public function getAllPermissions()
  263. {
  264. return $this->getGuardUser() ? $this->getGuardUser()->getAllPermissions() : array();
  265. }
  266. /**
  267. * Returns the array of all permissions names.
  268. *
  269. * @return array
  270. */
  271. public function getAllPermissionNames()
  272. {
  273. return $this->getGuardUser() ? $this->getGuardUser()->getAllPermissionNames() : array();
  274. }
  275. /**
  276. * Returns the related profile object.
  277. *
  278. * @return Doctrine_Record
  279. */
  280. public function getProfile()
  281. {
  282. return $this->getGuardUser() ? $this->getGuardUser()->getProfile() : null;
  283. }
  284. /**
  285. * Adds a group from its name to the current user.
  286. *
  287. * @param string $name The group name
  288. * @param Doctrine_Connection $con A Doctrine_Connection object
  289. */
  290. public function addGroupByName($name, $con = null)
  291. {
  292. return $this->getGuardUser()->addGroupByName($name, $con);
  293. }
  294. /**
  295. * Adds a permission from its name to the current user.
  296. *
  297. * @param string $name The permission name
  298. * @param Doctrine_Connection $con A Doctrine_Connection object
  299. */
  300. public function addPermissionByName($name, $con = null)
  301. {
  302. return $this->getGuardUser()->addPermissionByName($name, $con);
  303. }
  304. }