【问题标题】:Auto-logout of session after x seconds for a user type (Yii 1.x)用户类型在 x 秒后自动注销会话(Yii 1.x)
【发布时间】:2015-04-23 11:36:38
【问题描述】:

我有一个 Yii 1.x 应用程序,它使用 WebUser 组件作为网站的登录部分 - 在我的 config/main.php 我的组件部分中有以下块,它将在 2 小时后自动超时会话(例如 3600 x 2 或 7200 秒)。

在用户在设定的秒数后被“踢出”我的应用程序的意义上,这很好用 - 但我将如何修改它以让此注销具有不同到期时间的某些“类型”用户。

例如,如果用户类型 == 1 则在 3600 秒后注销,如果用户类型 == 2 则在 7200 秒后注销...

// config/main.php
'components'        => array(
   'user'    => array(
       'class'   => 'application.components.WebUser',
       'allowAutoLogin' => true,
       'loginUrl'           => array('frontend/user/login'),
       'loginRequiredAjaxResponse' => 'CR_SESSION_EXPIRED',
       'authTimeout'       => 3600*2, // auto-logout after 2 hours
        ),
 .......

注意 - 这里使用的是 Yii 1.x 而不是 Yii 2.0。

我假设这需要在 WebUser 集成中而不是配置文件中..

--更新-- 我已将以下块添加到 WebUser.php 组件(扩展 CWebUser)

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

    if (($user = $this->getState('userModel')) !== null) {

        $this->authTimeout = 5;
        $this->absoluteAuthTimeout = 5;
        $this->setUserData(unserialize($user));
    }
}

我已将 authTimeout 和 absoluteAuthTimout 设置为 5 秒,但 5 秒后我仍然保持登录状态...有什么想法吗?

【问题讨论】:

  • 你不能通过覆盖WebUser 类的init() 函数来设置$authTimeout 属性吗?您可以基于存储在配置中的值。你的配置文件中不应该有任何逻辑,只有值。
  • 你应该在你的逻辑之后调用parent::init();。现在它首先设置会话,然后设置新的$authTimeout
  • 觉得我猜对了!!谢谢 - 上一个开发人员对 WebUser 进行了许多更改,它正在做一些与原始 CWebUser 完全不同的事情。例如,它从来没有为一个人调用 updateAuthStatus...
  • @JelledeFries 太棒了......现在这一切都说得通了 :-) 它是其中之一!感谢您的努力.. 非常感谢!

标签: php session yii yii-extensions yii-components


【解决方案1】:

就像我在评论中所说的那样。

我认为您应该能够覆盖 WebUser 类中的值。

<?php
class WebUser extends CWebUser{

    public $authTimeouts = array(); //array with the timeouts

    public function init(){
        //you need to get the userType first
        if(array_key_exists($userType,$this->authTimeouts)){ 
            $authTimeout = $this->authTimeouts[$userType];
        }
        parent::init();
    }
}

那么你的配置应该是这样的:

// config/main.php
'components'        => array(
   'user'    => array(
       'class'   => 'application.components.WebUser',
       'allowAutoLogin' => true,
       'loginUrl'           => array('frontend/user/login'),
       'loginRequiredAjaxResponse' => 'CR_SESSION_EXPIRED',
       'authTimeout'       => 3600*2, // auto-logout after 2 hours
       'authTimeouts'=> array(
            'userType1' => 10,
            'userType2' => 500,
            ),
        ),
 ......

类似的东西。 有关源代码和 init() 函数的更多信息,请参阅: https://github.com/yiisoft/yii/blob/1.1.16/framework/web/auth/CWebUser.php#L196

【讨论】:

  • 我已经覆盖这些值无济于事......我在配置文件中做错了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多