Yii Framework Forum: RBAC again - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

RBAC again Rate Topic: -----

#1 User is offline   kvl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 12-February 09

Posted 07 April 2009 - 03:13 AM

Hi All!
I red already the posts concerning RBAC, they didn't help me... ???
I have some lines in my UserIdentity::authorization():

$roleName='admin';
$auth = Yii::app()->authManager;
$role = $auth->createRole($roleName);
$task = $auth->createTask($roleName . 'Panel');   
$role->addChild($roleName . 'Panel');
$auth->assign($roleName, $user->id);

Then, access rules in the controller:

public function accessRules()
{
return array(
array('allow',
'roles'=>array('admin'),
                'users'=>array('@')
),
array('deny',  // deny all users
'users'=>array('*'),
),
);
}

of course,

public function filters()
{
return array(
'accessControl',
);
}

And it doesn't work in any case!
My user has access to nothing at this controller!  :o
Wat's wrong? help me, please, if you can see my error!
0

#2 User is offline   emix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 02-March 09
  • Location:Poland

Posted 08 April 2009 - 06:43 AM

1) how did you configure the auth manager? (copy code from main config)
2) this code:


$auth = Yii::app()->authManager;
$role = $auth->createRole($roleName);
$task = $auth->createTask($roleName . 'Panel');   
$role->addChild($roleName . 'Panel');


is meant to be run only once. After that it will be saved in a persistent storage and remembered.

The role assign thing $auth->assign($roleName, $user->id) MUST BE RUN every script run, not only at authorization!
You can do so by overriding init() method in the controller.

example. At authorization point set isAdmin flag if user is admin or not.

class yourController extends CController {
  public function init() {
      if (Yii::app()->user->isAdmin) Yii::app()->authManager->assign('admin', Yii::app()->user->id);
  }
}


simple.
0

#3 User is offline   kvl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 12-February 09

Posted 08 April 2009 - 07:56 AM

Quote

1) how did you configure the auth manager? (copy code from main config)



<?php
'authManager'=>array('class'=>'CPhpAuthManager'),


Quote

2) this code:

.......

is meant to be run only once. After that it will be saved in a persistent storage and remembered.

The role assign thing $auth->assign($roleName, $user->id) MUST BE RUN every script run, not only at authorization!

You can do so by overriding init() method in the controller.

example. At authorization point set isAdmin flag if user is admin or not.


Thanks, it seems, I'm beginning to understand slowly...
But - if I need to set 'admin' flag manually and check it in my controllers - what advantage of this approach?
One step remains to mark all needed rules in a filter - that is that I did after all...
0

#4 User is offline   emix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 02-March 09
  • Location:Poland

Posted 08 April 2009 - 09:02 AM

I assumed you need only user/admin groups and showed an example how to make rbac working. The final method is up to you.
0

#5 User is offline   Rashid A. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 03-July 09
  • Location:Islamabad, PK

Posted 15 October 2009 - 02:47 AM

Hi,

I need some guide here.

I want to use RBAC based on PHP code only and not database.

The code I have put in the auth.php is...

//authorization manager object
$auth = Yii::app()->authManager;

//create roles
$sales = $auth->createRole('sales');
$screening = $auth->createRole('screening');
$admin = $auth->createRole('admin');

$bizRule = 'return !Yii::app()->user->isGuest;';
$auth->createRole('authenticated', 'authenticated user', $bizRule);

$bizRule = 'return Yii::app()->user->isGuest;';
$auth->createRole('guest', 'guest user', $bizRule);

if (Yii::app()->user->role == User::SALES) {
	$auth->assign('sales', Yii::app()->user->id);
}
elseif (Yii::app()->user->role == User::SCREENING) {
	$auth->assign('screening', Yii::app()->user->id);
}
elseif (Yii::app()->user->role == User::ADMIN) {
	$auth->assign('admin', Yii::app()->user->id);
}

$auth->save();


I was using "expression" for access control that's why I have those conditional statements but now want to use RBAC.

Now I have read that this code executes only one time, really?
Then where is it saving?
what is happening behind the scene?

Plus, I'm also getting this error in auth.php

Quote

Fatal error: Call to a member function createRole() on a non-object in D:\xampp\htdocs\sas\protected\data\auth.php on line 6

which is this line
$sales = $auth->createRole('sales');


Thanks.
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 October 2009 - 04:30 AM

You could for example put it into a (obscured) controller action and call this action one time. Then comment it out again to make it unaccessible from outside. Or use any other mechanism that makes this action only accessible for you.

Also make sure you configured a authManager component.

Edit:
Also check CPhpAuthManagers options on where the hierarchy will be saved.
1

#7 User is offline   Rashid A. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 03-July 09
  • Location:Islamabad, PK

Posted 15 October 2009 - 04:46 AM

Quote

You could for example put it into a (obscured) controller action and call this action one time. Then comment it out again to make it unaccessible from outside. Or use any other mechanism that makes this action only accessible for you.

Yeah, i guess that will be better and let say if I have to change the roles on production I can enable and call the action again.

Quote

Also make sure you configured a authManager component.

I have following enabled component in my config.php
'authManager'=>array(
    'class'=>'CPhpAuthManager',
    'defaultRoles'=>array('guest'),
),



Quote

Also check CPhpAuthManagers options on where the hierarchy will be saved.

Yep... going to read that now.

Thanks allllot again Mike.

regards,
0

#8 User is offline   Rashid A. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 03-July 09
  • Location:Islamabad, PK

Posted 15 October 2009 - 05:13 AM

The documentation says...

Quote

The authorization data will be saved to and loaded from a file specified by authFile, which defaults to 'protected/data/auth.php'.
which means that what we write in the auth.php is actually saved and that has to be executed once per session of the visitor. Because there is not other storage medium defined it can be possible that the auth.php is running every time which would be poor approach.

right? :(

I can't clear the mist about it in my head. Need some explanation on this.

regards,
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 October 2009 - 05:18 AM

1. Create some mechanism to set up RBAC hierarchy (e.g. controller action as mentioned above)
2. Call this action once: The hierarchy will be saved to the data file
3. Whenever you add or revoke users to roles, this will also be written to the data file.

On every request the data file is read automatically by CPhpAuthManager. You should never touch that file manually!
1

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 October 2009 - 05:21 AM

View Postemix, on 08 April 2009 - 06:43 AM, said:

The role assign thing $auth->assign($roleName, $user->id) MUST BE RUN every script run, not only at authorization!
You can do so by overriding init() method in the controller.


Maybe the confusion comes from this. The above is wrong (sorry emix ;))! The assignments are also saved persistently.
0

#11 User is offline   Rashid A. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 03-July 09
  • Location:Islamabad, PK

Posted 15 October 2009 - 06:02 AM

Quote

1. Create some mechanism to set up RBAC hierarchy (e.g. controller action as mentioned above)
2. Call this action once: The hierarchy will be saved to the data file
3. Whenever you add or revoke users to roles, this will also be written to the data file.

On every request the data file is read automatically by CPhpAuthManager. You should never touch that file manually!

It's not working this way...

I had put up all the auth.php code in an action of site controller. The auth.php is empty now. When I exec the action in browser it gives error like this while trying to load authorization data from auth.php

Quote

D:\xampp\htdocs\yii-1.0.9.r1396\framework\web\auth\CPhpAuthManager.php(449)
. Now this means I have to create the hierarchy in auth.php but when I do that... it doesn't create the object and I get the method call error on non-object error.

So I gave a thought about it and deleted the auth.php and called the action again. Guess what... IT WORKED!!!
Yii has created a new auth.php and the roles have been created in it. Now I know how the roles look like.
return array (
  'sales' => 
  array (
    'type' => 2,
    'description' => '',
    'bizRule' => NULL,
    'data' => NULL,
  ),
  'recruitment' => 
  array (
    'type' => 2,
    'description' => '',
    'bizRule' => NULL,
    'data' => NULL,
  ),
  'screening' => 
  array (
    'type' => 2,
    'description' => '',
    'bizRule' => NULL,
    'data' => NULL,
  ),
  'admin' => 
  array (
    'type' => 2,
    'description' => '',
    'bizRule' => NULL,
    'data' => NULL,
  ),
  'authenticated' => 
  array (
    'type' => 2,
    'description' => 'authenticated user',
    'bizRule' => 'return !Yii::app()->user->isGuest;',
    'data' => NULL,
  ),
  'guest' => 
  array (
    'type' => 2,
    'description' => 'guest user',
    'bizRule' => 'return Yii::app()->user->isGuest;',
    'data' => NULL,
  ),
);


Now I don't have to call the action anymore for minor changes... I can change the roles directly in this array.

Thanks Mike again, you last 3 points helped a lot. I think the documentation needs to provide a bit more detail on this thing as most people are asking the same question. Will catch you some time later ^_^

Hope this whole discussion will help someone get their rbac started right away without hassle.

Good Luck and regards,
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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