PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/sfDoctrineGuardPlugin/lib/user/sfGuardSecurityUser.class.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 391 lines | 176 code | 44 blank | 171 comment | 9 complexity | 41af2bfc8019a765beebe6e71f917dcb MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. *
  11. * @package symfony
  12. * @subpackage plugin
  13. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. * @version SVN: $Id: sfGuardSecurityUser.class.php 30264 2010-07-16 16:59:21Z Jonathan.Wage $
  15. */
  16. class sfGuardSecurityUser extends sfBasicSecurityUser
  17. {
  18. protected $user = null;
  19. /**
  20. * Initializes the sfGuardSecurityUser object.
  21. *
  22. * @param sfEventDispatcher $dispatcher The event dispatcher object
  23. * @param sfStorage $storage The session storage object
  24. * @param array $options An array of options
  25. */
  26. public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  27. {
  28. parent::initialize($dispatcher, $storage, $options);
  29. if (!$this->isAuthenticated())
  30. {
  31. // remove user if timeout
  32. $this->getAttributeHolder()->removeNamespace('sfGuardSecurityUser');
  33. $this->user = null;
  34. }
  35. }
  36. /**
  37. * Returns the referer uri.
  38. *
  39. * @param string $default The default uri to return
  40. * @return string $referer The referer
  41. */
  42. public function getReferer($default)
  43. {
  44. $referer = $this->getAttribute('referer', $default);
  45. $this->getAttributeHolder()->remove('referer');
  46. return $referer;
  47. }
  48. /**
  49. * Sets the referer.
  50. *
  51. * @param string $referer
  52. */
  53. public function setReferer($referer)
  54. {
  55. if (!$this->hasAttribute('referer'))
  56. {
  57. $this->setAttribute('referer', $referer);
  58. }
  59. }
  60. /**
  61. * Returns whether or not the user has the given credential.
  62. *
  63. * @param string $credential The credential name
  64. * @param boolean $useAnd Whether or not to use an AND condition
  65. * @return boolean
  66. */
  67. public function hasCredential($credential, $useAnd = true)
  68. {
  69. if (empty($credential))
  70. {
  71. return true;
  72. }
  73. if (!$this->getGuardUser())
  74. {
  75. return false;
  76. }
  77. if ($this->getGuardUser()->getIsSuperAdmin())
  78. {
  79. return true;
  80. }
  81. return parent::hasCredential($credential, $useAnd);
  82. }
  83. /**
  84. * Returns whether or not the user is a super admin.
  85. *
  86. * @return boolean
  87. */
  88. public function isSuperAdmin()
  89. {
  90. return $this->getGuardUser() ? $this->getGuardUser()->getIsSuperAdmin() : false;
  91. }
  92. /**
  93. * Returns whether or not the user is anonymous.
  94. *
  95. * @return boolean
  96. */
  97. public function isAnonymous()
  98. {
  99. return !$this->isAuthenticated();
  100. }
  101. /**
  102. * Signs in the user on the application.
  103. *
  104. * @param sfGuardUser $user The sfGuardUser id
  105. * @param boolean $remember Whether or not to remember the user
  106. * @param Doctrine_Connection $con A Doctrine_Connection object
  107. */
  108. public function signIn($user, $remember = false, $con = null)
  109. {
  110. // signin
  111. $this->setAttribute('user_id', $user->getId(), 'sfGuardSecurityUser');
  112. $this->setAuthenticated(true);
  113. $this->clearCredentials();
  114. $this->addCredentials($user->getAllPermissionNames());
  115. // save last login
  116. $user->setLastLogin(date('Y-m-d H:i:s'));
  117. $user->save($con);
  118. // remember?
  119. if ($remember)
  120. {
  121. $expiration_age = sfConfig::get('app_sf_guard_plugin_remember_key_expiration_age', 15 * 24 * 3600);
  122. // remove old keys
  123. Doctrine_Core::getTable('sfGuardRememberKey')->createQuery()
  124. ->delete()
  125. ->where('created_at < ?', date('Y-m-d H:i:s', time() - $expiration_age))
  126. ->execute();
  127. // remove other keys from this user
  128. Doctrine_Core::getTable('sfGuardRememberKey')->createQuery()
  129. ->delete()
  130. ->where('user_id = ?', $user->getId())
  131. ->execute();
  132. // generate new keys
  133. $key = $this->generateRandomKey();
  134. // save key
  135. $rk = new sfGuardRememberKey();
  136. $rk->setRememberKey($key);
  137. $rk->setUser($user);
  138. $rk->setIpAddress($_SERVER['REMOTE_ADDR']);
  139. $rk->save($con);
  140. // make key as a cookie
  141. $remember_cookie = sfConfig::get('app_sf_guard_plugin_remember_cookie_name', 'sfRemember');
  142. sfContext::getInstance()->getResponse()->setCookie($remember_cookie, $key, time() + $expiration_age);
  143. }
  144. }
  145. /**
  146. * Returns a random generated key.
  147. *
  148. * @param int $len The key length
  149. * @return string
  150. */
  151. protected function generateRandomKey($len = 20)
  152. {
  153. return base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  154. }
  155. /**
  156. * Signs out the user.
  157. *
  158. */
  159. public function signOut()
  160. {
  161. $this->getAttributeHolder()->removeNamespace('sfGuardSecurityUser');
  162. $this->user = null;
  163. $this->clearCredentials();
  164. $this->setAuthenticated(false);
  165. $expiration_age = sfConfig::get('app_sf_guard_plugin_remember_key_expiration_age', 15 * 24 * 3600);
  166. $remember_cookie = sfConfig::get('app_sf_guard_plugin_remember_cookie_name', 'sfRemember');
  167. sfContext::getInstance()->getResponse()->setCookie($remember_cookie, '', time() - $expiration_age);
  168. }
  169. /**
  170. * Returns the related sfGuardUser.
  171. *
  172. * @return sfGuardUser
  173. */
  174. public function getGuardUser()
  175. {
  176. if (!$this->user && $id = $this->getAttribute('user_id', null, 'sfGuardSecurityUser'))
  177. {
  178. $this->user = Doctrine_Core::getTable('sfGuardUser')->find($id);
  179. if (!$this->user)
  180. {
  181. // the user does not exist anymore in the database
  182. $this->signOut();
  183. throw new sfException('The user does not exist anymore in the database.');
  184. }
  185. }
  186. return $this->user;
  187. }
  188. /**
  189. * Returns the string representation of the object.
  190. *
  191. * @return string
  192. */
  193. public function __toString()
  194. {
  195. return $this->getGuardUser()->__toString();
  196. }
  197. /**
  198. * Returns the sfGuardUser object's username.
  199. *
  200. * @return string
  201. */
  202. public function getUsername()
  203. {
  204. return $this->getGuardUser()->getUsername();
  205. }
  206. /**
  207. * Returns the name(first and last) of the user
  208. *
  209. * @return string
  210. */
  211. public function getName()
  212. {
  213. return $this->getGuardUser()->getName();
  214. }
  215. /**
  216. * Returns the sfGuardUser object's email.
  217. *
  218. * @return string
  219. */
  220. public function getEmail()
  221. {
  222. return $this->getGuardUser()->getEmail();
  223. }
  224. /**
  225. * Sets the user's password.
  226. *
  227. * @param string $password The password
  228. * @param Doctrine_Collection $con A Doctrine_Connection object
  229. */
  230. public function setPassword($password, $con = null)
  231. {
  232. $this->getGuardUser()->setPassword($password);
  233. $this->getGuardUser()->save($con);
  234. }
  235. /**
  236. * Returns whether or not the given password is valid.
  237. *
  238. * @return boolean
  239. */
  240. public function checkPassword($password)
  241. {
  242. return $this->getGuardUser()->checkPassword($password);
  243. }
  244. /**
  245. * Returns whether or not the user belongs to the given group.
  246. *
  247. * @param string $name The group name
  248. * @return boolean
  249. */
  250. public function hasGroup($name)
  251. {
  252. return $this->getGuardUser() ? $this->getGuardUser()->hasGroup($name) : false;
  253. }
  254. /**
  255. * Returns the user's groups.
  256. *
  257. * @return array|Doctrine_Collection
  258. */
  259. public function getGroups()
  260. {
  261. return $this->getGuardUser() ? $this->getGuardUser()->getGroups() : array();
  262. }
  263. /**
  264. * Returns the user's group names.
  265. *
  266. * @return array
  267. */
  268. public function getGroupNames()
  269. {
  270. return $this->getGuardUser() ? $this->getGuardUser()->getGroupNames() : array();
  271. }
  272. /**
  273. * Returns whether or not the user has the given permission.
  274. *
  275. * @param string $name The permission name
  276. * @return string
  277. */
  278. public function hasPermission($name)
  279. {
  280. return $this->getGuardUser() ? $this->getGuardUser()->hasPermission($name) : false;
  281. }
  282. /**
  283. * Returns the Doctrine_Collection of single sfGuardPermission objects.
  284. *
  285. * @return Doctrine_Collection
  286. */
  287. public function getPermissions()
  288. {
  289. return $this->getGuardUser()->getPermissions();
  290. }
  291. /**
  292. * Returns the array of permissions names.
  293. *
  294. * @return array
  295. */
  296. public function getPermissionNames()
  297. {
  298. return $this->getGuardUser() ? $this->getGuardUser()->getPermissionNames() : array();
  299. }
  300. /**
  301. * Returns the array of all permissions.
  302. *
  303. * @return array
  304. */
  305. public function getAllPermissions()
  306. {
  307. return $this->getGuardUser() ? $this->getGuardUser()->getAllPermissions() : array();
  308. }
  309. /**
  310. * Returns the array of all permissions names.
  311. *
  312. * @return array
  313. */
  314. public function getAllPermissionNames()
  315. {
  316. return $this->getGuardUser() ? $this->getGuardUser()->getAllPermissionNames() : array();
  317. }
  318. /**
  319. * Returns the related profile object.
  320. *
  321. * @return Doctrine_Record
  322. */
  323. public function getProfile()
  324. {
  325. return $this->getGuardUser() ? $this->getGuardUser()->getProfile() : null;
  326. }
  327. /**
  328. * Adds a group from its name to the current user.
  329. *
  330. * @param string $name The group name
  331. * @param Doctrine_Connection $con A Doctrine_Connection object
  332. */
  333. public function addGroupByName($name, $con = null)
  334. {
  335. return $this->getGuardUser()->addGroupByName($name, $con);
  336. }
  337. /**
  338. * Adds a permission from its name to the current user.
  339. *
  340. * @param string $name The permission name
  341. * @param Doctrine_Connection $con A Doctrine_Connection object
  342. */
  343. public function addPermissionByName($name, $con = null)
  344. {
  345. return $this->getGuardUser()->addPermissionByName($name, $con);
  346. }
  347. }