Your Location is: Home > Php

Yii2 How to logout User based on its type

From: Swaziland View: 2231 Milan Suthar 

Question

I want to make a specific type of use to be log out after certain amount of time and other should remain login until logout manually earlier it was

 'user' => [
            'class' => app\components\WebUser::class,
            'identityClass' => app\modules\ecosmob\auth\models\UserMaster::class,
            'enableAutoLogin' => false,
            'loginUrl' => ['/auth/auth/login']
            ]

along with below in webUser.php

public function init()
    {
        parent::init();

        $authTimeout = (new Query())
            ->select('gwc_value')
            ->from('global_web_config')
            ->where('gwc_key = :gwc_key', ['gwc_key' => 'session_timeout'])
            ->scalar();

        if ($authTimeout) {
            $this->authTimeout = ($authTimeout * 60) - 5;
        } else {
            $this->authTimeout = AUTH_TIMEOUT_DYNAMIC;
        }
    }

main.php

Yii::$app->view->registerMetaTag(['http-equiv' => 'refresh', 'content' => Yii::$app->user->authTimeout + 5]);

i've chaged it to revert functionality and make autologin for all users and make certain type of users logout using login duration but its not working as below

 'user' => [
            'class' => app\components\WebUser::class,
            'identityClass' => app\modules\ecosmob\auth\models\UserMaster::class,
            'enableAutoLogin' => true,
            'loginUrl' => ['/auth/auth/login'],
            'enableSession'=>true
        ],

and at login model

return Yii::$app->user->login($this->getUser(),  60);

Best answer

There might be different kind of solutions, but the first one which came to my mind is to react on the EVENT_BEFORE_LOGOUT event. There you can check if you want to logout the user and let the logout process to be finished (or not). Check yii\web\User::beforeLogout