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