1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class NUser extends NObject implements IUser
28: {
29:
30: const MANUAL = 1;
31: const INACTIVITY = 2;
32: const BROWSER_CLOSED = 3;
33:
34:
35:
36: public $guestRole = 'guest';
37:
38:
39: public $authenticatedRole = 'authenticated';
40:
41:
42: public $onLoggedIn;
43:
44:
45: public $onLoggedOut;
46:
47:
48: private $authenticationHandler;
49:
50:
51: private $authorizationHandler;
52:
53:
54: private $namespace = '';
55:
56:
57: private $session;
58:
59:
60:
61:
62:
63:
64:
65: 66: 67: 68: 69: 70: 71:
72: public function login($username = NULL, $password = NULL)
73: {
74: $handler = $this->getAuthenticationHandler();
75: if ($handler === NULL) {
76: throw new InvalidStateException('Authentication handler has not been set.');
77: }
78:
79: $this->logout(TRUE);
80:
81: $credentials = func_get_args();
82: $this->setIdentity($handler->authenticate($credentials));
83: $this->setAuthenticated(TRUE);
84: $this->onLoggedIn($this);
85: }
86:
87:
88:
89: 90: 91: 92: 93:
94: final public function logout($clearIdentity = FALSE)
95: {
96: if ($this->isLoggedIn()) {
97: $this->setAuthenticated(FALSE);
98: $this->onLoggedOut($this);
99: }
100:
101: if ($clearIdentity) {
102: $this->setIdentity(NULL);
103: }
104: }
105:
106:
107:
108: 109: 110: 111:
112: final public function isLoggedIn()
113: {
114: $session = $this->getSessionNamespace(FALSE);
115: return $session && $session->authenticated;
116: }
117:
118:
119:
120: 121: 122: 123:
124: final public function getIdentity()
125: {
126: $session = $this->getSessionNamespace(FALSE);
127: return $session ? $session->identity : NULL;
128: }
129:
130:
131:
132: 133: 134: 135:
136: public function getId()
137: {
138: $identity = $this->getIdentity();
139: return $identity ? $identity->getId() : NULL;
140: }
141:
142:
143:
144: 145: 146: 147: 148:
149: public function setAuthenticationHandler(IAuthenticator $handler)
150: {
151: $this->authenticationHandler = $handler;
152: return $this;
153: }
154:
155:
156:
157: 158: 159: 160:
161: final public function getAuthenticationHandler()
162: {
163: if ($this->authenticationHandler === NULL) {
164: $this->authenticationHandler = NEnvironment::getService('Nette\\Security\\IAuthenticator');
165: }
166: return $this->authenticationHandler;
167: }
168:
169:
170:
171: 172: 173: 174: 175:
176: public function setNamespace($namespace)
177: {
178: if ($this->namespace !== $namespace) {
179: $this->namespace = (string) $namespace;
180: $this->session = NULL;
181: }
182: return $this;
183: }
184:
185:
186:
187: 188: 189: 190:
191: final public function getNamespace()
192: {
193: return $this->namespace;
194: }
195:
196:
197:
198: 199: 200: 201: 202: 203: 204:
205: public function setExpiration($time, $whenBrowserIsClosed = TRUE, $clearIdentity = FALSE)
206: {
207: $session = $this->getSessionNamespace(TRUE);
208: if ($time) {
209: $time = NTools::createDateTime($time)->format('U');
210: $session->expireTime = $time;
211: $session->expireDelta = $time - time();
212:
213: } else {
214: unset($session->expireTime, $session->expireDelta);
215: }
216:
217: $session->expireIdentity = (bool) $clearIdentity;
218: $session->expireBrowser = (bool) $whenBrowserIsClosed;
219: $session->browserCheck = TRUE;
220: $session->setExpiration(0, 'browserCheck');
221: return $this;
222: }
223:
224:
225:
226: 227: 228: 229:
230: final public function getLogoutReason()
231: {
232: $session = $this->getSessionNamespace(FALSE);
233: return $session ? $session->reason : NULL;
234: }
235:
236:
237:
238: 239: 240: 241:
242: protected function getSessionNamespace($need)
243: {
244: if ($this->session !== NULL) {
245: return $this->session;
246: }
247:
248: $sessionHandler = $this->getSession();
249: if (!$need && !$sessionHandler->exists()) {
250: return NULL;
251: }
252:
253: $this->session = $session = $sessionHandler->getNamespace('Nette.Web.User/' . $this->namespace);
254:
255: if (!($session->identity instanceof IIdentity) || !is_bool($session->authenticated)) {
256: $session->remove();
257: }
258:
259: if ($session->authenticated && $session->expireBrowser && !$session->browserCheck) { 260: $session->reason = self::BROWSER_CLOSED;
261: $session->authenticated = FALSE;
262: $this->onLoggedOut($this);
263: if ($session->expireIdentity) {
264: unset($session->identity);
265: }
266: }
267:
268: if ($session->authenticated && $session->expireDelta > 0) { 269: if ($session->expireTime < time()) {
270: $session->reason = self::INACTIVITY;
271: $session->authenticated = FALSE;
272: $this->onLoggedOut($this);
273: if ($session->expireIdentity) {
274: unset($session->identity);
275: }
276: }
277: $session->expireTime = time() + $session->expireDelta; 278: }
279:
280: if (!$session->authenticated) {
281: unset($session->expireTime, $session->expireDelta, $session->expireIdentity,
282: $session->expireBrowser, $session->browserCheck, $session->authTime);
283: }
284:
285: return $this->session;
286: }
287:
288:
289:
290: 291: 292: 293: 294:
295: protected function setAuthenticated($state)
296: {
297: $session = $this->getSessionNamespace(TRUE);
298: $session->authenticated = (bool) $state;
299:
300: 301: $this->getSession()->regenerateId();
302:
303: if ($state) {
304: $session->reason = NULL;
305: $session->authTime = time(); 306:
307: } else {
308: $session->reason = self::MANUAL;
309: $session->authTime = NULL;
310: }
311: return $this;
312: }
313:
314:
315:
316: 317: 318: 319: 320:
321: protected function setIdentity(IIdentity $identity = NULL)
322: {
323: $this->getSessionNamespace(TRUE)->identity = $identity;
324: return $this;
325: }
326:
327:
328:
329:
330:
331:
332:
333: 334: 335: 336:
337: public function getRoles()
338: {
339: if (!$this->isLoggedIn()) {
340: return array($this->guestRole);
341: }
342:
343: $identity = $this->getIdentity();
344: return $identity ? $identity->getRoles() : array($this->authenticatedRole);
345: }
346:
347:
348:
349: 350: 351: 352: 353:
354: final public function isInRole($role)
355: {
356: return in_array($role, $this->getRoles(), TRUE);
357: }
358:
359:
360:
361: 362: 363: 364: 365: 366: 367:
368: public function isAllowed($resource = IAuthorizator::ALL, $privilege = IAuthorizator::ALL)
369: {
370: $handler = $this->getAuthorizationHandler();
371: if (!$handler) {
372: throw new InvalidStateException("Authorization handler has not been set.");
373: }
374:
375: foreach ($this->getRoles() as $role) {
376: if ($handler->isAllowed($role, $resource, $privilege)) return TRUE;
377: }
378:
379: return FALSE;
380: }
381:
382:
383:
384: 385: 386: 387: 388:
389: public function setAuthorizationHandler(IAuthorizator $handler)
390: {
391: $this->authorizationHandler = $handler;
392: return $this;
393: }
394:
395:
396:
397: 398: 399: 400:
401: final public function getAuthorizationHandler()
402: {
403: if ($this->authorizationHandler === NULL) {
404: $this->authorizationHandler = NEnvironment::getService('Nette\\Security\\IAuthorizator');
405: }
406: return $this->authorizationHandler;
407: }
408:
409:
410:
411:
412:
413:
414:
415: 416: 417: 418:
419: protected function getSession()
420: {
421: return NEnvironment::getSession();
422: }
423:
424: }
425: