Namespaces

  • 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

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

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  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:  */
 11: 
 12: namespace Nette\Http;
 13: 
 14: use Nette,
 15:     Nette\Security\IIdentity;
 16: 
 17: 
 18: /**
 19:  * Session storage for user object.
 20:  *
 21:  * @author David Grudl, Jan Tichý
 22:  */
 23: class UserStorage extends Nette\Object implements Nette\Security\IUserStorage
 24: {
 25:     /** @var string */
 26:     private $namespace = '';
 27: 
 28:     /** @var Session */
 29:     private $sessionHandler;
 30: 
 31:     /** @var SessionSection */
 32:     private $sessionSection;
 33: 
 34: 
 35:     public function  __construct(Session $sessionHandler)
 36:     {
 37:         $this->sessionHandler = $sessionHandler;
 38:     }
 39: 
 40: 
 41:     /**
 42:      * Sets the authenticated status of this user.
 43:      * @param  bool
 44:      * @return self
 45:      */
 46:     public function setAuthenticated($state)
 47:     {
 48:         $section = $this->getSessionSection(TRUE);
 49:         $section->authenticated = (bool) $state;
 50: 
 51:         // Session Fixation defence
 52:         $this->sessionHandler->regenerateId();
 53: 
 54:         if ($state) {
 55:             $section->reason = NULL;
 56:             $section->authTime = time(); // informative value
 57: 
 58:         } else {
 59:             $section->reason = self::MANUAL;
 60:             $section->authTime = NULL;
 61:         }
 62:         return $this;
 63:     }
 64: 
 65: 
 66:     /**
 67:      * Is this user authenticated?
 68:      * @return bool
 69:      */
 70:     public function isAuthenticated()
 71:     {
 72:         $session = $this->getSessionSection(FALSE);
 73:         return $session && $session->authenticated;
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Sets the user identity.
 79:      * @return self
 80:      */
 81:     public function setIdentity(IIdentity $identity = NULL)
 82:     {
 83:         $this->getSessionSection(TRUE)->identity = $identity;
 84:         return $this;
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Returns current user identity, if any.
 90:      * @return Nette\Security\IIdentity|NULL
 91:      */
 92:     public function getIdentity()
 93:     {
 94:         $session = $this->getSessionSection(FALSE);
 95:         return $session ? $session->identity : NULL;
 96:     }
 97: 
 98: 
 99:     /**
100:      * Changes namespace; allows more users to share a session.
101:      * @param  string
102:      * @return self
103:      */
104:     public function setNamespace($namespace)
105:     {
106:         if ($this->namespace !== $namespace) {
107:             $this->namespace = (string) $namespace;
108:             $this->sessionSection = NULL;
109:         }
110:         return $this;
111:     }
112: 
113: 
114:     /**
115:      * Returns current namespace.
116:      * @return string
117:      */
118:     public function getNamespace()
119:     {
120:         return $this->namespace;
121:     }
122: 
123: 
124:     /**
125:      * Enables log out after inactivity.
126:      * @param  string|int|DateTime Number of seconds or timestamp
127:      * @param  int Log out when the browser is closed | Clear the identity from persistent storage?
128:      * @return self
129:      */
130:     public function setExpiration($time, $flags = 0)
131:     {
132:         $section = $this->getSessionSection(TRUE);
133:         if ($time) {
134:             $time = Nette\DateTime::from($time)->format('U');
135:             $section->expireTime = $time;
136:             $section->expireDelta = $time - time();
137: 
138:         } else {
139:             unset($section->expireTime, $section->expireDelta);
140:         }
141: 
142:         $section->expireIdentity = (bool) ($flags & self::CLEAR_IDENTITY);
143:         $section->expireBrowser = (bool) ($flags & self::BROWSER_CLOSED);
144:         $section->browserCheck = TRUE;
145:         $section->setExpiration(0, 'browserCheck');
146:         $section->setExpiration($time, 'foo'); // time check
147:         return $this;
148:     }
149: 
150: 
151:     /**
152:      * Why was user logged out?
153:      * @return int
154:      */
155:     public function getLogoutReason()
156:     {
157:         $session = $this->getSessionSection(FALSE);
158:         return $session ? $session->reason : NULL;
159:     }
160: 
161: 
162:     /**
163:      * Returns and initializes $this->sessionSection.
164:      * @return SessionSection
165:      */
166:     protected function getSessionSection($need)
167:     {
168:         if ($this->sessionSection !== NULL) {
169:             return $this->sessionSection;
170:         }
171: 
172:         if (!$need && !$this->sessionHandler->exists()) {
173:             return NULL;
174:         }
175: 
176:         $this->sessionSection = $section = $this->sessionHandler->getSection('Nette.Http.UserStorage/' . $this->namespace);
177: 
178:         if (!$section->identity instanceof IIdentity || !is_bool($section->authenticated)) {
179:             $section->remove();
180:         }
181: 
182:         if ($section->authenticated && $section->expireBrowser && !$section->browserCheck) { // check if browser was closed?
183:             $section->reason = self::BROWSER_CLOSED;
184:             $section->authenticated = FALSE;
185:             if ($section->expireIdentity) {
186:                 unset($section->identity);
187:             }
188:         }
189: 
190:         if ($section->authenticated && $section->expireDelta > 0) { // check time expiration
191:             if ($section->expireTime < time()) {
192:                 $section->reason = self::INACTIVITY;
193:                 $section->authenticated = FALSE;
194:                 if ($section->expireIdentity) {
195:                     unset($section->identity);
196:                 }
197:             }
198:             $section->expireTime = time() + $section->expireDelta; // sliding expiration
199:         }
200: 
201:         if (!$section->authenticated) {
202:             unset($section->expireTime, $section->expireDelta, $section->expireIdentity,
203:                 $section->expireBrowser, $section->browserCheck, $section->authTime);
204:         }
205: 
206:         return $this->sessionSection;
207:     }
208: 
209: }
210: 
Nette Framework 2.0.12 API API documentation generated by ApiGen 2.8.0