1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class UserStorage extends Object implements IUserStorage
22: {
23:
24: private $namespace = '';
25:
26:
27: private $sessionHandler;
28:
29:
30: private $sessionSection;
31:
32:
33:
34: public function __construct(Session $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 = 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');
154: return $this;
155: }
156:
157:
158:
159: 160: 161: 162:
163: public function getLogoutReason()
164: {
165: $session = $this->getSessionSection(FALSE);
166: return $session ? $session->reason : NULL;
167: }
168:
169:
170:
171: 172: 173: 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) {
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) {
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;
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: