0

I was looking to make dynamic authTimeout for users. So that user can set their timeout frequency.

I came across Yii2 - How to set dynamic authTimeout in User Identity?

And, then I changed web.php configuration

'user' => [
  'identityClass' => 'app\models\Users',
  'loginUrl' => ['login'],
  'authTimeout' => $params['LOGIN_TIMEOUT_DURATION'], //default 3600
  'returnUrlParam' => 'return_url'
],

to

'user' => [
  'class' => app\models\WebUser::class,
  'identityClass' => 'app\models\Users',
  'loginUrl' => ['login'],
  'authTimeout' => $params['LOGIN_TIMEOUT_DURATION'], //default 3600
  'returnUrlParam' => 'return_url'
],

WebUser.php

<?php
namespace app\models;
use Yii;

class WebUser extends \yii\web\User {
    public function init() {
        if(isset(Yii::$app->params['authTimeout'])){
            if($this->authTimeout != Yii::$app->params['authTimeout']){
                $logout_time = Yii::$app->params['authTimeout'];
                $this->authTimeout = $logout_time;
            }
        }
    }    
}

Then, used WebUser.php in Settings Class

Settings.php

<?php
namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\models\MyRecord;

class Settings extends MyRecord 
{

  public static function tableName(){
    return '{{%table_name}}';
  }

  private $c;
  public function __construct(){
    $this->c = new WebUser;
  }

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


}

So, basically when any user set their timeout frequency, I store that value in params variable 'authTimeout' (like Yii::$app->params['authTimeout']). Then, I use it in WebUser class to override the previous authTimeout value.

But, It's not working as it's taking the initial authTimeout value of 'authTimeout' => $params['LOGIN_TIMEOUT_DURATION'], of web.php.

I checked Yii::$app->params['authTimeout'] value is coming to WebUser class, but somehow it's not overriding the previous values.

Testing:

<?php
namespace app\models;
use Yii;

class WebUser extends \yii\web\User {
    public function init() {
        if(isset(Yii::$app->params['authTimeout'])){
            if($this->authTimeout != Yii::$app->params['authTimeout']){
                $logout_time = Yii::$app->params['authTimeout'];
                echo $logout_time; //printing 60 as I set it to 60 dynamically
                $this->authTimeout = $logout_time;
            }
        }
        print_r($this); die;
    }    
}

Output:

app\models\WebUser Object
(
    [identityClass] => app\models\Users
    [enableAutoLogin] => 
    [enableSession] => 1
    [loginUrl] => Array
        (
            [0] => login
        )

    [identityCookie] => Array
        (
            [name] => _identity
            [httpOnly] => 1
        )

    [authTimeout] => 3600
    [accessChecker] => 
    [absoluteAuthTimeout] => 
    [autoRenewCookie] => 1
    [idParam] => __id
    [authTimeoutParam] => __expire
    [absoluteAuthTimeoutParam] => __absoluteExpire
    [returnUrlParam] => return_url
    [acceptableRedirectTypes] => Array

And, it works if I directly set any frequency in authTimeout variable. It logout me to login page.

<?php
namespace app\modules\settings\models;
use Yii;

class WebUser extends \yii\web\User {
    public function init() {
        $this->authTimeout = 30;
    }    
}

[When a user pick it's timeout frequency from dropdown. I store that value in a session, then assign it to a variable Yii::$app->params['authTimeout'] and uses this variable for this functionality.]

I'm not able to find out the issue.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • `Yii::$app->params['authTimeout']` is not persistent - value set in one request will not be available in next request, you should not be use it for storing any setting from user (this is not a storage in the first place). You need to save these settings in database, like in example from https://stackoverflow.com/questions/52423705/yii2-how-to-set-dynamic-authtimeout-in-user-identity – rob006 Jun 25 '19 at 15:15
  • If you want different values for different user, you may try overriding `switchIdentity()` and setting `authTimeout` based on passed `$identity`. But this is much more tricky - you can easily get chicken-egg problem. – rob006 Jun 25 '19 at 15:31

0 Answers0