【问题标题】:hard luck with RBAC in yiiyii 中的 RBAC 运气不好
【发布时间】:2012-06-23 17:21:34
【问题描述】:

我正在尝试按照 yii 上的各种在线教程和文档来学习 RBAC,最后以类似下面的内容结束,我遗漏了一些东西,但我不知道是什么。我学习了两次理论和教程,但在实际实现方面仍然很困难,所以最后我决定向 SO 社区寻求帮助。我到目前为止所做的正好低于

**I step**
create a table with fields:  username,password,email,role
role is enum datatype with 4 roles values ('superadmin','admin','useractive','userpassive')

**II step**
then i imported the schema-mysql.sql file in my database from the framework/web/auth folder of my yii setup.


**III step**
configured my config.php for CDbauthmanager

'authManager'=>array(
    'class'=>'CDbAuthManager',
'connectionID'=>'db',
'itemTable'=>'AuthItem',
'itemChildTable'=>'AuthItemChild',
'assignmentTable'=>'AuthAssignment',
),


**IV step**

then i added few lines to UserIdentity.php

public function authenticate()
{
    $user = Users::model()->findByAttributes(array('email'=>$this->username));
if ($user===null) { // No user found!
    $this->errorCode=self::ERROR_USERNAME_INVALID;
} 
    else if ($user->password !== $this->password ) 
    { // Invalid password!
    $this->errorCode=self::ERROR_PASSWORD_INVALID;
} else { // Okay!
        $this->errorCode=self::ERROR_NONE;
        // Store the role in a session:
        $this->setState('roles', $user->role);
    $this->_id = $user->id;
}
    return !$this->errorCode;
}
public function getId()
{
 return $this->_id;
}

**V step**

then i inserted values manually in the RBAC required table i.e. AuthItem,AuthItemChild,AuthAssignment

AuthItem table values
================================================================
name         type   description     bizrule data
user1        2          the user1 role      NULL    NULL
updateProfile    0          update profile      NULL    NULL
================================================================

AuthItemChild 
================================================================
parent  child
user1   updateProfile
================================================================

AuthAssignment table values
================================================================
itemname    userid  bizrule data
user1   1   NULL    NULL

And My users table
=================================================================
username    password    email             role
test1       pass1       tes1@local.com    user1

**VI step**

after that i tried to play with a controller

public function actionIndex()
{
    if(Yii::app()->user->checkAccess('updateProfile'))
    {
        echo "yes";
    }
    else
    {
        echo "missing something";
    }
}

现在,当我登录并尝试使用user1 访问控制器时,它显示“缺少某些东西”,但我已经为用户分配了相同的角色。我到底在想什么。

这就是我所做的正是我缺少的部分我不知道我几乎无法做到这一点。

感谢大家的宝贵时间

【问题讨论】:

    标签: authentication yii rbac


    【解决方案1】:

    小心你称为“user1”的角色。如果您还不知道:这不是您的用户,而只是一个称为 user1 的角色,如果您以后再回到它,可能会感到困惑 :)

    您表中的数据看起来不错。您添加了一个角色 (user1)、一个操作 (updateProfile),并将 updateProfile 操作链接为“user1”的子项。此外,您的 AuthAssignment 表明角色 user1 已添加到 userid 1,所以一切正常。

    我认为问题在于您似乎从未对 CWebUser 实例做任何事情。 Yii::app()->user 是一个 CWebUser 实例,它需要有一个与之关联的用户 ID。由于我没有看到分配此代码的代码,因此它可能为 NULL。 NULL 表示 Yii 将用户视为访客。你应该尝试做一个 Yii::app()->user->id 的 var_dump 来确定;

    正确的方法是在某处设置一行进行登录,如下所示: Yii::app()->user->login(, );

    要么这样,要么偷懒做一个 Yii::app()->user->id = ;

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2013-04-22
      • 2013-11-28
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多