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