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