This is my current situation: I've a website with four roles: "manager", "contributor" and "editor". Those roles cannot be added together to an user. An user can be a "manager" OR "contributor" OR "editor".

Currently "manager" users can create new users. I've used Administer Users by Role module so "manager" users can add/delete other users except the administration account. I've also used Role delegation module which allows "manager" users to assign the role to users.

When a "manager" add/edit an user, the role selection form displays all the selectable roles ("manager", "contributor" and "editor") with checkboxes.

I'd like to change checkboxes to an "option button" so only one role can be selected.

Someone has suggestions or hints about this issue? Should I customize Role delegation module or does it exists a simple way?

Thanks :-)

Comments

finex’s picture

Another idea I've had (but still unapplicable) is to modify the auto assign role in order to allow managers to create user account from specific path: role by path

http://drupal.org/node/673760

finex’s picture

I'm also trying with strange rules and pageroute combinations, but it looks that I'm not following the right path... :-(

caschbre’s picture

Check out the Role Delegation module. You choose which roles can assign which other roles to users.

Another option is to use the Rules module but that may be a bit overkill.

finex’s picture

As I've wrote in the initial message, I'm already using it, but it cannot force a single role from the selectable roles: more roles can be selected.

What I need is to give to the privileged user the ability to select only one role from the list of allowed selectable roles.

caschbre’s picture

Ah, sorry I missed that point.

Two things that come to mind initially are...

1) Override that form and change the checkboxes to radio buttons. That may be the best approach, but I'm not sure exactly how to do that.

2) Use the Rules module to trigger off of that form submission. In the condition of that rule you can write a little PHP to check if more than one role is selected. If so then you throw a form validation error and provide a message to the user.

tontonton’s picture

I was wondering if there any updates on this case?

timofey’s picture

Include the following in the module:

//convert roles checkboxes in registration form to select 
function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  if ( $form_id == 'user_register_form') {
    $form['account']['roles']['#type'] = 'select';
    $form['account']['roles']['#required'] = TRUE;
  }
}

//convert roles checkboxes in profile form to select
function MYMODULE_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  if ( $form_id == 'user_profile_form') {
    $form['account']['roles']['#type'] = 'select';
    $form['account']['roles']['#required'] = TRUE;
  }
}

//hack for registration & profile forms, attaches the authenticated role
function MODULE_user_presave(&$edit, $account, $category) {
  if (isset($edit['roles'])) {
    is_array($edit['roles']) ? '' : $edit['roles'] = array ( $edit['roles'] => $edit['roles'], 2 => '1' );
    $edit['roles'] = array_filter($edit['roles']);
  }
}
dromansab’s picture

In my case, this solution doesn't work with Role Delegation module... Do you know another solution?

Thanks!!

cmcs’s picture

Not sure if you ever came right with this, but I thought I'll perhaps put what I did for Role Delegation:

function MYMODULE_form_role_delegation_roles_form_alter(&$form, &$form_state, $form_id)
{
    $form['account']['roles_change']['#type'] = "select";
    $form['account']['roles_change']['#required'] = TRUE;
}
wolmi’s picture

I have the same situation where I have tree roles and need to asign one role per user.
Based in the timofey code I created a sandbox with a little module that simply do this feature.

The link to the sandbox for everyone who needs the same feature.

https://drupal.org/sandbox/wolmi/2022403

rishi.kulshreshtha’s picture

Hey FiNeX,

I've created this sandbox project with same requirement. Please test it, surely this will help you out.

git clone --branch master http://git.drupal.org/sandbox/RishiKulshreshtha/2235115.git single_role

Cheers!

gbisht’s picture

If anyone looking for module to limit user roles to one role per user then there is a module Single User Role for drupal 7. This module is also compatible with role delegation module. Module also provides configuration option to whether you want radio or select list for role field.