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 User extends Object
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, IDIContainer $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 ($clearIdentity) {
108: $this->storage->setIdentity(NULL);
109: }
110: if ($this->isLoggedIn()) {
111: $this->storage->setAuthenticated(FALSE);
112: $this->onLoggedOut($this);
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:
157: public function setAuthenticator(IAuthenticator $handler)
158: {
159: $this->authenticator = $handler;
160: return $this;
161: }
162:
163:
164:
165: 166: 167: 168:
169: final public function getAuthenticator()
170: {
171: return ($tmp=$this->authenticator) ? $tmp : $this->context->getByType('IAuthenticator');
172: }
173:
174:
175:
176: 177: 178: 179: 180: 181: 182:
183: public function setExpiration($time, $whenBrowserIsClosed = TRUE, $clearIdentity = FALSE)
184: {
185: $flags = ($whenBrowserIsClosed ? IUserStorage::BROWSER_CLOSED : 0) | ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
186: $this->storage->setExpiration($time, $flags);
187: return $this;
188: }
189:
190:
191:
192: 193: 194: 195:
196: final public function getLogoutReason()
197: {
198: return $this->storage->getLogoutReason();
199: }
200:
201:
202:
203:
204:
205:
206:
207: 208: 209: 210:
211: public function getRoles()
212: {
213: if (!$this->isLoggedIn()) {
214: return array($this->guestRole);
215: }
216:
217: $identity = $this->getIdentity();
218: return $identity && $identity->getRoles() ? $identity->getRoles() : array($this->authenticatedRole);
219: }
220:
221:
222:
223: 224: 225: 226: 227:
228: final public function isInRole($role)
229: {
230: return in_array($role, $this->getRoles(), TRUE);
231: }
232:
233:
234:
235: 236: 237: 238: 239: 240: 241:
242: public function isAllowed($resource = IAuthorizator::ALL, $privilege = IAuthorizator::ALL)
243: {
244: $authorizator = $this->getAuthorizator();
245: foreach ($this->getRoles() as $role) {
246: if ($authorizator->isAllowed($role, $resource, $privilege)) {
247: return TRUE;
248: }
249: }
250:
251: return FALSE;
252: }
253:
254:
255:
256: 257: 258: 259: 260:
261: public function setAuthorizator(IAuthorizator $handler)
262: {
263: $this->authorizator = $handler;
264: return $this;
265: }
266:
267:
268:
269: 270: 271: 272:
273: final public function getAuthorizator()
274: {
275: return ($tmp=$this->authorizator) ? $tmp : $this->context->getByType('IAuthorizator');
276: }
277:
278:
279:
280:
281:
282:
283: function setNamespace($namespace)
284: {
285: trigger_error(__METHOD__ . '() is deprecated; use getStorage()->setNamespace() instead.', E_USER_WARNING);
286: $this->storage->setNamespace($namespace);
287: return $this;
288: }
289:
290:
291: function getNamespace()
292: {
293: trigger_error(__METHOD__ . '() is deprecated; use getStorage()->getNamespace() instead.', E_USER_WARNING);
294: return $this->storage->getNamespace();
295: }
296:
297:
298: function setAuthenticationHandler($v)
299: {
300: trigger_error(__METHOD__ . '() is deprecated; use setAuthenticator() instead.', E_USER_WARNING);
301: return $this->setAuthenticator($v);
302: }
303:
304:
305: function setAuthorizationHandler($v)
306: {
307: trigger_error(__METHOD__ . '() is deprecated; use setAuthorizator() instead.', E_USER_WARNING);
308: return $this->setAuthorizator($v);
309: }
310:
311: }
312: