I just set my RBAC hierarchie like this (using 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();

And update my LoginForm model class like this :

    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;
    }

(Look at the line with 3 stars ***) (Yii::app()->user->id returns user's ID as integer)

It should work well. I'm logging user to system (with Yii::app()->user->login()) and then (Yii::app()->authManager->assign())

Now i'm checking with this :

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

But output is YOU CAN'T DO THIS . Am i missing something? What is the problem?

ADDITIONAL

Result of

var_dump(Yii::app()->authManager->getRoles());

:

prettier version 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 } }
有帮助吗?

解决方案

Any changes to your auth setup needs to be saved, so simply add the following line after your assignment:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top