1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Http;
13:
14: use Nette,
15: Nette\Security\IIdentity;
16:
17:
18:
19: 20: 21: 22: 23:
24: class UserStorage extends Nette\Object implements Nette\Security\IUserStorage
25: {
26:
27: private $namespace = '';
28:
29:
30: private $sessionHandler;
31:
32:
33: private $sessionSection;
34:
35:
36:
37: public function __construct(Session $sessionHandler)
38: {
39: $this->sessionHandler = $sessionHandler;
40: }
41:
42:
43:
44: 45: 46: 47: 48:
49: public function setAuthenticated($state)
50: {
51: $section = $this->getSessionSection(TRUE);
52: $section->authenticated = (bool) $state;
53:
54:
55: $this->sessionHandler->regenerateId();
56:
57: if ($state) {
58: $section->reason = NULL;
59: $section->authTime = time();
60:
61: } else {
62: $section->reason = self::MANUAL;
63: $section->authTime = NULL;
64: }
65: return $this;
66: }
67:
68:
69:
70: 71: 72: 73:
74: public function isAuthenticated()
75: {
76: $session = $this->getSessionSection(FALSE);
77: return $session && $session->authenticated;
78: }
79:
80:
81:
82: 83: 84: 85: 86:
87: public function setIdentity(IIdentity $identity = NULL)
88: {
89: $this->getSessionSection(TRUE)->identity = $identity;
90: return $this;
91: }
92:
93:
94:
95: 96: 97: 98:
99: public function getIdentity()
100: {
101: $session = $this->getSessionSection(FALSE);
102: return $session ? $session->identity : NULL;
103: }
104:
105:
106:
107: 108: 109: 110: 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: 125: 126:
127: public function getNamespace()
128: {
129: return $this->namespace;
130: }
131:
132:
133:
134: 135: 136: 137: 138: 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');
157: return $this;
158: }
159:
160:
161:
162: 163: 164: 165:
166: public function getLogoutReason()
167: {
168: $session = $this->getSessionSection(FALSE);
169: return $session ? $session->reason : NULL;
170: }
171:
172:
173:
174: 175: 176: 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) {
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) {
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;
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: