在一个应用程序中定义两个不同的登录过

时间:2013-03-15 18:10:43

标签: php login yii identity

我的yii应用程序中的登录过程有问题。我有两种不同类型的用户,包括管理网站和客户的管理员用户。我正在使用CwebUserUserIdentitiy来管理这两个群组的登录。但我的问题是,当客户登录网站时,他也可以访问管理页面。我想要做的是说应用程序为它们创建两个不同的身份会话。我不想使用访问控制规则之类的东西。有没有办法在Yii风格中为它们创建不同的会话?

3 个答案:

答案 0 :(得分:8)

如果您不想使用其中一条评论中建议的复杂RBAC设置,那么如果您使用不同的stateKeyPrefix配置2个用户组件,则可以实现此目的:

'user' => array(              // Webuser for the frontend
    'class'             => 'CWebUser',
    'loginUrl'          => array('site/login'),
    'stateKeyPrefix'    => 'frontend_',
),
'adminUser' => array(         // Webuser for the admin area (admin)
    'class'             => 'CWebUser',
    'loginUrl'          => array('/admin/site/login'),
    'stateKeyPrefix'    => 'admin_',
),

现在您有2个独立的用户组件:useradminUser。您必须创建在AdminLoginForm中使用的另一个AdminUserIdentity/admin/site/login来进行身份验证。这应该是直截了当的。

虽然您仍然遇到问题,但您无法在管理模块中轻松定义accessRules,因为它会使用user组件执行授权检查,而您希望它使用adminUser组件。要解决此问题,您可以改变AdminModule中的组件配置,如下所示:

class AdminModule extends CWebModule
{
    public function beforeControllerAction($controller, $action)
    {
        if(parent::beforeControllerAction($controller,$action))
        {
            // Make 'adminUser' the main user in this module.
            // Frontend user is available as 'Yii::app()->frontendUser'.
            Yii::app()->setComponent('frontendUser', Yii::app()->user);
            Yii::app()->setComponent('user', Yii::app()->adminUser);
            return true;
        }
        else
            return false;
    }
}

现在,您可以登录网站的两个不同部分,两者都使用不同的用户群进行身份验证。

答案 1 :(得分:0)

是的,有。如果您有两个登录页面,则可以轻松使用两个CUserIdentity子类。然后,您的身份验证过程可能会有很大差异。

如果两个用户组的登录页面需要相同,则会涉及更多内容。但它应该是可行的。

答案 2 :(得分:0)

我解决问题的方式就像@michael解决方案,但有些不同。我在配置文件中定义了两个不同的用户组件:usercustomer

 'user' => array(
            'loginUrl' => '/admin/default/login',
            // enable cookie-based authentication
            'allowAutoLogin' => true,
        ),
        'customer' => array(
            'class' => 'NWebUser',
            'loginUrl' => '/customer/signIn',
            // enable cookie-based authentication
            'allowAutoLogin' => true,
        ),

对于客户,我违反了CWebUser名为NWebUser的扩展课程。这只是一个空洞的课程。除了他们之外,我已经定义了另一个扩展UserIdentity调用CustomerIdentity的类,我已经定义了我的客户验证方法:

class CustomerIdentity extends CUserIdentity {

    private $_id;

    public function authenticate() {
        $record = Customer::model()->findByAttributes(array('email' => $this->username));
        if ($record === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($record->password !== Customer::hashPassword($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $record->id;
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}

现在在我的登录方法中,我只使用此CustomerIdentity类:

public function login() {
        if ($this->_identity === null) {
            $this->_identity = new CustomerIdentity($this->username, $this->password);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === CustomerIdentity::ERROR_NONE) {
            $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
            Yii::app()->customer->login($this->_identity, $duration);
            return true;
        }
        else
            return false;
    }