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