Source for file User.php

Documentation is available at User.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Web
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Object.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Web/IUser.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Authentication and authorization.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Web
  34. 34:  *
  35. 35:  * @property-read IIdentity $identity 
  36. 36:  * @property   IAuthenticator $authenticationHandler 
  37. 37:  * @property   IAuthorizator $authorizationHandler 
  38. 38:  * @property-read int $signOutReason 
  39. 39:  * @property-read array $roles 
  40. 40:  * @property-read bool $authenticated 
  41. 41:  */
  42. 42: class User extends Object implements IUser
  43. 43: {
  44. 44:     /**#@+ sign-out reason {@link User::getSignOutReason()} */
  45. 45:     const MANUAL = 1;
  46. 46:     const INACTIVITY = 2;
  47. 47:     const BROWSER_CLOSED = 3;
  48. 48:     /**#@-*/
  49. 49:  
  50. 50:     /** @var string  default role for unauthenticated user */
  51. 51:     public $guestRole = 'guest';
  52. 52:  
  53. 53:     /** @var string  default role for authenticated user without own identity */
  54. 54:     public $authenticatedRole = 'authenticated';
  55. 55:  
  56. 56:     /** @var array of function(User $sender); Occurs when the user is successfully authenticated */
  57. 57:     public $onAuthenticated;
  58. 58:  
  59. 59:     /** @var array of function(User $sender); Occurs when the user is logged off */
  60. 60:     public $onSignedOut;
  61. 61:  
  62. 62:     /** @var IAuthenticator */
  63. 63:     private $authenticationHandler;
  64. 64:  
  65. 65:     /** @var IAuthorizator */
  66. 66:     private $authorizationHandler;
  67. 67:  
  68. 68:     /** @var string */
  69. 69:     private $namespace '';
  70. 70:  
  71. 71:     /** @var SessionNamespace */
  72. 72:     private $session;
  73. 73:  
  74. 74:  
  75. 75:  
  76. 76:     /********************* Authentication ****************d*g**/
  77. 77:  
  78. 78:  
  79. 79:  
  80. 80:     /**
  81. 81:      * Conducts the authentication process.
  82. 82:      * @param  string 
  83. 83:      * @param  string 
  84. 84:      * @param  mixed 
  85. 85:      * @return void 
  86. 86:      * @throws AuthenticationException if authentication was not successful
  87. 87:      */
  88. 88:     public function authenticate($username$password$extra NULL)
  89. 89:     {
  90. 90:         $handler $this->getAuthenticationHandler();
  91. 91:         if ($handler === NULL{
  92. 92:             throw new InvalidStateException('Authentication handler has not been set.');
  93. 93:         }
  94. 94:  
  95. 95:         $this->signOut(TRUE);
  96. 96:  
  97. 97:         $credentials array(
  98. 98:             IAuthenticator::USERNAME => $username,
  99. 99:             IAuthenticator::PASSWORD => $password,
  100. 100:             'extra' => $extra,
  101. 101:         );
  102. 102:  
  103. 103:         $this->setIdentity($handler->authenticate($credentials));
  104. 104:         $this->setAuthenticated(TRUE);
  105. 105:         $this->onAuthenticated($this);
  106. 106:     }
  107. 107:  
  108. 108:  
  109. 109:  
  110. 110:     /**
  111. 111:      * Logs off the user from the current session.
  112. 112:      * @param  bool  clear the identity from persistent storage?
  113. 113:      * @return void 
  114. 114:      */
  115. 115:     final public function signOut($clearIdentity FALSE)
  116. 116:     {
  117. 117:         if ($this->isAuthenticated()) {
  118. 118:             $this->setAuthenticated(FALSE);
  119. 119:             $this->onSignedOut($this);
  120. 120:         }
  121. 121:  
  122. 122:         if ($clearIdentity{
  123. 123:             $this->setIdentity(NULL);
  124. 124:         }
  125. 125:     }
  126. 126:  
  127. 127:  
  128. 128:  
  129. 129:     /**
  130. 130:      * Is this user authenticated?
  131. 131:      * @return bool 
  132. 132:      */
  133. 133:     final public function isAuthenticated()
  134. 134:     {
  135. 135:         $session $this->getSessionNamespace(FALSE);
  136. 136:         return $session && $session->authenticated;
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /**
  142. 142:      * Returns current user identity, if any.
  143. 143:      * @return IIdentity 
  144. 144:      */
  145. 145:     final public function getIdentity()
  146. 146:     {
  147. 147:         $session $this->getSessionNamespace(FALSE);
  148. 148:         return $session $session->identity NULL;
  149. 149:     }
  150. 150:  
  151. 151:  
  152. 152:  
  153. 153:     /**
  154. 154:      * Sets authentication handler.
  155. 155:      * @param  IAuthenticator 
  156. 156:      * @return void 
  157. 157:      */
  158. 158:     public function setAuthenticationHandler(IAuthenticator $handler)
  159. 159:     {
  160. 160:         $this->authenticationHandler $handler;
  161. 161:     }
  162. 162:  
  163. 163:  
  164. 164:  
  165. 165:     /**
  166. 166:      * Returns authentication handler.
  167. 167:      * @return IAuthenticator 
  168. 168:      */
  169. 169:     final public function getAuthenticationHandler()
  170. 170:     {
  171. 171:         if ($this->authenticationHandler === NULL{
  172. 172:             $this->authenticationHandler Environment::getService('Nette\Security\IAuthenticator');
  173. 173:         }
  174. 174:         return $this->authenticationHandler;
  175. 175:     }
  176. 176:  
  177. 177:  
  178. 178:  
  179. 179:     /**
  180. 180:      * Changes namespace; allows more users to share a session.
  181. 181:      * @param  string 
  182. 182:      * @return void 
  183. 183:      */
  184. 184:     public function setNamespace($namespace)
  185. 185:     {
  186. 186:         if ($this->namespace !== $namespace{
  187. 187:             $this->namespace = (string) $namespace;
  188. 188:             $this->session NULL;
  189. 189:         }
  190. 190:     }
  191. 191:  
  192. 192:  
  193. 193:  
  194. 194:     /**
  195. 195:      * Returns current namespace.
  196. 196:      * @return string 
  197. 197:      */
  198. 198:     final public function getNamespace()
  199. 199:     {
  200. 200:         return $this->namespace;
  201. 201:     }
  202. 202:  
  203. 203:  
  204. 204:  
  205. 205:     /**
  206. 206:      * Enables sign out after inactivity.
  207. 207:      * @param  mixed number of seconds or timestamp
  208. 208:      * @param  bool  sign out when the browser is closed?
  209. 209:      * @param  bool  clear the identity from persistent storage?
  210. 210:      * @return void 
  211. 211:      */
  212. 212:     public function setExpiration($seconds$whenBrowserIsClosed TRUE$clearIdentity FALSE)
  213. 213:     {
  214. 214:         if (is_string($seconds&& !is_numeric($seconds)) {
  215. 215:             $seconds strtotime($seconds);
  216. 216:         }
  217. 217:  
  218. 218:         $session $this->getSessionNamespace(TRUE);
  219. 219:         if ($seconds 0{
  220. 220:             if ($seconds <= Tools::YEAR{
  221. 221:                 $seconds += time();
  222. 222:             }
  223. 223:             $session->expireTime $seconds;
  224. 224:             $session->expireDelta $seconds time();
  225. 225:  
  226. 226:         else {
  227. 227:             unset($session->expireTime$session->expireDelta);
  228. 228:         }
  229. 229:  
  230. 230:         $session->expireIdentity = (bool) $clearIdentity;
  231. 231:         $session->expireBrowser = (bool) $whenBrowserIsClosed;
  232. 232:         $session->browserCheck TRUE;
  233. 233:         $session->setExpiration(0'browserCheck');
  234. 234:     }
  235. 235:  
  236. 236:  
  237. 237:  
  238. 238:     /**
  239. 239:      * Why was user signed out?
  240. 240:      * @return int 
  241. 241:      */
  242. 242:     final public function getSignOutReason()
  243. 243:     {
  244. 244:         $session $this->getSessionNamespace(FALSE);
  245. 245:         return $session $session->reason NULL;
  246. 246:     }
  247. 247:  
  248. 248:  
  249. 249:  
  250. 250:     /**
  251. 251:      * Returns and initializes $this->session.
  252. 252:      * @return SessionNamespace 
  253. 253:      */
  254. 254:     protected function getSessionNamespace($need)
  255. 255:     {
  256. 256:         if ($this->session !== NULL{
  257. 257:             return $this->session;
  258. 258:         }
  259. 259:  
  260. 260:         $sessionHandler $this->getSession();
  261. 261:         if (!$need && !$sessionHandler->exists()) {
  262. 262:             return NULL;
  263. 263:         }
  264. 264:  
  265. 265:         $this->session $session $sessionHandler->getNamespace('Nette.Web.User/' $this->namespace);
  266. 266:  
  267. 267:         if (!($session->identity instanceof IIdentity|| !is_bool($session->authenticated)) {
  268. 268:             $session->remove();
  269. 269:         }
  270. 270:  
  271. 271:         if ($session->authenticated && $session->expireBrowser && !$session->browserCheck// check if browser was closed?
  272. 272:             $session->reason self::BROWSER_CLOSED;
  273. 273:             $session->authenticated FALSE;
  274. 274:             $this->onSignedOut($this);
  275. 275:             if ($session->expireIdentity{
  276. 276:                 unset($session->identity);
  277. 277:             }
  278. 278:         }
  279. 279:  
  280. 280:         if ($session->authenticated && $session->expireDelta 0// check time expiration
  281. 281:             if ($session->expireTime time()) {
  282. 282:                 $session->reason self::INACTIVITY;
  283. 283:                 $session->authenticated FALSE;
  284. 284:                 $this->onSignedOut($this);
  285. 285:                 if ($session->expireIdentity{
  286. 286:                     unset($session->identity);
  287. 287:                 }
  288. 288:             }
  289. 289:             $session->expireTime time($session->expireDelta// sliding expiration
  290. 290:         }
  291. 291:  
  292. 292:         if (!$session->authenticated{
  293. 293:             unset($session->expireTime$session->expireDelta$session->expireIdentity,
  294. 294:                 $session->expireBrowser$session->browserCheck$session->authTime);
  295. 295:         }
  296. 296:  
  297. 297:         return $this->session;
  298. 298:     }
  299. 299:  
  300. 300:  
  301. 301:  
  302. 302:     /**
  303. 303:      * Set the authenticated status of this user.
  304. 304:      * @param  bool  flag indicating the authenticated status of user
  305. 305:      * @return void 
  306. 306:      */
  307. 307:     protected function setAuthenticated($state)
  308. 308:     {
  309. 309:         $session $this->getSessionNamespace(TRUE);
  310. 310:         $session->authenticated = (bool) $state;
  311. 311:  
  312. 312:         // Session Fixation defence
  313. 313:         $this->getSession()->regenerateId();
  314. 314:  
  315. 315:         if ($state{
  316. 316:             $session->reason NULL;
  317. 317:             $session->authTime time()// informative value
  318. 318:  
  319. 319:         else {
  320. 320:             $session->reason self::MANUAL;
  321. 321:             $session->authTime NULL;
  322. 322:         }
  323. 323:     }
  324. 324:  
  325. 325:  
  326. 326:  
  327. 327:     protected function setIdentity(IIdentity $identity NULL)
  328. 328:     {
  329. 329:         $this->getSessionNamespace(TRUE)->identity $identity;
  330. 330:     }
  331. 331:  
  332. 332:  
  333. 333:  
  334. 334:     /********************* Authorization ****************d*g**/
  335. 335:  
  336. 336:  
  337. 337:  
  338. 338:     /**
  339. 339:      * Returns a list of effective roles that a user has been granted.
  340. 340:      * @return array 
  341. 341:      */
  342. 342:     public function getRoles()
  343. 343:     {
  344. 344:         if (!$this->isAuthenticated()) {
  345. 345:             return array($this->guestRole);
  346. 346:         }
  347. 347:  
  348. 348:         $identity $this->getIdentity();
  349. 349:         return $identity $identity->getRoles(array($this->authenticatedRole);
  350. 350:     }
  351. 351:  
  352. 352:  
  353. 353:  
  354. 354:     /**
  355. 355:      * Is a user in the specified effective role?
  356. 356:      * @param  string 
  357. 357:      * @return bool 
  358. 358:      */
  359. 359:     final public function isInRole($role)
  360. 360:     {
  361. 361:         return in_array($role$this->getRoles()TRUE);
  362. 362:     }
  363. 363:  
  364. 364:  
  365. 365:  
  366. 366:     /**
  367. 367:      * Has a user effective access to the Resource?
  368. 368:      * If $resource is NULL, then the query applies to all resources.
  369. 369:      * @param  string  resource
  370. 370:      * @param  string  privilege
  371. 371:      * @return bool 
  372. 372:      */
  373. 373:     public function isAllowed($resource NULL$privilege NULL)
  374. 374:     {
  375. 375:         $handler $this->getAuthorizationHandler();
  376. 376:         if (!$handler{
  377. 377:             throw new InvalidStateException("Authorization handler has not been set.");
  378. 378:         }
  379. 379:  
  380. 380:         foreach ($this->getRoles(as $role{
  381. 381:             if ($handler->isAllowed($role$resource$privilege)) return TRUE;
  382. 382:         }
  383. 383:  
  384. 384:         return FALSE;
  385. 385:     }
  386. 386:  
  387. 387:  
  388. 388:  
  389. 389:     /**
  390. 390:      * Sets authorization handler.
  391. 391:      * @param  IAuthorizator 
  392. 392:      * @return void 
  393. 393:      */
  394. 394:     public function setAuthorizationHandler(IAuthorizator $handler)
  395. 395:     {
  396. 396:         $this->authorizationHandler $handler;
  397. 397:     }
  398. 398:  
  399. 399:  
  400. 400:  
  401. 401:     /**
  402. 402:      * Returns current authorization handler.
  403. 403:      * @return IAuthorizator 
  404. 404:      */
  405. 405:     final public function getAuthorizationHandler()
  406. 406:     {
  407. 407:         if ($this->authorizationHandler === NULL{
  408. 408:             $this->authorizationHandler Environment::getService('Nette\Security\IAuthorizator');
  409. 409:         }
  410. 410:         return $this->authorizationHandler;
  411. 411:     }
  412. 412:  
  413. 413:  
  414. 414:  
  415. 415:     /********************* backend ****************d*g**/
  416. 416:  
  417. 417:  
  418. 418:  
  419. 419:     /**
  420. 420:      * Returns session handler.
  421. 421:      * @return Session 
  422. 422:      */
  423. 423:     protected function getSession()
  424. 424:     {
  425. 425:         return Environment::getSession();
  426. 426:     }
  427. 427: