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

  • HttpContext
  • HttpRequest
  • HttpRequestFactory
  • HttpResponse
  • HttpUploadedFile
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

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