Rights Assignment On User Creation

Hi everyone,

i’m using yii-user + rights extensions.

I would like to know if it would be possible to assign a role (keep using rights extension) for a user, during the user creation, with an yii-user custom field (a dropdownlist menu of the roles defined in rights), and not through the rights administration panel.

Thanks,

Lilli

some additional information:

in my case only the administrator can create new users, through the "create new user" function of yii-user extension.

i would like to have a new dropdown field, so i can decide which is the role given to the user during his creation. this role must be recorded in rights (table AuthAssignment).

Hi,

in your form:


<?php echo $form->dropDownList($model, 'reader_role', Rights::getAuthItemSelectOptions('2',array('Admin'))); ?>

2 : type of AuthItem (Role in this case)

array(‘Admin’, ‘…’) : array of roles to exclude

cheers

thanks luc!

this is good for populating my dropdown menu with rights role, but i need a way to save the role in rights model, so in AuthAssigment table (remember that the form that we are using is for creating yii-user user and not for managing rights role).

do you know the easier way to do it?

thanks,

lilli

assuming $reader_role is what you get from the form submission




$user = new User;

// do the user stuff here: 

...........


if($user->save()){

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

$auth->assign($reader_role,$user->id);

$this->redirect(array('view','id'=>$user->id));

}



assign function

wow! great! thank you.

but does it works also with rights extension?

yes, rights exetension is just a (great) front end for Yii’s authManager

hi luc,

and what if i want to revoke/change the role?

i’ve seen that there is a revoke function but i have to know which is the role to revoke. how can i get it?

update i’ve seen this http://www.yiiframework.com/doc/api/1.1/IAuthManager#getAuthItems-detail

… but i can’t get the values. why?


... but i can't get the values. why?

could you provide me more informations on what you’re trying to do ?

[color="#006400"]/* moved from General Discussion */[/color]

something like this but it doesn’t work


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

	$roles =$auth->getAuthItems('2',$user->id);

	if (!empty($roles)){

		foreach($roles as $role){

			$auth->revoke($role,$user->id);

		}

	}

now it works:


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

					$arrayAuthRoleItems=$auth->getAuthItems('2',$user->id);

$roles = array_keys($arrayAuthRoleItems);

if (!empty($roles)){

foreach($roles as $key=>$role){

    $auth->revoke($role,$user->id);

}

}

good !