dynamic authTimeout: not overriding previous authT

2019-08-20 21:45发布

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.

标签: php yii2
0条回答
登录 后发表回答