You are here

function ldapauth_form_alter in LDAP integration 6

Same name and namespace in other branches
  1. 5.2 ldapauth.module \ldapauth_form_alter()
  2. 5 ldapauth.module \ldapauth_form_alter()

Implements hook_form_alter().

File

./ldapauth.module, line 276
ldapauth provides authentication against ldap server.

Code

function ldapauth_form_alter(&$form, $form_state, $form_id) {
  global $user;

  // Replace the drupal authenticate function is it's used as validation.
  if (isset($form['#validate']) && is_array($form['#validate']) && ($key = array_search('user_login_authenticate_validate', $form['#validate']))) {
    $form['#validate'][$key] = 'ldapauth_login_authenticate_validate';
  }
  switch ($form_id) {
    case 'user_login_block':
    case 'user_login':
      if (LDAPAUTH_DISABLE_PASS_CHANGE && $user->uid != 1) {
        unset($form['links']);
        $key = array_search('user_login_final_validate', $form['#validate']);
        $form['#validate'][$key] = 'ldapauth_user_login_final_validate';
      }
      break;
    case 'user_profile_form':
      $account = $form["_account"]["#value"];
      if ($user->uid != 1 && isset($account->ldap_authentified)) {
        switch (LDAPAUTH_ALTER_USERNAME_FIELD) {
          case LDAPAUTH_USERNAME_FIELD_REMOVE:
            $form['account']['name']['#type'] = 'hidden';
            $form['account']['name']['#attributes']['READONLY'] = 'READONLY';
            break;
          case LDAPAUTH_USERNAME_FIELD_DISABLE:
            $form['account']['name']['#attributes']['READONLY'] = 'READONLY';
            $form['account']['name']['#description'] = t('NOTE: Can only be changed on the LDAP server.') . '<br/>' . $form['account']['name']['#description'];
            break;
        }
        if (LDAPAUTH_DISABLE_PASS_CHANGE) {
          unset($form['account']['pass']);
        }
        switch (LDAPAUTH_ALTER_EMAIL_FIELD) {
          case LDAPAUTH_EMAIL_FIELD_REMOVE:
            $form['account']['mail']['#type'] = 'hidden';
            $form['account']['mail']['#attributes']['READONLY'] = 'READONLY';
            break;
          case LDAPAUTH_EMAIL_FIELD_DISABLE:
            $form['account']['mail']['#attributes']['READONLY'] = 'READONLY';
            break;
        }

        // Remove fieldset if empty.
        if (isset($form['account']) && !isset($form['account']['pass']) && $form['account']['mail']['#type'] == 'hidden' && count(array_filter($form['account'], create_function('$a', 'return is_array($a) ? TRUE : FALSE;'))) == 1) {
          $form['mail'] = $form['account']['mail'];
          unset($form['account']);
        }
      }
      break;
  }
}