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