Issues (910)

framework/web/User.php (6 issues)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://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\di\Instance;
15
use yii\rbac\CheckAccessInterface;
16
17
/**
18
 * User is the class for the `user` application component that manages the user authentication status.
19
 *
20
 * You may use [[isGuest]] to determine whether the current user is a guest or not.
21
 * If the user is a guest, the [[identity]] property would return `null`. Otherwise, it would
22
 * be an instance of [[IdentityInterface]].
23
 *
24
 * You may call various methods to change the user authentication status:
25
 *
26
 * - [[login()]]: sets the specified identity and remembers the authentication status in session and cookie;
27
 * - [[logout()]]: marks the user as a guest and clears the relevant information from session and cookie;
28
 * - [[setIdentity()]]: changes the user identity without touching session or cookie
29
 *   (this is best used in stateless RESTful API implementation).
30
 *
31
 * Note that User only maintains the user authentication status. It does NOT handle how to authenticate
32
 * a user. The logic of how to authenticate a user should be done in the class implementing [[IdentityInterface]].
33
 * You are also required to set [[identityClass]] with the name of this class.
34
 *
35
 * User is configured as an application component in [[\yii\web\Application]] by default.
36
 * You can access that instance via `Yii::$app->user`.
37
 *
38
 * You can modify its configuration by adding an array to your application config under `components`
39
 * as it is shown in the following example:
40
 *
41
 * ```php
42
 * 'user' => [
43
 *     'identityClass' => 'app\models\User', // User must implement the IdentityInterface
44
 *     'enableAutoLogin' => true,
45
 *     // 'loginUrl' => ['user/login'],
46
 *     // ...
47
 * ]
48
 * ```
49
 *
50
 * @property-read string|int|null $id The unique identifier for the user. If `null`, it means the user is a
51
 * guest.
52
 * @property IdentityInterface|null $identity The identity object associated with the currently logged-in
53
 * user. `null` is returned if the user is not logged in (not authenticated).
54
 * @property-read bool $isGuest Whether the current user is a guest.
55
 * @property string $returnUrl The URL that the user should be redirected to after login. Note that the type
56
 * of this property differs in getter and setter. See [[getReturnUrl()]] and [[setReturnUrl()]] for details.
57
 *
58
 * @author Qiang Xue <[email protected]>
59
 * @since 2.0
60
 */
61
class User extends Component
62
{
63
    const EVENT_BEFORE_LOGIN = 'beforeLogin';
64
    const EVENT_AFTER_LOGIN = 'afterLogin';
65
    const EVENT_BEFORE_LOGOUT = 'beforeLogout';
66
    const EVENT_AFTER_LOGOUT = 'afterLogout';
67
68
    /**
69
     * @var string the class name of the [[identity]] object.
70
     */
71
    public $identityClass;
72
    /**
73
     * @var bool whether to enable cookie-based login. Defaults to `false`.
74
     * Note that this property will be ignored if [[enableSession]] is `false`.
75
     */
76
    public $enableAutoLogin = false;
77
    /**
78
     * @var bool whether to use session to persist authentication status across multiple requests.
79
     * You set this property to be `false` if your application is stateless, which is often the case
80
     * for RESTful APIs.
81
     */
82
    public $enableSession = true;
83
    /**
84
     * @var string|array|null the URL for login when [[loginRequired()]] is called.
85
     * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL.
86
     * The first element of the array should be the route to the login action, and the rest of
87
     * the name-value pairs are GET parameters used to construct the login URL. For example,
88
     *
89
     * ```php
90
     * ['site/login', 'ref' => 1]
91
     * ```
92
     *
93
     * If this property is `null`, a 403 HTTP exception will be raised when [[loginRequired()]] is called.
94
     */
95
    public $loginUrl = ['site/login'];
96
    /**
97
     * @var array the configuration of the identity cookie. This property is used only when [[enableAutoLogin]] is `true`.
98
     * @see Cookie
99
     */
100
    public $identityCookie = ['name' => '_identity', 'httpOnly' => true];
101
    /**
102
     * @var int|null the number of seconds in which the user will be logged out automatically if the user
103
     * remains inactive. If this property is not set, the user will be logged out after
104
     * the current session expires (c.f. [[Session::timeout]]).
105
     * Note that this will not work if [[enableAutoLogin]] is `true`.
106
     */
107
    public $authTimeout;
108
    /**
109
     * @var CheckAccessInterface|string|array|null The access checker object to use for checking access or the application
110
     * component ID of the access checker.
111
     * If not set the application auth manager will be used.
112
     * @since 2.0.9
113
     */
114
    public $accessChecker;
115
    /**
116
     * @var int|null the number of seconds in which the user will be logged out automatically
117
     * regardless of activity.
118
     * Note that this will not work if [[enableAutoLogin]] is `true`.
119
     */
120
    public $absoluteAuthTimeout;
121
    /**
122
     * @var bool whether to automatically renew the identity cookie each time a page is requested.
123
     * This property is effective only when [[enableAutoLogin]] is `true`.
124
     * When this is `false`, the identity cookie will expire after the specified duration since the user
125
     * is initially logged in. When this is `true`, the identity cookie will expire after the specified duration
126
     * since the user visits the site the last time.
127
     * @see enableAutoLogin
128
     */
129
    public $autoRenewCookie = true;
130
    /**
131
     * @var string the session variable name used to store the value of [[id]].
132
     */
133
    public $idParam = '__id';
134
    /**
135
     * @var string the session variable name used to store authentication key.
136
     * @since 2.0.41
137
     */
138
    public $authKeyParam = '__authKey';
139
    /**
140
     * @var string the session variable name used to store the value of expiration timestamp of the authenticated state.
141
     * This is used when [[authTimeout]] is set.
142
     */
143
    public $authTimeoutParam = '__expire';
144
    /**
145
     * @var string the session variable name used to store the value of absolute expiration timestamp of the authenticated state.
146
     * This is used when [[absoluteAuthTimeout]] is set.
147
     */
148
    public $absoluteAuthTimeoutParam = '__absoluteExpire';
149
    /**
150
     * @var string the session variable name used to store the value of [[returnUrl]].
151
     */
152
    public $returnUrlParam = '__returnUrl';
153
    /**
154
     * @var array MIME types for which this component should redirect to the [[loginUrl]].
155
     * @since 2.0.8
156
     */
157
    public $acceptableRedirectTypes = ['text/html', 'application/xhtml+xml'];
158
159
    private $_access = [];
160
161
162
    /**
163
     * Initializes the application component.
164
     */
165 115
    public function init()
166
    {
167 115
        parent::init();
168
169 115
        if ($this->identityClass === null) {
170
            throw new InvalidConfigException('User::identityClass must be set.');
171
        }
172 115
        if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) {
173
            throw new InvalidConfigException('User::identityCookie must contain the "name" element.');
174
        }
175 115
        if ($this->accessChecker !== null) {
176 1
            $this->accessChecker = Instance::ensure($this->accessChecker, '\yii\rbac\CheckAccessInterface');
177
        }
178
    }
179
180
    private $_identity = false;
181
182
    /**
183
     * Returns the identity object associated with the currently logged-in user.
184
     * When [[enableSession]] is true, this method may attempt to read the user's authentication data
185
     * stored in session and reconstruct the corresponding identity object, if it has not done so before.
186
     * @param bool $autoRenew whether to automatically renew authentication status if it has not been done so before.
187
     * This is only useful when [[enableSession]] is true.
188
     * @return IdentityInterface|null the identity object associated with the currently logged-in user.
189
     * `null` is returned if the user is not logged in (not authenticated).
190
     * @see login()
191
     * @see logout()
192
     */
193 96
    public function getIdentity($autoRenew = true)
194
    {
195 96
        if ($this->_identity === false) {
196 40
            if ($this->enableSession && $autoRenew) {
197
                try {
198 38
                    $this->_identity = null;
199 38
                    $this->renewAuthStatus();
200 1
                } catch (\Exception $e) {
201 1
                    $this->_identity = false;
202 1
                    throw $e;
203
                } catch (\Throwable $e) {
204
                    $this->_identity = false;
205 37
                    throw $e;
206
                }
207
            } else {
208 2
                return null;
209
            }
210
        }
211
212 93
        return $this->_identity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_identity also could return the type true which is incompatible with the documented return type null|yii\web\IdentityInterface.
Loading history...
213
    }
214
215
    /**
216
     * Sets the user identity object.
217
     *
218
     * Note that this method does not deal with session or cookie. You should usually use [[switchIdentity()]]
219
     * to change the identity of the current user.
220
     *
221
     * @param IdentityInterface|null $identity the identity object associated with the currently logged user.
222
     * If null, it means the current user will be a guest without any associated identity.
223
     * @throws InvalidValueException if `$identity` object does not implement [[IdentityInterface]].
224
     */
225 93
    public function setIdentity($identity)
226
    {
227 93
        if ($identity instanceof IdentityInterface) {
228 62
            $this->_identity = $identity;
229 42
        } elseif ($identity === null) {
230 42
            $this->_identity = null;
231
        } else {
232 1
            throw new InvalidValueException('The identity object must implement IdentityInterface.');
233
        }
234 93
        $this->_access = [];
235
    }
236
237
    /**
238
     * Logs in a user.
239
     *
240
     * After logging in a user:
241
     * - the user's identity information is obtainable from the [[identity]] property
242
     *
243
     * If [[enableSession]] is `true`:
244
     * - the identity information will be stored in session and be available in the next requests
245
     * - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser
246
     * - in case of `$duration > 0`: as long as the session remains active or as long as the cookie
247
     *   remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`.
248
     *
249
     * If [[enableSession]] is `false`:
250
     * - the `$duration` parameter will be ignored
251
     *
252
     * @param IdentityInterface $identity the user identity (which should already be authenticated)
253
     * @param int $duration number of seconds that the user can remain in logged-in status, defaults to `0`
254
     * @return bool whether the user is logged in
255
     */
256 43
    public function login(IdentityInterface $identity, $duration = 0)
257
    {
258 43
        if ($this->beforeLogin($identity, false, $duration)) {
259 43
            $this->switchIdentity($identity, $duration);
260 43
            $id = $identity->getId();
261 43
            $ip = Yii::$app->getRequest()->getUserIP();
262 43
            if ($this->enableSession) {
263 43
                $log = "User '$id' logged in from $ip with duration $duration.";
264
            } else {
265
                $log = "User '$id' logged in from $ip. Session not enabled.";
266
            }
267
268 43
            $this->regenerateCsrfToken();
269
270 43
            Yii::info($log, __METHOD__);
271 43
            $this->afterLogin($identity, false, $duration);
272
        }
273
274 43
        return !$this->getIsGuest();
275
    }
276
277
    /**
278
     * Regenerates CSRF token
279
     *
280
     * @since 2.0.14.2
281
     */
282 43
    protected function regenerateCsrfToken()
283
    {
284 43
        $request = Yii::$app->getRequest();
285 43
        if ($request->enableCsrfCookie || $this->enableSession) {
0 ignored issues
show
Bug Best Practice introduced by
The property enableCsrfCookie does not exist on yii\console\Request. Since you implemented __get, consider adding a @property annotation.
Loading history...
286 43
            $request->getCsrfToken(true);
287
        }
288
    }
289
290
    /**
291
     * Logs in a user by the given access token.
292
     * This method will first authenticate the user by calling [[IdentityInterface::findIdentityByAccessToken()]]
293
     * with the provided access token. If successful, it will call [[login()]] to log in the authenticated user.
294
     * If authentication fails or [[login()]] is unsuccessful, it will return null.
295
     * @param string $token the access token
296
     * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
297
     * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
298
     * @return IdentityInterface|null the identity associated with the given access token. Null is returned if
299
     * the access token is invalid or [[login()]] is unsuccessful.
300
     */
301 42
    public function loginByAccessToken($token, $type = null)
302
    {
303
        /* @var $class IdentityInterface */
304 42
        $class = $this->identityClass;
305 42
        $identity = $class::findIdentityByAccessToken($token, $type);
306 42
        if ($identity && $this->login($identity)) {
307 27
            return $identity;
308
        }
309
310 15
        return null;
311
    }
312
313
    /**
314
     * Logs in a user by cookie.
315
     *
316
     * This method attempts to log in a user using the ID and authKey information
317
     * provided by the [[identityCookie|identity cookie]].
318
     */
319 2
    protected function loginByCookie()
320
    {
321 2
        $data = $this->getIdentityAndDurationFromCookie();
322 2
        if (isset($data['identity'], $data['duration'])) {
323 1
            $identity = $data['identity'];
324 1
            $duration = $data['duration'];
325 1
            if ($this->beforeLogin($identity, true, $duration)) {
326 1
                $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0);
327 1
                $id = $identity->getId();
328 1
                $ip = Yii::$app->getRequest()->getUserIP();
329 1
                Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__);
330 1
                $this->afterLogin($identity, true, $duration);
331
            }
332
        }
333
    }
334
335
    /**
336
     * Logs out the current user.
337
     * This will remove authentication-related session data.
338
     * If `$destroySession` is true, all session data will be removed.
339
     * @param bool $destroySession whether to destroy the whole session. Defaults to true.
340
     * This parameter is ignored if [[enableSession]] is false.
341
     * @return bool whether the user is logged out
342
     */
343 1
    public function logout($destroySession = true)
344
    {
345 1
        $identity = $this->getIdentity();
346 1
        if ($identity !== null && $this->beforeLogout($identity)) {
347 1
            $this->switchIdentity(null);
348 1
            $id = $identity->getId();
349 1
            $ip = Yii::$app->getRequest()->getUserIP();
350 1
            Yii::info("User '$id' logged out from $ip.", __METHOD__);
351 1
            if ($destroySession && $this->enableSession) {
352
                Yii::$app->getSession()->destroy();
353
            }
354 1
            $this->afterLogout($identity);
355
        }
356
357 1
        return $this->getIsGuest();
358
    }
359
360
    /**
361
     * Returns a value indicating whether the user is a guest (not authenticated).
362
     * @return bool whether the current user is a guest.
363
     * @see getIdentity()
364
     */
365 44
    public function getIsGuest()
366
    {
367 44
        return $this->getIdentity() === null;
368
    }
369
370
    /**
371
     * Returns a value that uniquely represents the user.
372
     * @return string|int|null the unique identifier for the user. If `null`, it means the user is a guest.
373
     * @see getIdentity()
374
     */
375 84
    public function getId()
376
    {
377 84
        $identity = $this->getIdentity();
378
379 84
        return $identity !== null ? $identity->getId() : null;
380
    }
381
382
    /**
383
     * Returns the URL that the browser should be redirected to after successful login.
384
     *
385
     * This method reads the return URL from the session. It is usually used by the login action which
386
     * may call this method to redirect the browser to where it goes after successful authentication.
387
     *
388
     * @param string|array|null $defaultUrl the default return URL in case it was not set previously.
389
     * If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to.
390
     * Please refer to [[setReturnUrl()]] on accepted format of the URL.
391
     * @return string the URL that the user should be redirected to after login.
392
     * @see loginRequired()
393
     */
394 3
    public function getReturnUrl($defaultUrl = null)
395
    {
396 3
        $url = Yii::$app->getSession()->get($this->returnUrlParam, $defaultUrl);
397 3
        if (is_array($url)) {
398
            if (isset($url[0])) {
399
                return Yii::$app->getUrlManager()->createUrl($url);
400
            }
401
402
            $url = null;
403
        }
404
405 3
        return $url === null ? Yii::$app->getHomeUrl() : $url;
406
    }
407
408
    /**
409
     * Remembers the URL in the session so that it can be retrieved back later by [[getReturnUrl()]].
410
     * @param string|array $url the URL that the user should be redirected to after login.
411
     * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL.
412
     * The first element of the array should be the route, and the rest of
413
     * the name-value pairs are GET parameters used to construct the URL. For example,
414
     *
415
     * ```php
416
     * ['admin/index', 'ref' => 1]
417
     * ```
418
     */
419 3
    public function setReturnUrl($url)
420
    {
421 3
        Yii::$app->getSession()->set($this->returnUrlParam, $url);
422
    }
423
424
    /**
425
     * Redirects the user browser to the login page.
426
     *
427
     * Before the redirection, the current URL (if it's not an AJAX url) will be kept as [[returnUrl]] so that
428
     * the user browser may be redirected back to the current page after successful login.
429
     *
430
     * Make sure you set [[loginUrl]] so that the user browser can be redirected to the specified login URL after
431
     * calling this method.
432
     *
433
     * Note that when [[loginUrl]] is set, calling this method will NOT terminate the application execution.
434
     *
435
     * @param bool $checkAjax whether to check if the request is an AJAX request. When this is true and the request
436
     * is an AJAX request, the current URL (for AJAX request) will NOT be set as the return URL.
437
     * @param bool $checkAcceptHeader whether to check if the request accepts HTML responses. Defaults to `true`. When this is true and
438
     * the request does not accept HTML responses the current URL will not be SET as the return URL. Also instead of
439
     * redirecting the user an ForbiddenHttpException is thrown. This parameter is available since version 2.0.8.
440
     * @return Response the redirection response if [[loginUrl]] is set
441
     * @throws ForbiddenHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set or a redirect is
442
     * not applicable.
443
     */
444 2
    public function loginRequired($checkAjax = true, $checkAcceptHeader = true)
445
    {
446 2
        $request = Yii::$app->getRequest();
447 2
        $canRedirect = !$checkAcceptHeader || $this->checkRedirectAcceptable();
448
        if (
449 2
            $this->enableSession
450 2
            && $request->getIsGet()
451 2
            && (!$checkAjax || !$request->getIsAjax())
452
            && $canRedirect
453
        ) {
454 1
            $this->setReturnUrl($request->getAbsoluteUrl());
455
        }
456 2
        if ($this->loginUrl !== null && $canRedirect) {
457 1
            $loginUrl = (array) $this->loginUrl;
458 1
            if ($loginUrl[0] !== Yii::$app->requestedRoute) {
459 1
                return Yii::$app->getResponse()->redirect($this->loginUrl);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Yii::app->getResp...direct($this->loginUrl) also could return the type yii\console\Response which is incompatible with the documented return type yii\web\Response.
Loading history...
460
            }
461
        }
462 2
        throw new ForbiddenHttpException(Yii::t('yii', 'Login Required'));
463
    }
464
465
    /**
466
     * This method is called before logging in a user.
467
     * The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event.
468
     * If you override this method, make sure you call the parent implementation
469
     * so that the event is triggered.
470
     * @param IdentityInterface $identity the user identity information
471
     * @param bool $cookieBased whether the login is cookie-based
472
     * @param int $duration number of seconds that the user can remain in logged-in status.
473
     * If 0, it means login till the user closes the browser or the session is manually destroyed.
474
     * @return bool whether the user should continue to be logged in
475
     */
476 43
    protected function beforeLogin($identity, $cookieBased, $duration)
477
    {
478 43
        $event = new UserEvent([
479 43
            'identity' => $identity,
480 43
            'cookieBased' => $cookieBased,
481 43
            'duration' => $duration,
482 43
        ]);
483 43
        $this->trigger(self::EVENT_BEFORE_LOGIN, $event);
484
485 43
        return $event->isValid;
486
    }
487
488
    /**
489
     * This method is called after the user is successfully logged in.
490
     * The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event.
491
     * If you override this method, make sure you call the parent implementation
492
     * so that the event is triggered.
493
     * @param IdentityInterface $identity the user identity information
494
     * @param bool $cookieBased whether the login is cookie-based
495
     * @param int $duration number of seconds that the user can remain in logged-in status.
496
     * If 0, it means login till the user closes the browser or the session is manually destroyed.
497
     */
498 43
    protected function afterLogin($identity, $cookieBased, $duration)
499
    {
500 43
        $this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent([
501 43
            'identity' => $identity,
502 43
            'cookieBased' => $cookieBased,
503 43
            'duration' => $duration,
504 43
        ]));
505
    }
506
507
    /**
508
     * This method is invoked when calling [[logout()]] to log out a user.
509
     * The default implementation will trigger the [[EVENT_BEFORE_LOGOUT]] event.
510
     * If you override this method, make sure you call the parent implementation
511
     * so that the event is triggered.
512
     * @param IdentityInterface $identity the user identity information
513
     * @return bool whether the user should continue to be logged out
514
     */
515 1
    protected function beforeLogout($identity)
516
    {
517 1
        $event = new UserEvent([
518 1
            'identity' => $identity,
519 1
        ]);
520 1
        $this->trigger(self::EVENT_BEFORE_LOGOUT, $event);
521
522 1
        return $event->isValid;
523
    }
524
525
    /**
526
     * This method is invoked right after a user is logged out via [[logout()]].
527
     * The default implementation will trigger the [[EVENT_AFTER_LOGOUT]] event.
528
     * If you override this method, make sure you call the parent implementation
529
     * so that the event is triggered.
530
     * @param IdentityInterface $identity the user identity information
531
     */
532 1
    protected function afterLogout($identity)
533
    {
534 1
        $this->trigger(self::EVENT_AFTER_LOGOUT, new UserEvent([
535 1
            'identity' => $identity,
536 1
        ]));
537
    }
538
539
    /**
540
     * Renews the identity cookie.
541
     * This method will set the expiration time of the identity cookie to be the current time
542
     * plus the originally specified cookie duration.
543
     */
544
    protected function renewIdentityCookie()
545
    {
546
        $name = $this->identityCookie['name'];
547
        $value = Yii::$app->getRequest()->getCookies()->getValue($name);
548
        if ($value !== null) {
549
            $data = json_decode($value, true);
550
            if (is_array($data) && isset($data[2])) {
551
                $cookie = Yii::createObject(array_merge($this->identityCookie, [
552
                    'class' => 'yii\web\Cookie',
553
                    'value' => $value,
554
                    'expire' => time() + (int) $data[2],
555
                ]));
556
                Yii::$app->getResponse()->getCookies()->add($cookie);
557
            }
558
        }
559
    }
560
561
    /**
562
     * Sends an identity cookie.
563
     * This method is used when [[enableAutoLogin]] is true.
564
     * It saves [[id]], [[IdentityInterface::getAuthKey()|auth key]], and the duration of cookie-based login
565
     * information in the cookie.
566
     * @param IdentityInterface $identity
567
     * @param int $duration number of seconds that the user can remain in logged-in status.
568
     * @see loginByCookie()
569
     */
570 2
    protected function sendIdentityCookie($identity, $duration)
571
    {
572 2
        $cookie = Yii::createObject(array_merge($this->identityCookie, [
573 2
            'class' => 'yii\web\Cookie',
574 2
            'value' => json_encode([
575 2
                $identity->getId(),
576 2
                $identity->getAuthKey(),
577 2
                $duration,
578 2
            ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
579 2
            'expire' => time() + $duration,
580 2
        ]));
581 2
        Yii::$app->getResponse()->getCookies()->add($cookie);
582
    }
583
584
    /**
585
     * Determines if an identity cookie has a valid format and contains a valid auth key.
586
     * This method is used when [[enableAutoLogin]] is true.
587
     * This method attempts to authenticate a user using the information in the identity cookie.
588
     * @return array|null Returns an array of 'identity' and 'duration' if valid, otherwise null.
589
     * @see loginByCookie()
590
     * @since 2.0.9
591
     */
592 2
    protected function getIdentityAndDurationFromCookie()
593
    {
594 2
        $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']);
595 2
        if ($value === null) {
596
            return null;
597
        }
598 2
        $data = json_decode($value, true);
599 2
        if (is_array($data) && count($data) == 3) {
600 1
            list($id, $authKey, $duration) = $data;
601
            /* @var $class IdentityInterface */
602 1
            $class = $this->identityClass;
603 1
            $identity = $class::findIdentity($id);
604 1
            if ($identity !== null) {
605 1
                if (!$identity instanceof IdentityInterface) {
606
                    throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface.");
607 1
                } elseif (!$identity->validateAuthKey($authKey)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identity->validateAuthKey($authKey) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
608
                    $ip = Yii::$app->getRequest()->getUserIP();
609
                    Yii::warning("Invalid cookie auth key attempted for user '$id' from $ip: $authKey", __METHOD__);
610
                } else {
611 1
                    return ['identity' => $identity, 'duration' => $duration];
612
                }
613
            }
614
        }
615 2
        $this->removeIdentityCookie();
616 2
        return null;
617
    }
618
619
    /**
620
     * Removes the identity cookie.
621
     * This method is used when [[enableAutoLogin]] is true.
622
     * @since 2.0.9
623
     */
624 2
    protected function removeIdentityCookie()
625
    {
626 2
        Yii::$app->getResponse()->getCookies()->remove(Yii::createObject(array_merge($this->identityCookie, [
627 2
            'class' => 'yii\web\Cookie',
628 2
        ])));
629
    }
630
631
    /**
632
     * Switches to a new identity for the current user.
633
     *
634
     * When [[enableSession]] is true, this method may use session and/or cookie to store the user identity information,
635
     * according to the value of `$duration`. Please refer to [[login()]] for more details.
636
     *
637
     * This method is mainly called by [[login()]], [[logout()]] and [[loginByCookie()]]
638
     * when the current user needs to be associated with the corresponding identity information.
639
     *
640
     * @param IdentityInterface|null $identity the identity information to be associated with the current user.
641
     * If null, it means switching the current user to be a guest.
642
     * @param int $duration number of seconds that the user can remain in logged-in status.
643
     * This parameter is used only when `$identity` is not null.
644
     */
645 45
    public function switchIdentity($identity, $duration = 0)
646
    {
647 45
        $this->setIdentity($identity);
648
649 45
        if (!$this->enableSession) {
650
            return;
651
        }
652
653
        /* Ensure any existing identity cookies are removed. */
654 45
        if ($this->enableAutoLogin && ($this->autoRenewCookie || $identity === null)) {
655 1
            $this->removeIdentityCookie();
656
        }
657
658 45
        $session = Yii::$app->getSession();
659 45
        $session->regenerateID(true);
660 45
        $session->remove($this->idParam);
661 45
        $session->remove($this->authTimeoutParam);
662 45
        $session->remove($this->authKeyParam);
663
664 45
        if ($identity) {
665 43
            $session->set($this->idParam, $identity->getId());
666 43
            $session->set($this->authKeyParam, $identity->getAuthKey());
667 43
            if ($this->authTimeout !== null) {
668 2
                $session->set($this->authTimeoutParam, time() + $this->authTimeout);
669
            }
670 43
            if ($this->absoluteAuthTimeout !== null) {
671
                $session->set($this->absoluteAuthTimeoutParam, time() + $this->absoluteAuthTimeout);
672
            }
673 43
            if ($this->enableAutoLogin && $duration > 0) {
674 2
                $this->sendIdentityCookie($identity, $duration);
675
            }
676
        }
677
    }
678
679
    /**
680
     * Updates the authentication status using the information from session and cookie.
681
     *
682
     * This method will try to determine the user identity using the [[idParam]] session variable.
683
     *
684
     * If [[authTimeout]] is set, this method will refresh the timer.
685
     *
686
     * If the user identity cannot be determined by session, this method will try to [[loginByCookie()|login by cookie]]
687
     * if [[enableAutoLogin]] is true.
688
     */
689 38
    protected function renewAuthStatus()
690
    {
691 38
        $session = Yii::$app->getSession();
692 38
        $id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($this->idParam) : null;
693
694 38
        if ($id === null) {
695 31
            $identity = null;
696
        } else {
697
            /* @var $class IdentityInterface */
698 8
            $class = $this->identityClass;
699 8
            $identity = $class::findIdentity($id);
700 7
            if ($identity === null) {
701 2
                $this->switchIdentity(null);
702
            }
703
        }
704
705 37
        if ($identity !== null) {
706 5
            $authKey = $session->get($this->authKeyParam);
707 5
            if ($authKey !== null && !$identity->validateAuthKey($authKey)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identity->validateAuthKey($authKey) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
708 1
                $identity = null;
709 1
                $ip = Yii::$app->getRequest()->getUserIP();
710 1
                Yii::warning("Invalid session auth key attempted for user '$id' from $ip: $authKey", __METHOD__);
711
            }
712
        }
713
714 37
        $this->setIdentity($identity);
715
716 37
        if ($identity !== null && ($this->authTimeout !== null || $this->absoluteAuthTimeout !== null)) {
717 2
            $expire = $this->authTimeout !== null ? $session->get($this->authTimeoutParam) : null;
718 2
            $expireAbsolute = $this->absoluteAuthTimeout !== null ? $session->get($this->absoluteAuthTimeoutParam) : null;
719 2
            if ($expire !== null && $expire < time() || $expireAbsolute !== null && $expireAbsolute < time()) {
720 1
                $this->logout(false);
721 2
            } elseif ($this->authTimeout !== null) {
722 2
                $session->set($this->authTimeoutParam, time() + $this->authTimeout);
723
            }
724
        }
725
726 37
        if ($this->enableAutoLogin) {
727 2
            if ($this->getIsGuest()) {
728 2
                $this->loginByCookie();
729 1
            } elseif ($this->autoRenewCookie) {
730
                $this->renewIdentityCookie();
731
            }
732
        }
733
    }
734
735
    /**
736
     * Checks if the user can perform the operation as specified by the given permission.
737
     *
738
     * Note that you must configure "authManager" application component in order to use this method.
739
     * Otherwise it will always return false.
740
     *
741
     * @param string $permissionName the name of the permission (e.g. "edit post") that needs access check.
742
     * @param array $params name-value pairs that would be passed to the rules associated
743
     * with the roles and permissions assigned to the user.
744
     * @param bool $allowCaching whether to allow caching the result of access check.
745
     * When this parameter is true (default), if the access check of an operation was performed
746
     * before, its result will be directly returned when calling this method to check the same
747
     * operation. If this parameter is false, this method will always call
748
     * [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this
749
     * caching is effective only within the same request and only works when `$params = []`.
750
     * @return bool whether the user can perform the operation as specified by the given permission.
751
     */
752 23
    public function can($permissionName, $params = [], $allowCaching = true)
753
    {
754 23
        if ($allowCaching && empty($params) && isset($this->_access[$permissionName])) {
755
            return $this->_access[$permissionName];
756
        }
757 23
        if (($accessChecker = $this->getAccessChecker()) === null) {
758
            return false;
759
        }
760 23
        $access = $accessChecker->checkAccess($this->getId(), $permissionName, $params);
761 23
        if ($allowCaching && empty($params)) {
762 10
            $this->_access[$permissionName] = $access;
763
        }
764
765 23
        return $access;
766
    }
767
768
    /**
769
     * Checks if the `Accept` header contains a content type that allows redirection to the login page.
770
     * The login page is assumed to serve `text/html` or `application/xhtml+xml` by default. You can change acceptable
771
     * content types by modifying [[acceptableRedirectTypes]] property.
772
     * @return bool whether this request may be redirected to the login page.
773
     * @see acceptableRedirectTypes
774
     * @since 2.0.8
775
     */
776 2
    public function checkRedirectAcceptable()
777
    {
778 2
        $acceptableTypes = Yii::$app->getRequest()->getAcceptableContentTypes();
779 2
        if (empty($acceptableTypes) || (count($acceptableTypes) === 1 && array_keys($acceptableTypes)[0] === '*/*')) {
780 1
            return true;
781
        }
782
783 2
        foreach ($acceptableTypes as $type => $params) {
784 2
            if (in_array($type, $this->acceptableRedirectTypes, true)) {
785 1
                return true;
786
            }
787
        }
788
789 2
        return false;
790
    }
791
792
    /**
793
     * Returns auth manager associated with the user component.
794
     *
795
     * By default this is the `authManager` application component.
796
     * You may override this method to return a different auth manager instance if needed.
797
     * @return \yii\rbac\ManagerInterface
798
     * @since 2.0.6
799
     * @deprecated since version 2.0.9, to be removed in 2.1. Use [[getAccessChecker()]] instead.
800
     */
801 2
    protected function getAuthManager()
802
    {
803 2
        return Yii::$app->getAuthManager();
804
    }
805
806
    /**
807
     * Returns the access checker used for checking access.
808
     * @return CheckAccessInterface
809
     * @since 2.0.9
810
     */
811 23
    protected function getAccessChecker()
812
    {
813 23
        return $this->accessChecker !== null ? $this->accessChecker : $this->getAuthManager();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->accessChec...$this->getAuthManager() also could return the type array|string which is incompatible with the documented return type yii\rbac\CheckAccessInterface.
Loading history...
814
    }
815
}
816