public function actionSwitch($id = null)
    {
        if (!$this->module->enableImpersonateUser) {
            throw new ForbiddenHttpException(Yii::t('user', 'Impersonate user is disabled in the application configuration'));
        }

        if(!$id && Yii::$app->session->has(self::ORIGINAL_USER_SESSION_KEY)) {
            $user = $this->findModel(Yii::$app->session->get(self::ORIGINAL_USER_SESSION_KEY));

            Yii::$app->session->remove(self::ORIGINAL_USER_SESSION_KEY);
        } else {
            if (!Yii::$app->user->identity->isAdmin) {
                throw new ForbiddenHttpException;
            }

            $user = $this->findModel($id);
            Yii::$app->session->set(self::ORIGINAL_USER_SESSION_KEY, Yii::$app->user->id);
        }

        $event = $this->getUserEvent($user);

        $this->trigger(self::EVENT_BEFORE_IMPERSONATE, $event);
        
        Yii::$app->user->switchIdentity($user, 3600);
        
        $this->trigger(self::EVENT_AFTER_IMPERSONATE, $event);

        return $this->goHome();
    }
public function actionSwitch($id = null)
    {
        if (!$this->module->enableImpersonateUser) {
            throw new ForbiddenHttpException(Yii::t('user', 'Impersonate user is disabled in the application configuration'));
        }

        if(!$id && Yii::$app->session->has(self::ORIGINAL_USER_SESSION_KEY)) {
            $user = $this->findModel(Yii::$app->session->get(self::ORIGINAL_USER_SESSION_KEY));

            Yii::$app->session->remove(self::ORIGINAL_USER_SESSION_KEY);
        } else {
            if (!Yii::$app->user->identity->isAdmin) {
                throw new ForbiddenHttpException;
            }

            $user = $this->findModel($id);
            Yii::$app->session->set(self::ORIGINAL_USER_SESSION_KEY, Yii::$app->user->id);
        }

        $event = $this->getUserEvent($user);

        $this->trigger(self::EVENT_BEFORE_IMPERSONATE, $event);
        
        Yii::$app->user->switchIdentity($user, 3600);
        
        $this->trigger(self::EVENT_AFTER_IMPERSONATE, $event);

        return $this->goHome();
    }
public function actionSwitch ( $ id = null ) { if ( ! $ this -> module -> enableImpersonateUser ) { throw new ForbiddenHttpException ( Yii :: t ( 'user' , 'Impersonate user is disabled in the application configuration' ) ) ; } if ( ! $ id && Yii :: $ app -> session -> has ( self :: ORIGINAL_USER_SESSION_KEY ) ) { $ user = $ this -> findModel ( Yii :: $ app -> session -> get ( self :: ORIGINAL_USER_SESSION_KEY ) ) ; Yii :: $ app -> session -> remove ( self :: ORIGINAL_USER_SESSION_KEY ) ; } else { if ( ! Yii :: $ app -> user -> identity -> isAdmin ) { throw new ForbiddenHttpException ; } $ user = $ this -> findModel ( $ id ) ; Yii :: $ app -> session -> set ( self :: ORIGINAL_USER_SESSION_KEY , Yii :: $ app -> user -> id ) ; } $ event = $ this -> getUserEvent ( $ user ) ; $ this -> trigger ( self :: EVENT_BEFORE_IMPERSONATE , $ event ) ; Yii :: $ app -> user -> switchIdentity ( $ user , 3600 ) ; $ this -> trigger ( self :: EVENT_AFTER_IMPERSONATE , $ event ) ; return $ this -> goHome ( ) ; }
Switches to the given user for the rest of the Session.
When no id is given, we switch back to the original admin user
that started the impersonation.

@param int $id

@return string
Switches to the given user for the rest of the Session . When no id is given we switch back to the original admin user that started the impersonation .