Yii框架的RBAC基础知识

时间:2013-02-11 21:51:27

标签: yii rbac

我只是设置我的RBAC hierarchie(使用CPhpAuthManager)

                $auth = Yii::app()->authManager;

                $auth->createOperation('create', 'add new data');
                $auth->createOperation('read', 'read data');
                $auth->createOperation('update', 'update data');
                $auth->createOperation('delete', 'delete data');

                $teammember = $auth->createRole('teammember');
                $teammember->addChild('create');
                $teammember->addChild('read');
                $teammember->addChild('update');
                $teammember->addChild('delete');

                $auth->save();

并像这样更新我的LoginForm模型类:

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

(查看 3星*** 的行)(Yii::app()->user->id将用户的ID作为整数返回)

它应该运作良好。我正在将用户登录到系统(with Yii::app()->user->login())然后(Yii::app()->authManager->assign()

现在我正在检查:

public function actionTest()
{
        if(Yii::app()->user->checkAccess('create'))
        {
                echo "YOU CAN DO THIS";
        }
        else
        {
                echo "YOU CAN'T DO THIS";
        }
}

但输出是 你不能这样做。我错过了什么吗?有什么问题?

其他

的结果
var_dump(Yii::app()->authManager->getRoles());

更漂亮的版本here

array(1) { ["teammember"]=> object(CAuthItem)#25 (8) { ["_auth":"CAuthItem":private]=> object(CPhpAuthManager)#20 (10) { ["authFile"]=> string(50) "/var/www/WingSplitDatabase/protected/data/auth.php" ["_items":"CPhpAuthManager":private]=> array(5) { ["create"]=> object(CAuthItem)#21 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(6) "create" ["_description":"CAuthItem":private]=> string(12) "add new data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["read"]=> object(CAuthItem)#22 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(4) "read" ["_description":"CAuthItem":private]=> string(9) "read data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["update"]=> object(CAuthItem)#23 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(6) "update" ["_description":"CAuthItem":private]=> string(11) "update data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["delete"]=> object(CAuthItem)#24 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(6) "delete" ["_description":"CAuthItem":private]=> string(11) "delete data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["teammember"]=> *RECURSION* } ["_children":"CPhpAuthManager":private]=> array(1) { ["teammember"]=> array(4) { ["create"]=> object(CAuthItem)#21 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(6) "create" ["_description":"CAuthItem":private]=> string(12) "add new data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["read"]=> object(CAuthItem)#22 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(4) "read" ["_description":"CAuthItem":private]=> string(9) "read data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["update"]=> object(CAuthItem)#23 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(6) "update" ["_description":"CAuthItem":private]=> string(11) "update data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["delete"]=> object(CAuthItem)#24 (8) { ["_auth":"CAuthItem":private]=> *RECURSION* ["_type":"CAuthItem":private]=> int(0) ["_name":"CAuthItem":private]=> string(6) "delete" ["_description":"CAuthItem":private]=> string(11) "delete data" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } } } ["_assignments":"CPhpAuthManager":private]=> array(0) { } ["showErrors"]=> bool(false) ["defaultRoles"]=> array(1) { [0]=> string(9) "ziyaretci" } ["behaviors"]=> array(0) { } ["_initialized":"CApplicationComponent":private]=> bool(true) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["_type":"CAuthItem":private]=> int(2) ["_name":"CAuthItem":private]=> string(10) "teammember" ["_description":"CAuthItem":private]=> string(0) "" ["_bizRule":"CAuthItem":private]=> NULL ["_data":"CAuthItem":private]=> NULL ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } }

1 个答案:

答案 0 :(得分:2)

您需要保存对身份验证设置的任何更改,因此只需在分配后添加以下行:

Yii::app()->authManager->save();