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: public function setIdentity(IIdentity $identity = NULL)
87: {
88: $this->getSessionSection(TRUE)->identity = $identity;
89: return $this;
90: }
91:
92:
93:
94: 95: 96: 97:
98: public function getIdentity()
99: {
100: $session = $this->getSessionSection(FALSE);
101: return $session ? $session->identity : NULL;
102: }
103:
104:
105:
106: 107: 108: 109: 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: 124: 125:
126: public function getNamespace()
127: {
128: return $this->namespace;
129: }
130:
131:
132:
133: 134: 135: 136: 137: 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');
156: return $this;
157: }
158:
159:
160:
161: 162: 163: 164:
165: public function getLogoutReason()
166: {
167: $session = $this->getSessionSection(FALSE);
168: return $session ? $session->reason : NULL;
169: }
170:
171:
172:
173: 174: 175: 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) {
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) {
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;
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: