1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Security;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class User extends Nette\Object
27: {
28:
29: const MANUAL = IUserStorage::MANUAL,
30: INACTIVITY = IUserStorage::INACTIVITY,
31: BROWSER_CLOSED = IUserStorage::BROWSER_CLOSED;
32:
33:
34: public $guestRole = 'guest';
35:
36:
37: public $authenticatedRole = 'authenticated';
38:
39:
40: public $onLoggedIn;
41:
42:
43: public $onLoggedOut;
44:
45:
46: private $storage;
47:
48:
49: private $authenticator;
50:
51:
52: private $authorizator;
53:
54:
55: private $context;
56:
57:
58: public function __construct(IUserStorage $storage, Nette\DI\Container $context)
59: {
60: $this->storage = $storage;
61: $this->context = $context;
62: }
63:
64:
65: 66: 67:
68: public function getStorage()
69: {
70: return $this->storage;
71: }
72:
73:
74:
75:
76:
77: 78: 79: 80: 81: 82: 83:
84: public function login($id = NULL, $password = NULL)
85: {
86: $this->logout(TRUE);
87: if (!$id instanceof IIdentity) {
88: $id = $this->getAuthenticator()->authenticate(func_get_args());
89: }
90: $this->storage->setIdentity($id);
91: $this->storage->setAuthenticated(TRUE);
92: $this->onLoggedIn($this);
93: }
94:
95:
96: 97: 98: 99: 100:
101: public function logout($clearIdentity = FALSE)
102: {
103: if ($this->isLoggedIn()) {
104: $this->onLoggedOut($this);
105: $this->storage->setAuthenticated(FALSE);
106: }
107: if ($clearIdentity) {
108: $this->storage->setIdentity(NULL);
109: }
110: }
111:
112:
113: 114: 115: 116:
117: public function isLoggedIn()
118: {
119: return $this->storage->isAuthenticated();
120: }
121:
122:
123: 124: 125: 126:
127: public function getIdentity()
128: {
129: return $this->storage->getIdentity();
130: }
131:
132:
133: 134: 135: 136:
137: public function getId()
138: {
139: $identity = $this->getIdentity();
140: return $identity ? $identity->getId() : NULL;
141: }
142:
143:
144: 145: 146: 147:
148: public function setAuthenticator(IAuthenticator $handler)
149: {
150: $this->authenticator = $handler;
151: return $this;
152: }
153:
154:
155: 156: 157: 158:
159: public function getAuthenticator()
160: {
161: return $this->authenticator ?: $this->context->getByType('Nette\Security\IAuthenticator');
162: }
163:
164:
165: 166: 167: 168: 169: 170: 171:
172: public function setExpiration($time, $whenBrowserIsClosed = TRUE, $clearIdentity = FALSE)
173: {
174: $flags = ($whenBrowserIsClosed ? IUserStorage::BROWSER_CLOSED : 0) | ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
175: $this->storage->setExpiration($time, $flags);
176: return $this;
177: }
178:
179:
180: 181: 182: 183:
184: public function getLogoutReason()
185: {
186: return $this->storage->getLogoutReason();
187: }
188:
189:
190:
191:
192:
193: 194: 195: 196:
197: public function getRoles()
198: {
199: if (!$this->isLoggedIn()) {
200: return array($this->guestRole);
201: }
202:
203: $identity = $this->getIdentity();
204: return $identity && $identity->getRoles() ? $identity->getRoles() : array($this->authenticatedRole);
205: }
206:
207:
208: 209: 210: 211: 212:
213: public function isInRole($role)
214: {
215: return in_array($role, $this->getRoles(), TRUE);
216: }
217:
218:
219: 220: 221: 222: 223: 224: 225:
226: public function isAllowed($resource = IAuthorizator::ALL, $privilege = IAuthorizator::ALL)
227: {
228: $authorizator = $this->getAuthorizator();
229: foreach ($this->getRoles() as $role) {
230: if ($authorizator->isAllowed($role, $resource, $privilege)) {
231: return TRUE;
232: }
233: }
234:
235: return FALSE;
236: }
237:
238:
239: 240: 241: 242:
243: public function setAuthorizator(IAuthorizator $handler)
244: {
245: $this->authorizator = $handler;
246: return $this;
247: }
248:
249:
250: 251: 252: 253:
254: public function getAuthorizator()
255: {
256: return $this->authorizator ?: $this->context->getByType('Nette\Security\IAuthorizator');
257: }
258:
259:
260:
261:
262:
263: function setNamespace($namespace)
264: {
265: trigger_error(__METHOD__ . '() is deprecated; use getStorage()->setNamespace() instead.', E_USER_WARNING);
266: $this->storage->setNamespace($namespace);
267: return $this;
268: }
269:
270:
271: function getNamespace()
272: {
273: trigger_error(__METHOD__ . '() is deprecated; use getStorage()->getNamespace() instead.', E_USER_WARNING);
274: return $this->storage->getNamespace();
275: }
276:
277:
278: function setAuthenticationHandler($v)
279: {
280: trigger_error(__METHOD__ . '() is deprecated; use setAuthenticator() instead.', E_USER_WARNING);
281: return $this->setAuthenticator($v);
282: }
283:
284:
285: function setAuthorizationHandler($v)
286: {
287: trigger_error(__METHOD__ . '() is deprecated; use setAuthorizator() instead.', E_USER_WARNING);
288: return $this->setAuthorizator($v);
289: }
290:
291: }
292: