1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NUserStorage extends NObject implements IUserStorage
22: {
23:
24: private $namespace = '';
25:
26:
27: private $sessionHandler;
28:
29:
30: private $sessionSection;
31:
32:
33:
34: public function __construct(NSession $sessionHandler)
35: {
36: $this->sessionHandler = $sessionHandler;
37: }
38:
39:
40:
41: 42: 43: 44: 45:
46: public function setAuthenticated($state)
47: {
48: $section = $this->getSessionSection(TRUE);
49: $section->authenticated = (bool) $state;
50:
51:
52: $this->sessionHandler->regenerateId();
53:
54: if ($state) {
55: $section->reason = NULL;
56: $section->authTime = time();
57:
58: } else {
59: $section->reason = self::MANUAL;
60: $section->authTime = NULL;
61: }
62: return $this;
63: }
64:
65:
66:
67: 68: 69: 70:
71: public function isAuthenticated()
72: {
73: $session = $this->getSessionSection(FALSE);
74: return $session && $session->authenticated;
75: }
76:
77:
78:
79: 80: 81: 82: 83:
84: public function setIdentity(IIdentity $identity = NULL)
85: {
86: $this->getSessionSection(TRUE)->identity = $identity;
87: return $this;
88: }
89:
90:
91:
92: 93: 94: 95:
96: public function getIdentity()
97: {
98: $session = $this->getSessionSection(FALSE);
99: return $session ? $session->identity : NULL;
100: }
101:
102:
103:
104: 105: 106: 107: 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: 122: 123:
124: public function getNamespace()
125: {
126: return $this->namespace;
127: }
128:
129:
130:
131: 132: 133: 134: 135: 136:
137: public function setExpiration($time, $flags = 0)
138: {
139: $section = $this->getSessionSection(TRUE);
140: if ($time) {
141: $time = NDateTime53::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: return $this;
154: }
155:
156:
157:
158: 159: 160: 161:
162: public function getLogoutReason()
163: {
164: $session = $this->getSessionSection(FALSE);
165: return $session ? $session->reason : NULL;
166: }
167:
168:
169:
170: 171: 172: 173:
174: protected function getSessionSection($need)
175: {
176: if ($this->sessionSection !== NULL) {
177: return $this->sessionSection;
178: }
179:
180: if (!$need && !$this->sessionHandler->exists()) {
181: return NULL;
182: }
183:
184: $this->sessionSection = $section = $this->sessionHandler->getSection('Nette.Http.UserStorage/' . $this->namespace);
185:
186: if (!$section->identity instanceof IIdentity || !is_bool($section->authenticated)) {
187: $section->remove();
188: }
189:
190: if ($section->authenticated && $section->expireBrowser && !$section->browserCheck) {
191: $section->reason = self::BROWSER_CLOSED;
192: $section->authenticated = FALSE;
193: if ($section->expireIdentity) {
194: unset($section->identity);
195: }
196: }
197:
198: if ($section->authenticated && $section->expireDelta > 0) {
199: if ($section->expireTime < time()) {
200: $section->reason = self::INACTIVITY;
201: $section->authenticated = FALSE;
202: if ($section->expireIdentity) {
203: unset($section->identity);
204: }
205: }
206: $section->expireTime = time() + $section->expireDelta;
207: }
208:
209: if (!$section->authenticated) {
210: unset($section->expireTime, $section->expireDelta, $section->expireIdentity,
211: $section->expireBrowser, $section->browserCheck, $section->authTime);
212: }
213:
214: return $this->sessionSection;
215: }
216:
217: }
218: