You are here

function subuser_form_alter in Subuser 7.2

Same name and namespace in other branches
  1. 8 subuser.module \subuser_form_alter()

Implements hook_form_alter().

File

./subuser.module, line 233
Provides primary Drupal hook implementations.

Code

function subuser_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  switch ($form_id) {
    case 'user_account_form':
    case 'user_profile_form':
      if (!isset($form['account']) || $form['#user_category'] != 'account') {
        return;
      }
      $account = $form['#user'];

      // If the user does not have access to the roles field then filter the roles
      // field based on subuser permissions and display if more then one left. If
      // the user has 'administer permissions' then the #access will be set to TRUE
      // and they will have access to all roles, otherwise if the user has access
      // to this page through subuser then only provide them with the roles they
      // are allowed based on subuser. All users will have at least one role, but
      // that role may be 'authenticated user' which is not included in #options.
      if (!$form['account']['roles']['#access']) {
        $parents = subuser_load_all($account, FALSE);
        if (user_access('edit subusers', $user) && in_array($user->uid, $parents)) {
          foreach ($form['account']['roles']['#options'] as $rid => $role) {
            if (!user_access('create subuser ' . $rid)) {
              unset($form['account']['roles']['#options'][$rid]);
            }
          }
          $form['account']['roles']['#access'] = count($form['account']['roles']['#options']) > 0;
        }
      }

      // Remove the limit user setting from users which do not have the
      // administer subusers permissions.
      $form['field_subuser_limit']['#access'] = user_access('administer subusers', $user);
      break;
    case 'user_register_form':

      // Remove the limit user setting from users which do not have the
      // administer subusers permissions.
      $form['field_subuser_limit']['#access'] = user_access('administer subusers', $user);

      // Expose any roles to which the user has access to grant, as above.
      if (!$form['account']['roles']['#access']) {
        foreach ($form['account']['roles']['#options'] as $rid => $role) {
          if (!user_access('create subuser ' . $rid)) {
            unset($form['account']['roles']['#options'][$rid]);
          }
        }
        $form['account']['roles']['#access'] = count($form['account']['roles']['#options']) > 0;
      }
      break;
  }
}