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