Skip to content

Instantly share code, notes, and snippets.

@jenitehan
Last active August 29, 2015 14:01
Show Gist options
  • Save jenitehan/c9529e529a8ffd975609 to your computer and use it in GitHub Desktop.
Save jenitehan/c9529e529a8ffd975609 to your computer and use it in GitHub Desktop.
Alter user VBO assign all user roles
<?php
/**
* Views Bulk Operations doesn't respect Role Delegation's permissions in the "Modify user roles"
* action and will offer all roles as options to add or remove from the users being edited. This form
* alter fixed that by checking which roles the user had permissions to assign and altering the options
* in the select element.
*
* This is no longer needed since I can just use the "Delegate roles" action provided by Role Delegation.
* I didn't realise that was there!
*/
/**
* Implements hook_form_FORM_ID_alter();
* Only allow users to assign and remove roles they are allowed to.
*/
function tastybackend_standard_form_views_form_user_bulk_operations_page_alter(&$form, &$form_state, $form_id) {
if (isset($form['add_roles'])) {
global $user;
$options = array();
// Don't alter form for user 1 or administrators.
if ($user->uid == '1' || in_array('administrator', $user->roles) || user_access('assign all roles')) {
$options = $form['add_roles']['#options'];
}
else {
$roles = user_roles(TRUE);
foreach ($roles as $rid => $role) {
// Check access to assign/remove each role determined by Role Delegation permissions.
if (user_access('assign ' . $role . ' role')) {
$options[$rid] = $role;
}
}
}
$form['add_roles']['#options'] = $options;
$form['remove_roles']['#options'] = $options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment