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 User  provides a fluent interface
  157. 157:      */
  158. 158:     public function setAuthenticationHandler(IAuthenticator $handler)
  159. 159:     {
  160. 160:         $this->authenticationHandler $handler;
  161. 161:         return $this;
  162. 162:     }
  163. 163:  
  164. 164:  
  165. 165:  
  166. 166:     /**
  167. 167:      * Returns authentication handler.
  168. 168:      * @return IAuthenticator 
  169. 169:      */
  170. 170:     final public function getAuthenticationHandler()
  171. 171:     {
  172. 172:         if ($this->authenticationHandler === NULL{
  173. 173:             $this->authenticationHandler Environment::getService('Nette\Security\IAuthenticator');
  174. 174:         }
  175. 175:         return $this->authenticationHandler;
  176. 176:     }
  177. 177:  
  178. 178:  
  179. 179:  
  180. 180:     /**
  181. 181:      * Changes namespace; allows more users to share a session.
  182. 182:      * @param  string 
  183. 183:      * @return User  provides a fluent interface
  184. 184:      */
  185. 185:     public function setNamespace($namespace)
  186. 186:     {
  187. 187:         if ($this->namespace !== $namespace{
  188. 188:             $this->namespace = (string) $namespace;
  189. 189:             $this->session NULL;
  190. 190:         }
  191. 191:         return $this;
  192. 192:     }
  193. 193:  
  194. 194:  
  195. 195:  
  196. 196:     /**
  197. 197:      * Returns current namespace.
  198. 198:      * @return string 
  199. 199:      */
  200. 200:     final public function getNamespace()
  201. 201:     {
  202. 202:         return $this->namespace;
  203. 203:     }
  204. 204:  
  205. 205:  
  206. 206:  
  207. 207:     /**
  208. 208:      * Enables sign out after inactivity.
  209. 209:      * @param  mixed number of seconds or timestamp
  210. 210:      * @param  bool  sign out when the browser is closed?
  211. 211:      * @param  bool  clear the identity from persistent storage?
  212. 212:      * @return User  provides a fluent interface
  213. 213:      */
  214. 214:     public function setExpiration($seconds$whenBrowserIsClosed TRUE$clearIdentity FALSE)
  215. 215:     {
  216. 216:         if (is_string($seconds&& !is_numeric($seconds)) {
  217. 217:             $seconds strtotime($seconds);
  218. 218:         }
  219. 219:  
  220. 220:         $session $this->getSessionNamespace(TRUE);
  221. 221:         if ($seconds 0{
  222. 222:             if ($seconds <= Tools::YEAR{
  223. 223:                 $seconds += time();
  224. 224:             }
  225. 225:             $session->expireTime $seconds;
  226. 226:             $session->expireDelta $seconds time();
  227. 227:  
  228. 228:         else {
  229. 229:             unset($session->expireTime$session->expireDelta);
  230. 230:         }
  231. 231:  
  232. 232:         $session->expireIdentity = (bool) $clearIdentity;
  233. 233:         $session->expireBrowser = (bool) $whenBrowserIsClosed;
  234. 234:         $session->browserCheck TRUE;
  235. 235:         $session->setExpiration(0'browserCheck');
  236. 236:         return $this;
  237. 237:     }
  238. 238:  
  239. 239:  
  240. 240:  
  241. 241:     /**
  242. 242:      * Why was user signed out?
  243. 243:      * @return int 
  244. 244:      */
  245. 245:     final public function getSignOutReason()
  246. 246:     {
  247. 247:         $session $this->getSessionNamespace(FALSE);
  248. 248:         return $session $session->reason NULL;
  249. 249:     }
  250. 250:  
  251. 251:  
  252. 252:  
  253. 253:     /**
  254. 254:      * Returns and initializes $this->session.
  255. 255:      * @return SessionNamespace 
  256. 256:      */
  257. 257:     protected function getSessionNamespace($need)
  258. 258:     {
  259. 259:         if ($this->session !== NULL{
  260. 260:             return $this->session;
  261. 261:         }
  262. 262:  
  263. 263:         $sessionHandler $this->getSession();
  264. 264:         if (!$need && !$sessionHandler->exists()) {
  265. 265:             return NULL;
  266. 266:         }
  267. 267:  
  268. 268:         $this->session $session $sessionHandler->getNamespace('Nette.Web.User/' $this->namespace);
  269. 269:  
  270. 270:         if (!($session->identity instanceof IIdentity|| !is_bool($session->authenticated)) {
  271. 271:             $session->remove();
  272. 272:         }
  273. 273:  
  274. 274:         if ($session->authenticated && $session->expireBrowser && !$session->browserCheck// check if browser was closed?
  275. 275:             $session->reason self::BROWSER_CLOSED;
  276. 276:             $session->authenticated FALSE;
  277. 277:             $this->onSignedOut($this);
  278. 278:             if ($session->expireIdentity{
  279. 279:                 unset($session->identity);
  280. 280:             }
  281. 281:         }
  282. 282:  
  283. 283:         if ($session->authenticated && $session->expireDelta 0// check time expiration
  284. 284:             if ($session->expireTime time()) {
  285. 285:                 $session->reason self::INACTIVITY;
  286. 286:                 $session->authenticated FALSE;
  287. 287:                 $this->onSignedOut($this);
  288. 288:                 if ($session->expireIdentity{
  289. 289:                     unset($session->identity);
  290. 290:                 }
  291. 291:             }
  292. 292:             $session->expireTime time($session->expireDelta// sliding expiration
  293. 293:         }
  294. 294:  
  295. 295:         if (!$session->authenticated{
  296. 296:             unset($session->expireTime$session->expireDelta$session->expireIdentity,
  297. 297:                 $session->expireBrowser$session->browserCheck$session->authTime);
  298. 298:         }
  299. 299:  
  300. 300:         return $this->session;
  301. 301:     }
  302. 302:  
  303. 303:  
  304. 304:  
  305. 305:     /**
  306. 306:      * Sets the authenticated status of this user.
  307. 307:      * @param  bool  flag indicating the authenticated status of user
  308. 308:      * @return User  provides a fluent interface
  309. 309:      */
  310. 310:     protected function setAuthenticated($state)
  311. 311:     {
  312. 312:         $session $this->getSessionNamespace(TRUE);
  313. 313:         $session->authenticated = (bool) $state;
  314. 314:  
  315. 315:         // Session Fixation defence
  316. 316:         $this->getSession()->regenerateId();
  317. 317:  
  318. 318:         if ($state{
  319. 319:             $session->reason NULL;
  320. 320:             $session->authTime time()// informative value
  321. 321:  
  322. 322:         else {
  323. 323:             $session->reason self::MANUAL;
  324. 324:             $session->authTime NULL;
  325. 325:         }
  326. 326:         return $this;
  327. 327:     }
  328. 328:  
  329. 329:  
  330. 330:  
  331. 331:     /**
  332. 332:      * Sets the user identity.
  333. 333:      * @param  IIdentity 
  334. 334:      * @return User  provides a fluent interface
  335. 335:      */
  336. 336:     protected function setIdentity(IIdentity $identity NULL)
  337. 337:     {
  338. 338:         $this->getSessionNamespace(TRUE)->identity $identity;
  339. 339:         return $this;
  340. 340:     }
  341. 341:  
  342. 342:  
  343. 343:  
  344. 344:     /********************* Authorization ****************d*g**/
  345. 345:  
  346. 346:  
  347. 347:  
  348. 348:     /**
  349. 349:      * Returns a list of effective roles that a user has been granted.
  350. 350:      * @return array 
  351. 351:      */
  352. 352:     public function getRoles()
  353. 353:     {
  354. 354:         if (!$this->isAuthenticated()) {
  355. 355:             return array($this->guestRole);
  356. 356:         }
  357. 357:  
  358. 358:         $identity $this->getIdentity();
  359. 359:         return $identity $identity->getRoles(array($this->authenticatedRole);
  360. 360:     }
  361. 361:  
  362. 362:  
  363. 363:  
  364. 364:     /**
  365. 365:      * Is a user in the specified effective role?
  366. 366:      * @param  string 
  367. 367:      * @return bool 
  368. 368:      */
  369. 369:     final public function isInRole($role)
  370. 370:     {
  371. 371:         return in_array($role$this->getRoles()TRUE);
  372. 372:     }
  373. 373:  
  374. 374:  
  375. 375:  
  376. 376:     /**
  377. 377:      * Has a user effective access to the Resource?
  378. 378:      * If $resource is NULL, then the query applies to all resources.
  379. 379:      * @param  string  resource
  380. 380:      * @param  string  privilege
  381. 381:      * @return bool 
  382. 382:      */
  383. 383:     public function isAllowed($resource NULL$privilege NULL)
  384. 384:     {
  385. 385:         $handler $this->getAuthorizationHandler();
  386. 386:         if (!$handler{
  387. 387:             throw new InvalidStateException("Authorization handler has not been set.");
  388. 388:         }
  389. 389:  
  390. 390:         foreach ($this->getRoles(as $role{
  391. 391:             if ($handler->isAllowed($role$resource$privilege)) return TRUE;
  392. 392:         }
  393. 393:  
  394. 394:         return FALSE;
  395. 395:     }
  396. 396:  
  397. 397:  
  398. 398:  
  399. 399:     /**
  400. 400:      * Sets authorization handler.
  401. 401:      * @param  IAuthorizator 
  402. 402:      * @return User  provides a fluent interface
  403. 403:      */
  404. 404:     public function setAuthorizationHandler(IAuthorizator $handler)
  405. 405:     {
  406. 406:         $this->authorizationHandler $handler;
  407. 407:         return $this;
  408. 408:     }
  409. 409:  
  410. 410:  
  411. 411:  
  412. 412:     /**
  413. 413:      * Returns current authorization handler.
  414. 414:      * @return IAuthorizator 
  415. 415:      */
  416. 416:     final public function getAuthorizationHandler()
  417. 417:     {
  418. 418:         if ($this->authorizationHandler === NULL{
  419. 419:             $this->authorizationHandler Environment::getService('Nette\Security\IAuthorizator');
  420. 420:         }
  421. 421:         return $this->authorizationHandler;
  422. 422:     }
  423. 423:  
  424. 424:  
  425. 425:  
  426. 426:     /********************* backend ****************d*g**/
  427. 427:  
  428. 428:  
  429. 429:  
  430. 430:     /**
  431. 431:      * Returns session handler.
  432. 432:      * @return Session 
  433. 433:      */
  434. 434:     protected function getSession()
  435. 435:     {
  436. 436:         return Environment::getSession();
  437. 437:     }
  438. 438: