Yii Framework Forum: Having problem getting bizrules to work - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Having problem getting bizrules to work Rate Topic: -----

#1 User is offline   mydarkpassenger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 26-April 10

Posted 01 March 2011 - 04:04 AM

I just learned how to use the RBAC and so far it's working great except for the business logic. The task I created looks like it should work but for some reason it always fails. Here's the logic:

       $bizRule = 'return Yii::app()->user->id==$params["object"]->user_id;';
       $task = $auth->createTask('updateOwnObject', 'update a object by user himself', $bizRule);        


and I'm testing it like this:

        $params = array();
        $params['object'] = Object::model()->findByPK(1);
        if (Yii::app()->user->checkAccess('updateOwnObject', $params))
            echo "Has access!";
        else
            echo "Does not have access";


However, when I run the code in a var_dump like this it returns true:

var_dump(Yii::app()->user->id==$params["object"]->user_id);  


Does anyone see anything wrong with my code. BTW if you need it here's the full structure I created:

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

        $auth->createOperation('createUser', 'create a user');
        $auth->createOperation('readUser', 'read a user');
        $auth->createOperation('updateUser', 'update a user');
        $auth->createOperation('deleteUser', 'delete a user');
        
        $auth->createOperation('createObject', 'create an object');
        $auth->createOperation('readObject', 'read an object');
        $auth->createOperation('updateObject', 'update an object');
        $auth->createOperation('deleteObject', 'delete an object');
        
        $bizRule = 'return Yii::app()->user->id==$params["object"]->user_id;';
        $task = $auth->createTask('updateOwnObject', 'update a object by user himself', $bizRule);         
        $task->addChild('updateObject');
        
        $role = $auth->createRole('registered');
        $role->addChild('readUser');
        $role->addChild('readObject');

        $role = $auth->createRole('member');
        $role->addChild('registered');
        $role->addChild('createObject');
        $role->addChild('updateObject');

        $role = $auth->createRole('admin');
        $role->addChild('registered');
        $role->addChild('member');
        $role->addChild('deleteUser');
        $role->addChild('createUser');
        $role->addChild('updateUser');
        $role->addChild('deleteObject');
        $auth->assign('admin', '3');
        $auth->assign('member', '4');


BTW, why does the assign example in the docs use names (for the id)? When I use names it always returns false.
0

#2 User is offline   Angéllica Araujo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 24-February 11

Posted 01 March 2011 - 09:19 AM

Try to call: Yii::app()->user->checkAccess('updateObject')

        $params = array();
        $params['object'] = Object::model()->findByPK(1);
       if(Yii::app()->user->checkAccess('updateObject'))
       // if (Yii::app()->user->checkAccess('updateOwnObject', $params))
            echo "Has access!";
        else
            echo "Does not have access";     

0

#3 User is offline   Angéllica Araujo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 24-February 11

Posted 01 March 2011 - 09:29 AM

Check this also:

return array(
    'components'=>array(
        'db'=>array(
            'class'=>'CDbConnection',
            'connectionString'=>'sqlite:path/to/file.db',
        ),
        'authManager'=>array(
            'class'=>'CDbAuthManager',
            'connectionID'=>'db',
        ),
    ),
);


http://www.yiiframew...ization-manager
0

#4 User is offline   mydarkpassenger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 26-April 10

Posted 01 March 2011 - 10:38 AM

updateObject works, it's just the biz rule that doesn't. The config file is correct as well. The problem is just limited to the updateOwnObject task.
0

#5 User is offline   Angéllica Araujo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 24-February 11

Posted 01 March 2011 - 10:50 AM

Qiang has an answer: http://www.yiiframew...nager#entry3943

Quote

The following is how checkAccess does:
1. For each assigned auth item of the user, it first checks if the bizRule for the assignment returns true.
2. If true, it calls the item's checkAccess method. If the item's bizRule returns true,
2.1. If the item name is the same as the name passed in the original checkAccess() method, it returns true;
2.2. Otherwise, for every child item, it calls its checkAccess.


:)

View Postmydarkpassenger, on 01 March 2011 - 10:38 AM, said:

updateObject works, it's just the biz rule that doesn't. The config file is correct as well. The problem is just limited to the updateOwnObject task.

0

#6 User is offline   mydarkpassenger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 26-April 10

Posted 01 March 2011 - 11:00 AM

View PostAngéllica Araujo, on 01 March 2011 - 10:50 AM, said:



Thank you so much that was the problem, makes total sense, I'm surprised I missed it. :)
0

#7 User is offline   Milan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 01-February 13

Posted 01 February 2013 - 10:16 AM

Hi to all
I have same problem :(

$auth=Yii::app()->authManager;
        $auth->clearAll();
        $auth->createOperation('viewTimelineOwnerMode','View timeline in owner mode');
        $auth->createOperation('viewTimelineVisitorMode','View timeline in visitor mode');
        $auth->createOperation('administration','Admin level');

        //$bizRule= "return ($visited->id == Yii::app()->user->id)";
        $bizRule= "return 1 == 1";
        $task=$auth->createTask('viewOwnTimeline','View own timeline on public profile page',$bizRule);
        $task->addChild('viewTimelineOwnerMode');

        $role=$auth->createRole('member');
        $role->addChild('viewTimelineVisitorMode');
        $role->addChild('viewOwnTimeline');

        $role=$auth->createRole('journalist');
        $role->addChild('viewTimelineOwnerMode');

        $role=$auth->createRole('admin');
        $role->addChild('journalist');
        $role->addChild('administration');

        $auth->assign('admin',649);
        $auth->assign('journalist',600);
        
        $auth->assign('member',304);


so, admin and journalist see viewTimelineOwnerMode, but member not. and $bizRule= "return 1 == 1"; -> always true...

do anybody see what I did wrong?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users