Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • Context
  • FileUpload
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • User

Interfaces

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