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: class User extends Nette\Object
25: {
26:
27: const MANUAL = IUserStorage::MANUAL,
28: INACTIVITY = IUserStorage::INACTIVITY,
29: BROWSER_CLOSED = IUserStorage::BROWSER_CLOSED;
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: public function __construct(IUserStorage $storage, IAuthenticator $authenticator = NULL, IAuthorizator $authorizator = NULL)
54: {
55: $this->storage = $storage;
56: $this->authenticator = $authenticator;
57: $this->authorizator = $authorizator;
58: }
59:
60:
61: 62: 63:
64: public function getStorage()
65: {
66: return $this->storage;
67: }
68:
69:
70:
71:
72:
73: 74: 75: 76: 77: 78: 79:
80: public function login($id = NULL, $password = NULL)
81: {
82: $this->logout(TRUE);
83: if (!$id instanceof IIdentity) {
84: $id = $this->getAuthenticator()->authenticate(func_get_args());
85: }
86: $this->storage->setIdentity($id);
87: $this->storage->setAuthenticated(TRUE);
88: $this->onLoggedIn($this);
89: }
90:
91:
92: 93: 94: 95: 96:
97: public function logout($clearIdentity = FALSE)
98: {
99: if ($this->isLoggedIn()) {
100: $this->onLoggedOut($this);
101: $this->storage->setAuthenticated(FALSE);
102: }
103: if ($clearIdentity) {
104: $this->storage->setIdentity(NULL);
105: }
106: }
107:
108:
109: 110: 111: 112:
113: public function isLoggedIn()
114: {
115: return $this->storage->isAuthenticated();
116: }
117:
118:
119: 120: 121: 122:
123: public function getIdentity()
124: {
125: return $this->storage->getIdentity();
126: }
127:
128:
129: 130: 131: 132:
133: public function getId()
134: {
135: $identity = $this->getIdentity();
136: return $identity ? $identity->getId() : NULL;
137: }
138:
139:
140: 141: 142: 143:
144: public function setAuthenticator(IAuthenticator $handler)
145: {
146: $this->authenticator = $handler;
147: return $this;
148: }
149:
150:
151: 152: 153: 154:
155: public function getAuthenticator($need = TRUE)
156: {
157: if ($need && !$this->authenticator) {
158: throw new Nette\InvalidStateException('Authenticator has not been set.');
159: }
160: return $this->authenticator;
161: }
162:
163:
164: 165: 166: 167: 168: 169: 170:
171: public function setExpiration($time, $whenBrowserIsClosed = TRUE, $clearIdentity = FALSE)
172: {
173: $flags = ($whenBrowserIsClosed ? IUserStorage::BROWSER_CLOSED : 0) | ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
174: $this->storage->setExpiration($time, $flags);
175: return $this;
176: }
177:
178:
179: 180: 181: 182:
183: public function getLogoutReason()
184: {
185: return $this->storage->getLogoutReason();
186: }
187:
188:
189:
190:
191:
192: 193: 194: 195:
196: public function getRoles()
197: {
198: if (!$this->isLoggedIn()) {
199: return array($this->guestRole);
200: }
201:
202: $identity = $this->getIdentity();
203: return $identity && $identity->getRoles() ? $identity->getRoles() : array($this->authenticatedRole);
204: }
205:
206:
207: 208: 209: 210: 211:
212: public function isInRole($role)
213: {
214: return in_array($role, $this->getRoles(), TRUE);
215: }
216:
217:
218: 219: 220: 221: 222: 223: 224:
225: public function isAllowed($resource = IAuthorizator::ALL, $privilege = IAuthorizator::ALL)
226: {
227: foreach ($this->getRoles() as $role) {
228: if ($this->getAuthorizator()->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: public function getAuthorizator($need = TRUE)
253: {
254: if ($need && !$this->authorizator) {
255: throw new Nette\InvalidStateException('Authorizator has not been set.');
256: }
257: return $this->authorizator;
258: }
259:
260: }
261: