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: class UserStorage extends Nette\Object implements Nette\Security\IUserStorage
24: {
25:
26: private $namespace = '';
27:
28:
29: private $sessionHandler;
30:
31:
32: private $sessionSection;
33:
34:
35: public function __construct(Session $sessionHandler)
36: {
37: $this->sessionHandler = $sessionHandler;
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: public function isAuthenticated()
71: {
72: $session = $this->getSessionSection(FALSE);
73: return $session && $session->authenticated;
74: }
75:
76:
77: 78: 79: 80:
81: public function setIdentity(IIdentity $identity = NULL)
82: {
83: $this->getSessionSection(TRUE)->identity = $identity;
84: return $this;
85: }
86:
87:
88: 89: 90: 91:
92: public function getIdentity()
93: {
94: $session = $this->getSessionSection(FALSE);
95: return $session ? $session->identity : NULL;
96: }
97:
98:
99: 100: 101: 102: 103:
104: public function setNamespace($namespace)
105: {
106: if ($this->namespace !== $namespace) {
107: $this->namespace = (string) $namespace;
108: $this->sessionSection = NULL;
109: }
110: return $this;
111: }
112:
113:
114: 115: 116: 117:
118: public function getNamespace()
119: {
120: return $this->namespace;
121: }
122:
123:
124: 125: 126: 127: 128: 129:
130: public function setExpiration($time, $flags = 0)
131: {
132: $section = $this->getSessionSection(TRUE);
133: if ($time) {
134: $time = Nette\DateTime::from($time)->format('U');
135: $section->expireTime = $time;
136: $section->expireDelta = $time - time();
137:
138: } else {
139: unset($section->expireTime, $section->expireDelta);
140: }
141:
142: $section->expireIdentity = (bool) ($flags & self::CLEAR_IDENTITY);
143: $section->expireBrowser = (bool) ($flags & self::BROWSER_CLOSED);
144: $section->browserCheck = TRUE;
145: $section->setExpiration(0, 'browserCheck');
146: $section->setExpiration($time, 'foo');
147: return $this;
148: }
149:
150:
151: 152: 153: 154:
155: public function getLogoutReason()
156: {
157: $session = $this->getSessionSection(FALSE);
158: return $session ? $session->reason : NULL;
159: }
160:
161:
162: 163: 164: 165:
166: protected function getSessionSection($need)
167: {
168: if ($this->sessionSection !== NULL) {
169: return $this->sessionSection;
170: }
171:
172: if (!$need && !$this->sessionHandler->exists()) {
173: return NULL;
174: }
175:
176: $this->sessionSection = $section = $this->sessionHandler->getSection('Nette.Http.UserStorage/' . $this->namespace);
177:
178: if (!$section->identity instanceof IIdentity || !is_bool($section->authenticated)) {
179: $section->remove();
180: }
181:
182: if ($section->authenticated && $section->expireBrowser && !$section->browserCheck) {
183: $section->reason = self::BROWSER_CLOSED;
184: $section->authenticated = FALSE;
185: if ($section->expireIdentity) {
186: unset($section->identity);
187: }
188: }
189:
190: if ($section->authenticated && $section->expireDelta > 0) {
191: if ($section->expireTime < time()) {
192: $section->reason = self::INACTIVITY;
193: $section->authenticated = FALSE;
194: if ($section->expireIdentity) {
195: unset($section->identity);
196: }
197: }
198: $section->expireTime = time() + $section->expireDelta;
199: }
200:
201: if (!$section->authenticated) {
202: unset($section->expireTime, $section->expireDelta, $section->expireIdentity,
203: $section->expireBrowser, $section->browserCheck, $section->authTime);
204: }
205:
206: return $this->sessionSection;
207: }
208:
209: }
210: