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
  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:      * @param  IIdentity
 85:      * @return UserStorage Provides a fluent interface
 86:      */
 87:     public function setIdentity(IIdentity $identity = NULL)
 88:     {
 89:         $this->getSessionSection(TRUE)->identity = $identity;
 90:         return $this;
 91:     }
 92: 
 93: 
 94: 
 95:     /**
 96:      * Returns current user identity, if any.
 97:      * @return Nette\Security\IIdentity|NULL
 98:      */
 99:     public function getIdentity()
100:     {
101:         $session = $this->getSessionSection(FALSE);
102:         return $session ? $session->identity : NULL;
103:     }
104: 
105: 
106: 
107:     /**
108:      * Changes namespace; allows more users to share a session.
109:      * @param  string
110:      * @return UserStorage Provides a fluent interface
111:      */
112:     public function setNamespace($namespace)
113:     {
114:         if ($this->namespace !== $namespace) {
115:             $this->namespace = (string) $namespace;
116:             $this->sessionSection = NULL;
117:         }
118:         return $this;
119:     }
120: 
121: 
122: 
123:     /**
124:      * Returns current namespace.
125:      * @return string
126:      */
127:     public function getNamespace()
128:     {
129:         return $this->namespace;
130:     }
131: 
132: 
133: 
134:     /**
135:      * Enables log out after inactivity.
136:      * @param  string|int|DateTime Number of seconds or timestamp
137:      * @param  int Log out when the browser is closed | Clear the identity from persistent storage?
138:      * @return UserStorage Provides a fluent interface
139:      */
140:     public function setExpiration($time, $flags = 0)
141:     {
142:         $section = $this->getSessionSection(TRUE);
143:         if ($time) {
144:             $time = Nette\DateTime::from($time)->format('U');
145:             $section->expireTime = $time;
146:             $section->expireDelta = $time - time();
147: 
148:         } else {
149:             unset($section->expireTime, $section->expireDelta);
150:         }
151: 
152:         $section->expireIdentity = (bool) ($flags & self::CLEAR_IDENTITY);
153:         $section->expireBrowser = (bool) ($flags & self::BROWSER_CLOSED);
154:         $section->browserCheck = TRUE;
155:         $section->setExpiration(0, 'browserCheck');
156:         $section->setExpiration($time, 'foo'); // time check
157:         return $this;
158:     }
159: 
160: 
161: 
162:     /**
163:      * Why was user logged out?
164:      * @return int
165:      */
166:     public function getLogoutReason()
167:     {
168:         $session = $this->getSessionSection(FALSE);
169:         return $session ? $session->reason : NULL;
170:     }
171: 
172: 
173: 
174:     /**
175:      * Returns and initializes $this->sessionSection.
176:      * @return SessionSection
177:      */
178:     protected function getSessionSection($need)
179:     {
180:         if ($this->sessionSection !== NULL) {
181:             return $this->sessionSection;
182:         }
183: 
184:         if (!$need && !$this->sessionHandler->exists()) {
185:             return NULL;
186:         }
187: 
188:         $this->sessionSection = $section = $this->sessionHandler->getSection('Nette.Http.UserStorage/' . $this->namespace);
189: 
190:         if (!$section->identity instanceof IIdentity || !is_bool($section->authenticated)) {
191:             $section->remove();
192:         }
193: 
194:         if ($section->authenticated && $section->expireBrowser && !$section->browserCheck) { // check if browser was closed?
195:             $section->reason = self::BROWSER_CLOSED;
196:             $section->authenticated = FALSE;
197:             if ($section->expireIdentity) {
198:                 unset($section->identity);
199:             }
200:         }
201: 
202:         if ($section->authenticated && $section->expireDelta > 0) { // check time expiration
203:             if ($section->expireTime < time()) {
204:                 $section->reason = self::INACTIVITY;
205:                 $section->authenticated = FALSE;
206:                 if ($section->expireIdentity) {
207:                     unset($section->identity);
208:                 }
209:             }
210:             $section->expireTime = time() + $section->expireDelta; // sliding expiration
211:         }
212: 
213:         if (!$section->authenticated) {
214:             unset($section->expireTime, $section->expireDelta, $section->expireIdentity,
215:                 $section->expireBrowser, $section->browserCheck, $section->authTime);
216:         }
217: 
218:         return $this->sessionSection;
219:     }
220: 
221: }
222: 
Nette Framework 2.0.4 API API documentation generated by ApiGen 2.7.0