2015-04-23 47 views
1

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

这个工作正常,用户在设置的秒数后被'踢出'我的应用程序 - 但是我该如何修改这个以注销某些具有不同过期时间的用户'类型'。

例如,如果用户类型== 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秒钟后登录...任何想法?

+1

你就不能设置'通过覆盖'的init()''你的类WebUser'功能$ authTimeout'财产?您可以根据存储在您的配置中的值进行设置。在配置文件中不应该有任何逻辑,只是值。 –

+1

你应该在你的逻辑之后调用'parent :: init();'。它现在首先设置会话并在之后设置新的'$ authTimeout'。 –

+0

认为我怀疑它!谢谢 - prev dev对WebUser进行了一些更改,以表明它正在做与原始CWebUser完全不同的事情。例如,它从来不会为updateAuthStatus调用一个... – Zabs

回答

2

就像我在我的评论中说的。

我认为你应该可以覆盖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

+0

我已经覆盖这些值无济于事......我在配置文件中做错了什么? – Zabs