You are here

function roleassign_form_alter in RoleAssign 6

Same name and namespace in other branches
  1. 8 roleassign.module \roleassign_form_alter()
  2. 5 roleassign.module \roleassign_form_alter()
  3. 7.2 roleassign.module \roleassign_form_alter()
  4. 7 roleassign.module \roleassign_form_alter()

Implementation of hook_form_alter().

Adds checkboxes for assignable roles to the user edit form.

File

./roleassign.module, line 114
Allows site administrators to further delegate the task of managing user's roles.

Code

function roleassign_form_alter(&$form, &$form_state, $form_id) {

  // Do nothing if the user already has 'administer permissions' permission.
  if (user_access('administer permissions')) {
    return;
  }

  // Do nothing if the user doesn't have both 'administer users' and
  // 'assign roles' permissions.
  if (!user_access('administer users') || !user_access('assign roles')) {
    return;
  }

  // Keep the (restricted) user from disabling this module.
  if ($form_id == 'system_modules') {
    $form['status']['#process'][] = '_roleassign_module_protect';
    return;
  }

  // Do nothing if right form isn't shown.
  if ($form_id != 'user_register' && ($form_id != 'user_profile_form' || !isset($form['account']))) {
    return;
  }

  // Get all roles that are available.
  $roles = user_roles(true);

  // Get roles that are available for assignment.
  $assignable_roles = _roleassign_assignable_roles($roles);

  // Get roles already assigned to the user.
  $user = user_load(array(
    'uid' => arg(1),
  ));
  $assigned_roles = $user->roles;

  // A user might already have a role that isn't available for assignment
  // through this module. Such a role is called "sticky".
  // Get sticky roles.
  $sticky_roles = array_diff($assigned_roles, $assignable_roles);
  $sticky_roles = array_intersect_key($roles, $sticky_roles);

  // Store sticky roles for later use in roleassign_user().
  _roleassign_sticky_roles($sticky_roles);

  // Make a string of all sticky roles.
  $sticky_roles[DRUPAL_AUTHENTICATED_RID] = $roles[DRUPAL_AUTHENTICATED_RID];
  $sticky_roles_str = implode(', ', $sticky_roles);

  // Build the assign roles checkboxes.
  $roles_field = array(
    '#type' => 'checkboxes',
    '#title' => t('Assignable roles'),
    '#options' => $assignable_roles,
    '#default_value' => array_keys($assigned_roles),
    '#description' => t('The user receives the combined permissions of all roles selected here and following roles: %roles.', array(
      '%roles' => $sticky_roles_str,
    )),
  );

  // The user form is sometimes within an 'account' fieldset.
  if (isset($form['account'])) {
    $user_form =& $form['account'];
  }
  else {
    $user_form =& $form;
  }

  // Add the assign roles checkboxes to the user form, and make sure
  // that the notify user checkbox comes last.
  if (isset($user_form['notify'])) {
    $notify_field = $user_form['notify'];
    unset($user_form['notify']);
    $user_form['roleassign_roles'] = $roles_field;
    $user_form['notify'] = $notify_field;
  }
  else {
    $user_form['roleassign_roles'] = $roles_field;
  }
  if (user_access('administer permissions', $user)) {
    drupal_set_message(t('Some of the fields on this form are locked for this user.'), 'warning');
    $form['account']['name']['#disabled'] = TRUE;
    $form['account']['mail']['#disabled'] = TRUE;
    $form['account']['pass']['#access'] = FALSE;
  }
}