[英]Yii2 - How to set dynamic authTimeout in User Identity?
Here, I have extended User
Identity of Yii2. 在这里,我扩展了Yii2的
User
身份。
This is my configuration. 这是我的配置。
'user' => [
'identityClass' => app\models\UserMaster::class,
'enableAutoLogin' => false,
'loginUrl' => ['/auth/login'],
'authTimeout' => 86400
],
Here, I have defined authTimout
statically. 在这里,我已经静态定义了
authTimout
。 But, What I want to do is that I want to fetch timeout value from database and set it in authTimeout
. 但是,我想要做的是我想从数据库中获取超时值并在
authTimeout
设置它。
Thanks. 谢谢。
You can use event to set authTimeout
before request will be handled: 您可以在处理请求之前使用event设置
authTimeout
:
'as beforeRequest' => [
'class' => function (Event $event) {
/* @var $app \yii\web\Application */
$app = $event->sender;
$app->getUser()->authTimeout = (new Query())
->select('value')
->from('{{%settings}}')
->where('name = :name', ['name' => 'authTimeout'])
->scalar($app->getDb());
}
],
But probably more clear approach would be to create custom component and handle this in init()
. 但可能更明确的方法是创建自定义组件并在
init()
处理它。
class WebUser extends \yii\web\User {
public function init() {
parent::init();
$this->authTimeout = (new Query())
->select('value')
->from('{{%settings}}')
->where('name = :name', ['name' => 'authTimeout'])
->scalar();
}
}
Then use new component in your config: 然后在配置中使用新组件:
'components' => [
'user' => [
'class' => WebUser::class,
'identityClass' => app\models\UserMaster::class,
'enableAutoLogin' => false,
'loginUrl' => ['/auth/login'],
],
// ...
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.