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