yii\web\User::switchIdentity PHP 메소드

switchIdentity() 공개 메소드

When [[enableSession]] is true, this method may use session and/or cookie to store the user identity information, according to the value of $duration. Please refer to User::login for more details. This method is mainly called by User::login, User::logout and User::loginByCookie when the current user needs to be associated with the corresponding identity information.
public switchIdentity ( yii\web\IdentityInterface | null $identity, integer $duration )
$identity yii\web\IdentityInterface | null the identity information to be associated with the current user. If null, it means switching the current user to be a guest.
$duration integer number of seconds that the user can remain in logged-in status. This parameter is used only when `$identity` is not null.
    public function switchIdentity($identity, $duration = 0)
    {
        $this->setIdentity($identity);
        if (!$this->enableSession) {
            return;
        }
        /* Ensure any existing identity cookies are removed. */
        if ($this->enableAutoLogin) {
            $this->removeIdentityCookie();
        }
        $session = Yii::$app->getSession();
        if (!YII_ENV_TEST) {
            $session->regenerateID(true);
        }
        $session->remove($this->idParam);
        $session->remove($this->authTimeoutParam);
        if ($identity) {
            $session->set($this->idParam, $identity->getId());
            if ($this->authTimeout !== null) {
                $session->set($this->authTimeoutParam, time() + $this->authTimeout);
            }
            if ($this->absoluteAuthTimeout !== null) {
                $session->set($this->absoluteAuthTimeoutParam, time() + $this->absoluteAuthTimeout);
            }
            if ($duration > 0 && $this->enableAutoLogin) {
                $this->sendIdentityCookie($identity, $duration);
            }
        }
    }