Packages

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

Classes

  • Identity
  • Permission
  • SimpleAuthenticator
  • User

Interfaces

  • IAuthenticator
  • IAuthorizator
  • IIdentity
  • IResource
  • IRole
  • IUserStorage

Exceptions

  • AuthenticationException
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 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:  * @package Nette\Security
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * User authentication and authorization.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read bool $loggedIn
 21:  * @property-read IIdentity $identity
 22:  * @property-read mixed $id
 23:  * @property   IAuthenticator $authenticator
 24:  * @property-read int $logoutReason
 25:  * @property-read array $roles
 26:  * @property   IAuthorizator $authorizator
 27:  * @package Nette\Security
 28:  */
 29: class User extends Object
 30: {
 31:     /** @var string  default role for unauthenticated user */
 32:     public $guestRole = 'guest';
 33: 
 34:     /** @var string  default role for authenticated user without own identity */
 35:     public $authenticatedRole = 'authenticated';
 36: 
 37:     /** @var array of function(User $sender); Occurs when the user is successfully logged in */
 38:     public $onLoggedIn;
 39: 
 40:     /** @var array of function(User $sender); Occurs when the user is logged out */
 41:     public $onLoggedOut;
 42: 
 43:     /** @var IUserStorage Session storage for current user */
 44:     private $storage;
 45: 
 46:     /** @var IAuthenticator */
 47:     private $authenticator;
 48: 
 49:     /** @var IAuthorizator */
 50:     private $authorizator;
 51: 
 52:     /** @var IDIContainer */
 53:     private $context;
 54: 
 55: 
 56: 
 57:     public function __construct(IUserStorage $storage, IDIContainer $context)
 58:     {
 59:         $this->storage = $storage;
 60:         $this->context = $context; // with IAuthenticator, IAuthorizator
 61:     }
 62: 
 63: 
 64: 
 65:     /**
 66:      * @return IUserStorage
 67:      */
 68:     final public function getStorage()
 69:     {
 70:         return $this->storage;
 71:     }
 72: 
 73: 
 74: 
 75:     /********************* Authentication ****************d*g**/
 76: 
 77: 
 78: 
 79:     /**
 80:      * Conducts the authentication process. Parameters are optional.
 81:      * @param  mixed optional parameter (e.g. username or IIdentity)
 82:      * @param  mixed optional parameter (e.g. password)
 83:      * @return void
 84:      * @throws AuthenticationException if authentication was not successful
 85:      */
 86:     public function login($id = NULL, $password = NULL)
 87:     {
 88:         $this->logout(TRUE);
 89:         if (!$id instanceof IIdentity) {
 90:             $credentials = func_get_args();
 91:             $id = $this->getAuthenticator()->authenticate($credentials);
 92:         }
 93:         $this->storage->setIdentity($id);
 94:         $this->storage->setAuthenticated(TRUE);
 95:         $this->onLoggedIn($this);
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * Logs out the user from the current session.
102:      * @param  bool  clear the identity from persistent storage?
103:      * @return void
104:      */
105:     final public function logout($clearIdentity = FALSE)
106:     {
107:         if ($clearIdentity) {
108:             $this->storage->setIdentity(NULL);
109:         }
110:         if ($this->isLoggedIn()) {
111:             $this->storage->setAuthenticated(FALSE);
112:             $this->onLoggedOut($this);
113:         }
114:     }
115: 
116: 
117: 
118:     /**
119:      * Is this user authenticated?
120:      * @return bool
121:      */
122:     final public function isLoggedIn()
123:     {
124:         return $this->storage->isAuthenticated();
125:     }
126: 
127: 
128: 
129:     /**
130:      * Returns current user identity, if any.
131:      * @return IIdentity|NULL
132:      */
133:     final public function getIdentity()
134:     {
135:         return $this->storage->getIdentity();
136:     }
137: 
138: 
139: 
140:     /**
141:      * Returns current user ID, if any.
142:      * @return mixed
143:      */
144:     public function getId()
145:     {
146:         $identity = $this->getIdentity();
147:         return $identity ? $identity->getId() : NULL;
148:     }
149: 
150: 
151: 
152:     /**
153:      * Sets authentication handler.
154:      * @param  IAuthenticator
155:      * @return User  provides a fluent interface
156:      */
157:     public function setAuthenticator(IAuthenticator $handler)
158:     {
159:         $this->authenticator = $handler;
160:         return $this;
161:     }
162: 
163: 
164: 
165:     /**
166:      * Returns authentication handler.
167:      * @return IAuthenticator
168:      */
169:     final public function getAuthenticator()
170:     {
171:         return ($tmp=$this->authenticator) ? $tmp : $this->context->getByType('IAuthenticator');
172:     }
173: 
174: 
175: 
176:     /**
177:      * Enables log out after inactivity.
178:      * @param  string|int|DateTime number of seconds or timestamp
179:      * @param  bool  log out when the browser is closed?
180:      * @param  bool  clear the identity from persistent storage?
181:      * @return User  provides a fluent interface
182:      */
183:     public function setExpiration($time, $whenBrowserIsClosed = TRUE, $clearIdentity = FALSE)
184:     {
185:         $flags = ($whenBrowserIsClosed ? IUserStorage::BROWSER_CLOSED : 0) | ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
186:         $this->storage->setExpiration($time, $flags);
187:         return $this;
188:     }
189: 
190: 
191: 
192:     /**
193:      * Why was user logged out?
194:      * @return int
195:      */
196:     final public function getLogoutReason()
197:     {
198:         return $this->storage->getLogoutReason();
199:     }
200: 
201: 
202: 
203:     /********************* Authorization ****************d*g**/
204: 
205: 
206: 
207:     /**
208:      * Returns a list of effective roles that a user has been granted.
209:      * @return array
210:      */
211:     public function getRoles()
212:     {
213:         if (!$this->isLoggedIn()) {
214:             return array($this->guestRole);
215:         }
216: 
217:         $identity = $this->getIdentity();
218:         return $identity && $identity->getRoles() ? $identity->getRoles() : array($this->authenticatedRole);
219:     }
220: 
221: 
222: 
223:     /**
224:      * Is a user in the specified effective role?
225:      * @param  string
226:      * @return bool
227:      */
228:     final public function isInRole($role)
229:     {
230:         return in_array($role, $this->getRoles(), TRUE);
231:     }
232: 
233: 
234: 
235:     /**
236:      * Has a user effective access to the Resource?
237:      * If $resource is NULL, then the query applies to all resources.
238:      * @param  string  resource
239:      * @param  string  privilege
240:      * @return bool
241:      */
242:     public function isAllowed($resource = IAuthorizator::ALL, $privilege = IAuthorizator::ALL)
243:     {
244:         $authorizator = $this->getAuthorizator();
245:         foreach ($this->getRoles() as $role) {
246:             if ($authorizator->isAllowed($role, $resource, $privilege)) {
247:                 return TRUE;
248:             }
249:         }
250: 
251:         return FALSE;
252:     }
253: 
254: 
255: 
256:     /**
257:      * Sets authorization handler.
258:      * @param  IAuthorizator
259:      * @return User  provides a fluent interface
260:      */
261:     public function setAuthorizator(IAuthorizator $handler)
262:     {
263:         $this->authorizator = $handler;
264:         return $this;
265:     }
266: 
267: 
268: 
269:     /**
270:      * Returns current authorization handler.
271:      * @return IAuthorizator
272:      */
273:     final public function getAuthorizator()
274:     {
275:         return ($tmp=$this->authorizator) ? $tmp : $this->context->getByType('IAuthorizator');
276:     }
277: 
278: 
279: 
280:     /********************* deprecated ****************d*g**/
281: 
282:     /** @deprecated */
283:     function setNamespace($namespace)
284:     {
285:         trigger_error(__METHOD__ . '() is deprecated; use getStorage()->setNamespace() instead.', E_USER_WARNING);
286:         $this->storage->setNamespace($namespace);
287:         return $this;
288:     }
289: 
290:     /** @deprecated */
291:     function getNamespace()
292:     {
293:         trigger_error(__METHOD__ . '() is deprecated; use getStorage()->getNamespace() instead.', E_USER_WARNING);
294:         return $this->storage->getNamespace();
295:     }
296: 
297:     /** @deprecated */
298:     function setAuthenticationHandler($v)
299:     {
300:         trigger_error(__METHOD__ . '() is deprecated; use setAuthenticator() instead.', E_USER_WARNING);
301:         return $this->setAuthenticator($v);
302:     }
303: 
304:     /** @deprecated */
305:     function setAuthorizationHandler($v)
306:     {
307:         trigger_error(__METHOD__ . '() is deprecated; use setAuthorizator() instead.', E_USER_WARNING);
308:         return $this->setAuthorizator($v);
309:     }
310: 
311: }
312: 
Nette Framework 2.0.1 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0