1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Component; |
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
use yii\base\InvalidValueException; |
14
|
|
|
use yii\rbac\CheckAccessInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User is the class for the `user` application component that manages the user authentication status. |
18
|
|
|
* |
19
|
|
|
* You may use [[isGuest]] to determine whether the current user is a guest or not. |
20
|
|
|
* If the user is a guest, the [[identity]] property would return `null`. Otherwise, it would |
21
|
|
|
* be an instance of [[IdentityInterface]]. |
22
|
|
|
* |
23
|
|
|
* You may call various methods to change the user authentication status: |
24
|
|
|
* |
25
|
|
|
* - [[login()]]: sets the specified identity and remembers the authentication status in session and cookie; |
26
|
|
|
* - [[logout()]]: marks the user as a guest and clears the relevant information from session and cookie; |
27
|
|
|
* - [[setIdentity()]]: changes the user identity without touching session or cookie |
28
|
|
|
* (this is best used in stateless RESTful API implementation). |
29
|
|
|
* |
30
|
|
|
* Note that User only maintains the user authentication status. It does NOT handle how to authenticate |
31
|
|
|
* a user. The logic of how to authenticate a user should be done in the class implementing [[IdentityInterface]]. |
32
|
|
|
* You are also required to set [[identityClass]] with the name of this class. |
33
|
|
|
* |
34
|
|
|
* User is configured as an application component in [[\yii\web\Application]] by default. |
35
|
|
|
* You can access that instance via `Yii::$app->user`. |
36
|
|
|
* |
37
|
|
|
* You can modify its configuration by adding an array to your application config under `components` |
38
|
|
|
* as it is shown in the following example: |
39
|
|
|
* |
40
|
|
|
* ```php |
41
|
|
|
* 'user' => [ |
42
|
|
|
* 'identityClass' => 'app\models\User', // User must implement the IdentityInterface |
43
|
|
|
* 'enableAutoLogin' => true, |
44
|
|
|
* // 'loginUrl' => ['user/login'], |
45
|
|
|
* // ... |
46
|
|
|
* ] |
47
|
|
|
* ``` |
48
|
|
|
* |
49
|
|
|
* @property string|int $id The unique identifier for the user. If `null`, it means the user is a guest. This |
50
|
|
|
* property is read-only. |
51
|
|
|
* @property IdentityInterface|null $identity The identity object associated with the currently logged-in |
52
|
|
|
* user. `null` is returned if the user is not logged in (not authenticated). |
53
|
|
|
* @property bool $isGuest Whether the current user is a guest. This property is read-only. |
54
|
|
|
* @property string $returnUrl The URL that the user should be redirected to after login. Note that the type |
55
|
|
|
* of this property differs in getter and setter. See [[getReturnUrl()]] and [[setReturnUrl()]] for details. |
56
|
|
|
* |
57
|
|
|
* @author Qiang Xue <[email protected]> |
58
|
|
|
* @since 2.0 |
59
|
|
|
*/ |
60
|
|
|
class User extends Component |
61
|
|
|
{ |
62
|
|
|
const EVENT_BEFORE_LOGIN = 'beforeLogin'; |
63
|
|
|
const EVENT_AFTER_LOGIN = 'afterLogin'; |
64
|
|
|
const EVENT_BEFORE_LOGOUT = 'beforeLogout'; |
65
|
|
|
const EVENT_AFTER_LOGOUT = 'afterLogout'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string the class name of the [[identity]] object. |
69
|
|
|
*/ |
70
|
|
|
public $identityClass; |
71
|
|
|
/** |
72
|
|
|
* @var bool whether to enable cookie-based login. Defaults to `false`. |
73
|
|
|
* Note that this property will be ignored if [[enableSession]] is `false`. |
74
|
|
|
*/ |
75
|
|
|
public $enableAutoLogin = false; |
76
|
|
|
/** |
77
|
|
|
* @var bool whether to use session to persist authentication status across multiple requests. |
78
|
|
|
* You set this property to be `false` if your application is stateless, which is often the case |
79
|
|
|
* for RESTful APIs. |
80
|
|
|
*/ |
81
|
|
|
public $enableSession = true; |
82
|
|
|
/** |
83
|
|
|
* @var string|array the URL for login when [[loginRequired()]] is called. |
84
|
|
|
* If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
85
|
|
|
* The first element of the array should be the route to the login action, and the rest of |
86
|
|
|
* the name-value pairs are GET parameters used to construct the login URL. For example, |
87
|
|
|
* |
88
|
|
|
* ```php |
89
|
|
|
* ['site/login', 'ref' => 1] |
90
|
|
|
* ``` |
91
|
|
|
* |
92
|
|
|
* If this property is `null`, a 403 HTTP exception will be raised when [[loginRequired()]] is called. |
93
|
|
|
*/ |
94
|
|
|
public $loginUrl = ['site/login']; |
95
|
|
|
/** |
96
|
|
|
* @var array the configuration of the identity cookie. This property is used only when [[enableAutoLogin]] is `true`. |
97
|
|
|
* @see Cookie |
98
|
|
|
*/ |
99
|
|
|
public $identityCookie = ['name' => '_identity', 'httpOnly' => true]; |
100
|
|
|
/** |
101
|
|
|
* @var int the number of seconds in which the user will be logged out automatically if he |
102
|
|
|
* remains inactive. If this property is not set, the user will be logged out after |
103
|
|
|
* the current session expires (c.f. [[Session::timeout]]). |
104
|
|
|
* Note that this will not work if [[enableAutoLogin]] is `true`. |
105
|
|
|
*/ |
106
|
|
|
public $authTimeout; |
107
|
|
|
/** |
108
|
|
|
* @var CheckAccessInterface The access checker to use for checking access. |
109
|
|
|
* If not set the application auth manager will be used. |
110
|
|
|
* @since 2.0.9 |
111
|
|
|
*/ |
112
|
|
|
public $accessChecker; |
113
|
|
|
/** |
114
|
|
|
* @var int the number of seconds in which the user will be logged out automatically |
115
|
|
|
* regardless of activity. |
116
|
|
|
* Note that this will not work if [[enableAutoLogin]] is `true`. |
117
|
|
|
*/ |
118
|
|
|
public $absoluteAuthTimeout; |
119
|
|
|
/** |
120
|
|
|
* @var bool whether to automatically renew the identity cookie each time a page is requested. |
121
|
|
|
* This property is effective only when [[enableAutoLogin]] is `true`. |
122
|
|
|
* When this is `false`, the identity cookie will expire after the specified duration since the user |
123
|
|
|
* is initially logged in. When this is `true`, the identity cookie will expire after the specified duration |
124
|
|
|
* since the user visits the site the last time. |
125
|
|
|
* @see enableAutoLogin |
126
|
|
|
*/ |
127
|
|
|
public $autoRenewCookie = true; |
128
|
|
|
/** |
129
|
|
|
* @var string the session variable name used to store the value of [[id]]. |
130
|
|
|
*/ |
131
|
|
|
public $idParam = '__id'; |
132
|
|
|
/** |
133
|
|
|
* @var string the session variable name used to store the value of expiration timestamp of the authenticated state. |
134
|
|
|
* This is used when [[authTimeout]] is set. |
135
|
|
|
*/ |
136
|
|
|
public $authTimeoutParam = '__expire'; |
137
|
|
|
/** |
138
|
|
|
* @var string the session variable name used to store the value of absolute expiration timestamp of the authenticated state. |
139
|
|
|
* This is used when [[absoluteAuthTimeout]] is set. |
140
|
|
|
*/ |
141
|
|
|
public $absoluteAuthTimeoutParam = '__absoluteExpire'; |
142
|
|
|
/** |
143
|
|
|
* @var string the session variable name used to store the value of [[returnUrl]]. |
144
|
|
|
*/ |
145
|
|
|
public $returnUrlParam = '__returnUrl'; |
146
|
|
|
/** |
147
|
|
|
* @var array MIME types for which this component should redirect to the [[loginUrl]]. |
148
|
|
|
* @since 2.0.8 |
149
|
|
|
*/ |
150
|
|
|
public $acceptableRedirectTypes = ['text/html', 'application/xhtml+xml']; |
151
|
|
|
|
152
|
|
|
private $_access = []; |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Initializes the application component. |
157
|
|
|
*/ |
158
|
91 |
|
public function init() |
159
|
|
|
{ |
160
|
91 |
|
parent::init(); |
161
|
|
|
|
162
|
91 |
|
if ($this->identityClass === null) { |
163
|
|
|
throw new InvalidConfigException('User::identityClass must be set.'); |
164
|
|
|
} |
165
|
91 |
|
if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) { |
166
|
|
|
throw new InvalidConfigException('User::identityCookie must contain the "name" element.'); |
167
|
|
|
} |
168
|
91 |
|
if (!empty($this->accessChecker) && is_string($this->accessChecker)) { |
169
|
1 |
|
$this->accessChecker = Yii::createObject($this->accessChecker); |
170
|
|
|
} |
171
|
91 |
|
} |
172
|
|
|
|
173
|
|
|
private $_identity = false; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the identity object associated with the currently logged-in user. |
177
|
|
|
* When [[enableSession]] is true, this method may attempt to read the user's authentication data |
178
|
|
|
* stored in session and reconstruct the corresponding identity object, if it has not done so before. |
179
|
|
|
* @param bool $autoRenew whether to automatically renew authentication status if it has not been done so before. |
180
|
|
|
* This is only useful when [[enableSession]] is true. |
181
|
|
|
* @return IdentityInterface|null the identity object associated with the currently logged-in user. |
182
|
|
|
* `null` is returned if the user is not logged in (not authenticated). |
183
|
|
|
* @see login() |
184
|
|
|
* @see logout() |
185
|
|
|
*/ |
186
|
81 |
|
public function getIdentity($autoRenew = true) |
187
|
|
|
{ |
188
|
81 |
|
if ($this->_identity === false) { |
189
|
29 |
|
if ($this->enableSession && $autoRenew) { |
190
|
|
|
try { |
191
|
28 |
|
$this->_identity = null; |
192
|
28 |
|
$this->renewAuthStatus(); |
193
|
1 |
|
} catch (\Exception $e) { |
194
|
1 |
|
$this->_identity = false; |
195
|
1 |
|
throw $e; |
196
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
197
|
|
|
$this->_identity = false; |
198
|
27 |
|
throw $e; |
199
|
|
|
} |
200
|
|
|
} else { |
201
|
1 |
|
return null; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
79 |
|
return $this->_identity; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Sets the user identity object. |
210
|
|
|
* |
211
|
|
|
* Note that this method does not deal with session or cookie. You should usually use [[switchIdentity()]] |
212
|
|
|
* to change the identity of the current user. |
213
|
|
|
* |
214
|
|
|
* @param IdentityInterface|null $identity the identity object associated with the currently logged user. |
215
|
|
|
* If null, it means the current user will be a guest without any associated identity. |
216
|
|
|
* @throws InvalidValueException if `$identity` object does not implement [[IdentityInterface]]. |
217
|
|
|
*/ |
218
|
79 |
|
public function setIdentity($identity) |
219
|
|
|
{ |
220
|
79 |
|
if ($identity instanceof IdentityInterface) { |
221
|
51 |
|
$this->_identity = $identity; |
|
|
|
|
222
|
51 |
|
$this->_access = []; |
223
|
34 |
|
} elseif ($identity === null) { |
224
|
34 |
|
$this->_identity = null; |
225
|
|
|
} else { |
226
|
|
|
throw new InvalidValueException('The identity object must implement IdentityInterface.'); |
227
|
|
|
} |
228
|
79 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Logs in a user. |
232
|
|
|
* |
233
|
|
|
* After logging in a user: |
234
|
|
|
* - the user's identity information is obtainable from the [[identity]] property |
235
|
|
|
* |
236
|
|
|
* If [[enableSession]] is `true`: |
237
|
|
|
* - the identity information will be stored in session and be available in the next requests |
238
|
|
|
* - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser |
239
|
|
|
* - in case of `$duration > 0`: as long as the session remains active or as long as the cookie |
240
|
|
|
* remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`. |
241
|
|
|
* |
242
|
|
|
* If [[enableSession]] is `false`: |
243
|
|
|
* - the `$duration` parameter will be ignored |
244
|
|
|
* |
245
|
|
|
* @param IdentityInterface $identity the user identity (which should already be authenticated) |
246
|
|
|
* @param int $duration number of seconds that the user can remain in logged-in status, defaults to `0` |
247
|
|
|
* @return bool whether the user is logged in |
248
|
|
|
*/ |
249
|
33 |
|
public function login(IdentityInterface $identity, $duration = 0) |
250
|
|
|
{ |
251
|
33 |
|
if ($this->beforeLogin($identity, false, $duration)) { |
252
|
33 |
|
$this->switchIdentity($identity, $duration); |
253
|
33 |
|
$id = $identity->getId(); |
254
|
33 |
|
$ip = Yii::$app->getRequest()->getUserIP(); |
255
|
33 |
|
if ($this->enableSession) { |
256
|
33 |
|
$log = "User '$id' logged in from $ip with duration $duration."; |
257
|
|
|
} else { |
258
|
|
|
$log = "User '$id' logged in from $ip. Session not enabled."; |
259
|
|
|
} |
260
|
|
|
|
261
|
33 |
|
$this->regenerateCsrfToken(); |
262
|
|
|
|
263
|
33 |
|
Yii::info($log, __METHOD__); |
264
|
33 |
|
$this->afterLogin($identity, false, $duration); |
265
|
|
|
} |
266
|
|
|
|
267
|
33 |
|
return !$this->getIsGuest(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Regenerates CSRF token |
272
|
|
|
* |
273
|
|
|
* @since 2.0.14.2 |
274
|
|
|
*/ |
275
|
33 |
|
protected function regenerateCsrfToken() |
276
|
|
|
{ |
277
|
33 |
|
$request = Yii::$app->getRequest(); |
278
|
33 |
|
if ($request->enableCsrfCookie || $this->enableSession) { |
279
|
33 |
|
$request->getCsrfToken(true); |
280
|
|
|
} |
281
|
33 |
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Logs in a user by the given access token. |
285
|
|
|
* This method will first authenticate the user by calling [[IdentityInterface::findIdentityByAccessToken()]] |
286
|
|
|
* with the provided access token. If successful, it will call [[login()]] to log in the authenticated user. |
287
|
|
|
* If authentication fails or [[login()]] is unsuccessful, it will return null. |
288
|
|
|
* @param string $token the access token |
289
|
|
|
* @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
290
|
|
|
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
291
|
|
|
* @return IdentityInterface|null the identity associated with the given access token. Null is returned if |
292
|
|
|
* the access token is invalid or [[login()]] is unsuccessful. |
293
|
|
|
*/ |
294
|
42 |
|
public function loginByAccessToken($token, $type = null) |
295
|
|
|
{ |
296
|
|
|
/* @var $class IdentityInterface */ |
297
|
42 |
|
$class = $this->identityClass; |
298
|
42 |
|
$identity = $class::findIdentityByAccessToken($token, $type); |
299
|
42 |
|
if ($identity && $this->login($identity)) { |
300
|
27 |
|
return $identity; |
301
|
|
|
} |
302
|
|
|
|
303
|
15 |
|
return null; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Logs in a user by cookie. |
308
|
|
|
* |
309
|
|
|
* This method attempts to log in a user using the ID and authKey information |
310
|
|
|
* provided by the [[identityCookie|identity cookie]]. |
311
|
|
|
*/ |
312
|
2 |
|
protected function loginByCookie() |
313
|
|
|
{ |
314
|
2 |
|
$data = $this->getIdentityAndDurationFromCookie(); |
315
|
2 |
|
if (isset($data['identity'], $data['duration'])) { |
316
|
1 |
|
$identity = $data['identity']; |
317
|
1 |
|
$duration = $data['duration']; |
318
|
1 |
|
if ($this->beforeLogin($identity, true, $duration)) { |
319
|
1 |
|
$this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); |
320
|
1 |
|
$id = $identity->getId(); |
321
|
1 |
|
$ip = Yii::$app->getRequest()->getUserIP(); |
322
|
1 |
|
Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); |
323
|
1 |
|
$this->afterLogin($identity, true, $duration); |
324
|
|
|
} |
325
|
|
|
} |
326
|
2 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Logs out the current user. |
330
|
|
|
* This will remove authentication-related session data. |
331
|
|
|
* If `$destroySession` is true, all session data will be removed. |
332
|
|
|
* @param bool $destroySession whether to destroy the whole session. Defaults to true. |
333
|
|
|
* This parameter is ignored if [[enableSession]] is false. |
334
|
|
|
* @return bool whether the user is logged out |
335
|
|
|
*/ |
336
|
|
|
public function logout($destroySession = true) |
337
|
|
|
{ |
338
|
|
|
$identity = $this->getIdentity(); |
339
|
|
|
if ($identity !== null && $this->beforeLogout($identity)) { |
|
|
|
|
340
|
|
|
$this->switchIdentity(null); |
341
|
|
|
$id = $identity->getId(); |
|
|
|
|
342
|
|
|
$ip = Yii::$app->getRequest()->getUserIP(); |
343
|
|
|
Yii::info("User '$id' logged out from $ip.", __METHOD__); |
344
|
|
|
if ($destroySession && $this->enableSession) { |
345
|
|
|
Yii::$app->getSession()->destroy(); |
|
|
|
|
346
|
|
|
} |
347
|
|
|
$this->afterLogout($identity); |
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return $this->getIsGuest(); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Returns a value indicating whether the user is a guest (not authenticated). |
355
|
|
|
* @return bool whether the current user is a guest. |
356
|
|
|
* @see getIdentity() |
357
|
|
|
*/ |
358
|
34 |
|
public function getIsGuest() |
359
|
|
|
{ |
360
|
34 |
|
return $this->getIdentity() === null; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Returns a value that uniquely represents the user. |
365
|
|
|
* @return string|int the unique identifier for the user. If `null`, it means the user is a guest. |
366
|
|
|
* @see getIdentity() |
367
|
|
|
*/ |
368
|
76 |
|
public function getId() |
369
|
|
|
{ |
370
|
76 |
|
$identity = $this->getIdentity(); |
371
|
|
|
|
372
|
76 |
|
return $identity !== null ? $identity->getId() : null; |
|
|
|
|
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Returns the URL that the browser should be redirected to after successful login. |
377
|
|
|
* |
378
|
|
|
* This method reads the return URL from the session. It is usually used by the login action which |
379
|
|
|
* may call this method to redirect the browser to where it goes after successful authentication. |
380
|
|
|
* |
381
|
|
|
* @param string|array $defaultUrl the default return URL in case it was not set previously. |
382
|
|
|
* If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to. |
383
|
|
|
* Please refer to [[setReturnUrl()]] on accepted format of the URL. |
384
|
|
|
* @return string the URL that the user should be redirected to after login. |
385
|
|
|
* @see loginRequired() |
386
|
|
|
*/ |
387
|
2 |
|
public function getReturnUrl($defaultUrl = null) |
388
|
|
|
{ |
389
|
2 |
|
$url = Yii::$app->getSession()->get($this->returnUrlParam, $defaultUrl); |
|
|
|
|
390
|
2 |
|
if (is_array($url)) { |
391
|
|
|
if (isset($url[0])) { |
392
|
|
|
return Yii::$app->getUrlManager()->createUrl($url); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
$url = null; |
396
|
|
|
} |
397
|
|
|
|
398
|
2 |
|
return $url === null ? Yii::$app->getHomeUrl() : $url; |
|
|
|
|
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Remembers the URL in the session so that it can be retrieved back later by [[getReturnUrl()]]. |
403
|
|
|
* @param string|array $url the URL that the user should be redirected to after login. |
404
|
|
|
* If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
405
|
|
|
* The first element of the array should be the route, and the rest of |
406
|
|
|
* the name-value pairs are GET parameters used to construct the URL. For example, |
407
|
|
|
* |
408
|
|
|
* ```php |
409
|
|
|
* ['admin/index', 'ref' => 1] |
410
|
|
|
* ``` |
411
|
|
|
*/ |
412
|
2 |
|
public function setReturnUrl($url) |
413
|
|
|
{ |
414
|
2 |
|
Yii::$app->getSession()->set($this->returnUrlParam, $url); |
|
|
|
|
415
|
2 |
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Redirects the user browser to the login page. |
419
|
|
|
* |
420
|
|
|
* Before the redirection, the current URL (if it's not an AJAX url) will be kept as [[returnUrl]] so that |
421
|
|
|
* the user browser may be redirected back to the current page after successful login. |
422
|
|
|
* |
423
|
|
|
* Make sure you set [[loginUrl]] so that the user browser can be redirected to the specified login URL after |
424
|
|
|
* calling this method. |
425
|
|
|
* |
426
|
|
|
* Note that when [[loginUrl]] is set, calling this method will NOT terminate the application execution. |
427
|
|
|
* |
428
|
|
|
* @param bool $checkAjax whether to check if the request is an AJAX request. When this is true and the request |
429
|
|
|
* is an AJAX request, the current URL (for AJAX request) will NOT be set as the return URL. |
430
|
|
|
* @param bool $checkAcceptHeader whether to check if the request accepts HTML responses. Defaults to `true`. When this is true and |
431
|
|
|
* the request does not accept HTML responses the current URL will not be SET as the return URL. Also instead of |
432
|
|
|
* redirecting the user an ForbiddenHttpException is thrown. This parameter is available since version 2.0.8. |
433
|
|
|
* @return Response the redirection response if [[loginUrl]] is set |
434
|
|
|
* @throws ForbiddenHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set or a redirect is |
435
|
|
|
* not applicable. |
436
|
|
|
*/ |
437
|
2 |
|
public function loginRequired($checkAjax = true, $checkAcceptHeader = true) |
438
|
|
|
{ |
439
|
2 |
|
$request = Yii::$app->getRequest(); |
440
|
2 |
|
$canRedirect = !$checkAcceptHeader || $this->checkRedirectAcceptable(); |
441
|
2 |
|
if ($this->enableSession |
442
|
2 |
|
&& $request->getIsGet() |
443
|
2 |
|
&& (!$checkAjax || !$request->getIsAjax()) |
444
|
2 |
|
&& $canRedirect |
445
|
|
|
) { |
446
|
1 |
|
$this->setReturnUrl($request->getUrl()); |
447
|
|
|
} |
448
|
2 |
|
if ($this->loginUrl !== null && $canRedirect) { |
449
|
1 |
|
$loginUrl = (array) $this->loginUrl; |
450
|
1 |
|
if ($loginUrl[0] !== Yii::$app->requestedRoute) { |
451
|
1 |
|
return Yii::$app->getResponse()->redirect($this->loginUrl); |
452
|
|
|
} |
453
|
|
|
} |
454
|
2 |
|
throw new ForbiddenHttpException(Yii::t('yii', 'Login Required')); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* This method is called before logging in a user. |
459
|
|
|
* The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event. |
460
|
|
|
* If you override this method, make sure you call the parent implementation |
461
|
|
|
* so that the event is triggered. |
462
|
|
|
* @param IdentityInterface $identity the user identity information |
463
|
|
|
* @param bool $cookieBased whether the login is cookie-based |
464
|
|
|
* @param int $duration number of seconds that the user can remain in logged-in status. |
465
|
|
|
* If 0, it means login till the user closes the browser or the session is manually destroyed. |
466
|
|
|
* @return bool whether the user should continue to be logged in |
467
|
|
|
*/ |
468
|
33 |
|
protected function beforeLogin($identity, $cookieBased, $duration) |
469
|
|
|
{ |
470
|
33 |
|
$event = new UserEvent([ |
471
|
33 |
|
'identity' => $identity, |
472
|
33 |
|
'cookieBased' => $cookieBased, |
473
|
33 |
|
'duration' => $duration, |
474
|
|
|
]); |
475
|
33 |
|
$this->trigger(self::EVENT_BEFORE_LOGIN, $event); |
476
|
|
|
|
477
|
33 |
|
return $event->isValid; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* This method is called after the user is successfully logged in. |
482
|
|
|
* The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event. |
483
|
|
|
* If you override this method, make sure you call the parent implementation |
484
|
|
|
* so that the event is triggered. |
485
|
|
|
* @param IdentityInterface $identity the user identity information |
486
|
|
|
* @param bool $cookieBased whether the login is cookie-based |
487
|
|
|
* @param int $duration number of seconds that the user can remain in logged-in status. |
488
|
|
|
* If 0, it means login till the user closes the browser or the session is manually destroyed. |
489
|
|
|
*/ |
490
|
33 |
|
protected function afterLogin($identity, $cookieBased, $duration) |
491
|
|
|
{ |
492
|
33 |
|
$this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent([ |
493
|
33 |
|
'identity' => $identity, |
494
|
33 |
|
'cookieBased' => $cookieBased, |
495
|
33 |
|
'duration' => $duration, |
496
|
|
|
])); |
497
|
33 |
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* This method is invoked when calling [[logout()]] to log out a user. |
501
|
|
|
* The default implementation will trigger the [[EVENT_BEFORE_LOGOUT]] event. |
502
|
|
|
* If you override this method, make sure you call the parent implementation |
503
|
|
|
* so that the event is triggered. |
504
|
|
|
* @param IdentityInterface $identity the user identity information |
505
|
|
|
* @return bool whether the user should continue to be logged out |
506
|
|
|
*/ |
507
|
|
|
protected function beforeLogout($identity) |
508
|
|
|
{ |
509
|
|
|
$event = new UserEvent([ |
510
|
|
|
'identity' => $identity, |
511
|
|
|
]); |
512
|
|
|
$this->trigger(self::EVENT_BEFORE_LOGOUT, $event); |
513
|
|
|
|
514
|
|
|
return $event->isValid; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* This method is invoked right after a user is logged out via [[logout()]]. |
519
|
|
|
* The default implementation will trigger the [[EVENT_AFTER_LOGOUT]] event. |
520
|
|
|
* If you override this method, make sure you call the parent implementation |
521
|
|
|
* so that the event is triggered. |
522
|
|
|
* @param IdentityInterface $identity the user identity information |
523
|
|
|
*/ |
524
|
|
|
protected function afterLogout($identity) |
525
|
|
|
{ |
526
|
|
|
$this->trigger(self::EVENT_AFTER_LOGOUT, new UserEvent([ |
527
|
|
|
'identity' => $identity, |
528
|
|
|
])); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Renews the identity cookie. |
533
|
|
|
* This method will set the expiration time of the identity cookie to be the current time |
534
|
|
|
* plus the originally specified cookie duration. |
535
|
|
|
*/ |
536
|
|
|
protected function renewIdentityCookie() |
537
|
|
|
{ |
538
|
|
|
$name = $this->identityCookie['name']; |
539
|
|
|
$value = Yii::$app->getRequest()->getCookies()->getValue($name); |
540
|
|
|
if ($value !== null) { |
541
|
|
|
$data = json_decode($value, true); |
542
|
|
|
if (is_array($data) && isset($data[2])) { |
543
|
|
|
$cookie = Yii::createObject(array_merge($this->identityCookie, [ |
544
|
|
|
'class' => 'yii\web\Cookie', |
545
|
|
|
'value' => $value, |
546
|
|
|
'expire' => time() + (int) $data[2], |
547
|
|
|
])); |
548
|
|
|
Yii::$app->getResponse()->getCookies()->add($cookie); |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Sends an identity cookie. |
555
|
|
|
* This method is used when [[enableAutoLogin]] is true. |
556
|
|
|
* It saves [[id]], [[IdentityInterface::getAuthKey()|auth key]], and the duration of cookie-based login |
557
|
|
|
* information in the cookie. |
558
|
|
|
* @param IdentityInterface $identity |
559
|
|
|
* @param int $duration number of seconds that the user can remain in logged-in status. |
560
|
|
|
* @see loginByCookie() |
561
|
|
|
*/ |
562
|
2 |
|
protected function sendIdentityCookie($identity, $duration) |
563
|
|
|
{ |
564
|
2 |
|
$cookie = Yii::createObject(array_merge($this->identityCookie, [ |
565
|
2 |
|
'class' => 'yii\web\Cookie', |
566
|
2 |
|
'value' => json_encode([ |
567
|
2 |
|
$identity->getId(), |
568
|
2 |
|
$identity->getAuthKey(), |
569
|
2 |
|
$duration, |
570
|
2 |
|
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
571
|
2 |
|
'expire' => time() + $duration, |
572
|
|
|
])); |
573
|
2 |
|
Yii::$app->getResponse()->getCookies()->add($cookie); |
574
|
2 |
|
} |
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* Determines if an identity cookie has a valid format and contains a valid auth key. |
578
|
|
|
* This method is used when [[enableAutoLogin]] is true. |
579
|
|
|
* This method attempts to authenticate a user using the information in the identity cookie. |
580
|
|
|
* @return array|null Returns an array of 'identity' and 'duration' if valid, otherwise null. |
581
|
|
|
* @see loginByCookie() |
582
|
|
|
* @since 2.0.9 |
583
|
|
|
*/ |
584
|
2 |
|
protected function getIdentityAndDurationFromCookie() |
585
|
|
|
{ |
586
|
2 |
|
$value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); |
587
|
2 |
|
if ($value === null) { |
588
|
|
|
return null; |
589
|
|
|
} |
590
|
2 |
|
$data = json_decode($value, true); |
591
|
2 |
|
if (is_array($data) && count($data) == 3) { |
592
|
1 |
|
list($id, $authKey, $duration) = $data; |
593
|
|
|
/* @var $class IdentityInterface */ |
594
|
1 |
|
$class = $this->identityClass; |
595
|
1 |
|
$identity = $class::findIdentity($id); |
596
|
1 |
|
if ($identity !== null) { |
597
|
1 |
|
if (!$identity instanceof IdentityInterface) { |
598
|
|
|
throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); |
599
|
1 |
|
} elseif (!$identity->validateAuthKey($authKey)) { |
600
|
|
|
Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); |
601
|
|
|
} else { |
602
|
1 |
|
return ['identity' => $identity, 'duration' => $duration]; |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
} |
606
|
2 |
|
$this->removeIdentityCookie(); |
607
|
2 |
|
return null; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Removes the identity cookie. |
612
|
|
|
* This method is used when [[enableAutoLogin]] is true. |
613
|
|
|
* @since 2.0.9 |
614
|
|
|
*/ |
615
|
2 |
|
protected function removeIdentityCookie() |
616
|
|
|
{ |
617
|
2 |
|
Yii::$app->getResponse()->getCookies()->remove(Yii::createObject(array_merge($this->identityCookie, [ |
618
|
2 |
|
'class' => 'yii\web\Cookie', |
619
|
|
|
]))); |
620
|
2 |
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Switches to a new identity for the current user. |
624
|
|
|
* |
625
|
|
|
* When [[enableSession]] is true, this method may use session and/or cookie to store the user identity information, |
626
|
|
|
* according to the value of `$duration`. Please refer to [[login()]] for more details. |
627
|
|
|
* |
628
|
|
|
* This method is mainly called by [[login()]], [[logout()]] and [[loginByCookie()]] |
629
|
|
|
* when the current user needs to be associated with the corresponding identity information. |
630
|
|
|
* |
631
|
|
|
* @param IdentityInterface|null $identity the identity information to be associated with the current user. |
632
|
|
|
* If null, it means switching the current user to be a guest. |
633
|
|
|
* @param int $duration number of seconds that the user can remain in logged-in status. |
634
|
|
|
* This parameter is used only when `$identity` is not null. |
635
|
|
|
*/ |
636
|
36 |
|
public function switchIdentity($identity, $duration = 0) |
637
|
|
|
{ |
638
|
36 |
|
$this->setIdentity($identity); |
639
|
|
|
|
640
|
36 |
|
if (!$this->enableSession) { |
641
|
|
|
return; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/* Ensure any existing identity cookies are removed. */ |
645
|
36 |
|
if ($this->enableAutoLogin && ($this->autoRenewCookie || $identity === null)) { |
646
|
1 |
|
$this->removeIdentityCookie(); |
647
|
|
|
} |
648
|
|
|
|
649
|
36 |
|
$session = Yii::$app->getSession(); |
|
|
|
|
650
|
36 |
|
if (!YII_ENV_TEST) { |
651
|
|
|
$session->regenerateID(true); |
652
|
|
|
} |
653
|
36 |
|
$session->remove($this->idParam); |
654
|
36 |
|
$session->remove($this->authTimeoutParam); |
655
|
|
|
|
656
|
36 |
|
if ($identity) { |
657
|
36 |
|
$session->set($this->idParam, $identity->getId()); |
658
|
36 |
|
if ($this->authTimeout !== null) { |
659
|
1 |
|
$session->set($this->authTimeoutParam, time() + $this->authTimeout); |
660
|
|
|
} |
661
|
36 |
|
if ($this->absoluteAuthTimeout !== null) { |
662
|
|
|
$session->set($this->absoluteAuthTimeoutParam, time() + $this->absoluteAuthTimeout); |
663
|
|
|
} |
664
|
36 |
|
if ($this->enableAutoLogin && $duration > 0) { |
665
|
2 |
|
$this->sendIdentityCookie($identity, $duration); |
666
|
|
|
} |
667
|
|
|
} |
668
|
36 |
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Updates the authentication status using the information from session and cookie. |
672
|
|
|
* |
673
|
|
|
* This method will try to determine the user identity using the [[idParam]] session variable. |
674
|
|
|
* |
675
|
|
|
* If [[authTimeout]] is set, this method will refresh the timer. |
676
|
|
|
* |
677
|
|
|
* If the user identity cannot be determined by session, this method will try to [[loginByCookie()|login by cookie]] |
678
|
|
|
* if [[enableAutoLogin]] is true. |
679
|
|
|
*/ |
680
|
28 |
|
protected function renewAuthStatus() |
681
|
|
|
{ |
682
|
28 |
|
$session = Yii::$app->getSession(); |
|
|
|
|
683
|
28 |
|
$id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($this->idParam) : null; |
684
|
|
|
|
685
|
28 |
|
if ($id === null) { |
686
|
25 |
|
$identity = null; |
687
|
|
|
} else { |
688
|
|
|
/* @var $class IdentityInterface */ |
689
|
4 |
|
$class = $this->identityClass; |
690
|
4 |
|
$identity = $class::findIdentity($id); |
691
|
|
|
} |
692
|
|
|
|
693
|
27 |
|
$this->setIdentity($identity); |
694
|
|
|
|
695
|
27 |
|
if ($identity !== null && ($this->authTimeout !== null || $this->absoluteAuthTimeout !== null)) { |
696
|
1 |
|
$expire = $this->authTimeout !== null ? $session->get($this->authTimeoutParam) : null; |
697
|
1 |
|
$expireAbsolute = $this->absoluteAuthTimeout !== null ? $session->get($this->absoluteAuthTimeoutParam) : null; |
698
|
1 |
|
if ($expire !== null && $expire < time() || $expireAbsolute !== null && $expireAbsolute < time()) { |
699
|
|
|
$this->logout(false); |
700
|
1 |
|
} elseif ($this->authTimeout !== null) { |
701
|
1 |
|
$session->set($this->authTimeoutParam, time() + $this->authTimeout); |
702
|
|
|
} |
703
|
|
|
} |
704
|
|
|
|
705
|
27 |
|
if ($this->enableAutoLogin) { |
706
|
2 |
|
if ($this->getIsGuest()) { |
707
|
2 |
|
$this->loginByCookie(); |
708
|
1 |
|
} elseif ($this->autoRenewCookie) { |
709
|
|
|
$this->renewIdentityCookie(); |
710
|
|
|
} |
711
|
|
|
} |
712
|
27 |
|
} |
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* Checks if the user can perform the operation as specified by the given permission. |
716
|
|
|
* |
717
|
|
|
* Note that you must configure "authManager" application component in order to use this method. |
718
|
|
|
* Otherwise it will always return false. |
719
|
|
|
* |
720
|
|
|
* @param string $permissionName the name of the permission (e.g. "edit post") that needs access check. |
721
|
|
|
* @param array $params name-value pairs that would be passed to the rules associated |
722
|
|
|
* with the roles and permissions assigned to the user. |
723
|
|
|
* @param bool $allowCaching whether to allow caching the result of access check. |
724
|
|
|
* When this parameter is true (default), if the access check of an operation was performed |
725
|
|
|
* before, its result will be directly returned when calling this method to check the same |
726
|
|
|
* operation. If this parameter is false, this method will always call |
727
|
|
|
* [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this |
728
|
|
|
* caching is effective only within the same request and only works when `$params = []`. |
729
|
|
|
* @return bool whether the user can perform the operation as specified by the given permission. |
730
|
|
|
*/ |
731
|
20 |
|
public function can($permissionName, $params = [], $allowCaching = true) |
732
|
|
|
{ |
733
|
20 |
|
if ($allowCaching && empty($params) && isset($this->_access[$permissionName])) { |
734
|
|
|
return $this->_access[$permissionName]; |
735
|
|
|
} |
736
|
20 |
|
if (($accessChecker = $this->getAccessChecker()) === null) { |
737
|
|
|
return false; |
738
|
|
|
} |
739
|
20 |
|
$access = $accessChecker->checkAccess($this->getId(), $permissionName, $params); |
740
|
20 |
|
if ($allowCaching && empty($params)) { |
741
|
8 |
|
$this->_access[$permissionName] = $access; |
742
|
|
|
} |
743
|
|
|
|
744
|
20 |
|
return $access; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Checks if the `Accept` header contains a content type that allows redirection to the login page. |
749
|
|
|
* The login page is assumed to serve `text/html` or `application/xhtml+xml` by default. You can change acceptable |
750
|
|
|
* content types by modifying [[acceptableRedirectTypes]] property. |
751
|
|
|
* @return bool whether this request may be redirected to the login page. |
752
|
|
|
* @see acceptableRedirectTypes |
753
|
|
|
* @since 2.0.8 |
754
|
|
|
*/ |
755
|
2 |
|
protected function checkRedirectAcceptable() |
756
|
|
|
{ |
757
|
2 |
|
$acceptableTypes = Yii::$app->getRequest()->getAcceptableContentTypes(); |
758
|
2 |
|
if (empty($acceptableTypes) || count($acceptableTypes) === 1 && array_keys($acceptableTypes)[0] === '*/*') { |
759
|
1 |
|
return true; |
760
|
|
|
} |
761
|
|
|
|
762
|
2 |
|
foreach ($acceptableTypes as $type => $params) { |
763
|
2 |
|
if (in_array($type, $this->acceptableRedirectTypes, true)) { |
764
|
2 |
|
return true; |
765
|
|
|
} |
766
|
|
|
} |
767
|
|
|
|
768
|
2 |
|
return false; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* Returns auth manager associated with the user component. |
773
|
|
|
* |
774
|
|
|
* By default this is the `authManager` application component. |
775
|
|
|
* You may override this method to return a different auth manager instance if needed. |
776
|
|
|
* @return \yii\rbac\ManagerInterface |
777
|
|
|
* @since 2.0.6 |
778
|
|
|
* @deprecated since version 2.0.9, to be removed in 2.1. Use [[getAccessChecker()]] instead. |
779
|
|
|
*/ |
780
|
|
|
protected function getAuthManager() |
781
|
|
|
{ |
782
|
|
|
return Yii::$app->getAuthManager(); |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Returns the access checker used for checking access. |
787
|
|
|
* @return CheckAccessInterface |
788
|
|
|
* @since 2.0.9 |
789
|
|
|
*/ |
790
|
20 |
|
protected function getAccessChecker() |
791
|
|
|
{ |
792
|
20 |
|
return $this->accessChecker !== null ? $this->accessChecker : $this->getAuthManager(); |
|
|
|
|
793
|
|
|
} |
794
|
|
|
} |
795
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.