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