I have done role based authentication using PhpAuthManager. I have defined the operations and roles statically in my PhpAuthManager class which extends CPhpAuthManager.
Now I want to have single role but the operations those belongs to the role to be added or decided on the fly i.e when user logs in.
To be honest, I do not really understand what you are trying to do.
But, let me give you this hint, if you are planning to change roles/operations/tasks/assignments during runtime, I would advise you using a database-based AuthManager, i.e. CDbAuthManager.
If you want to learn more about this, read the following manual:
<?php
class PhpAuthManager extends CPhpAuthManager{
public function init(){
$auth=Yii::app()->authManager;
$changed = false;
if ($auth->getAuthItem('main') === NULL) {
$auth->createOperation('main','Main Action');
$changed = true;
}
if($auth->getAuthItem('admin') === NULL) {
$role=$auth->createRole('admin');
$role->addChild('main');
$changed = true;
}
if ($changed) {
$auth->save();
}
}
public function assignRole($role) {
$auth=Yii::app()->authManager;
# revoke all auth items assigned to the user
$items = $auth->getRoles(Yii::app()->user->id);
foreach ($items as $item) {
$removel = $auth->revoke($item->name, Yii::app()->user->id);
}
# assign new role to the user
$auth->assign($role, Yii::app()->user->id);
$auth->save();
}
}
?>
In my PhpAuthManager file I have init function in that I am initializing the roles and assigning operations to the role. In current situation I have one role - admin and one operation - main. I am assigning role to the user on demand - when user logs in.
Instead I would like to have a single role like admin and operations to that role can be assigned in assignRole() function on demand and then that role will be assigned to the user.