You are here

function _ldap_authentication_login_form_alter in Lightweight Directory Access Protocol (LDAP) 7

Same name and namespace in other branches
  1. 8.4 ldap_authentication/ldap_authentication.module \_ldap_authentication_login_form_alter()
  2. 8.2 ldap_authentication/ldap_authentication.inc \_ldap_authentication_login_form_alter()
  3. 8.3 ldap_authentication/ldap_authentication.module \_ldap_authentication_login_form_alter()
  4. 7.2 ldap_authentication/ldap_authentication.inc \_ldap_authentication_login_form_alter()

helper function for ldap_authn_form_user_login_block_alter and ldap_authn_form_user_login_alter

hook_user is gone in drupal 7 so functionality can be replaced by altering login form submit and validate functions http://drupal.org/update/modules/6/7#remove_op

if form is being generated on non https and is set in preferences, set warning and end form development add submit functions to form

  • make sure submit function is in the correct order; that is if ldap precedes drupal, make _ldap_authn_login_form_submit first.

do not remove other authentication submit functions, just reorder.

2 calls to _ldap_authentication_login_form_alter()
ldap_authentication_form_user_login_alter in ldap_authentication/ldap_authentication.module
Implements hook_form_FORM_ID_alter(). for user_login
ldap_authentication_form_user_login_block_alter in ldap_authentication/ldap_authentication.module
Implements hook_form_FORM_ID_alter(). for user_login_block

File

ldap_authentication/ldap_authentication.inc, line 21
ldap_authn provides authentication against ldap server.

Code

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

  /**
   * make sure ldap_authentication is configured and valid first
   */
  if (!($auth_conf = ldap_authentication_get_valid_conf())) {
    return;
  }
  if (!$auth_conf
    ->hasEnabledAuthenticationServers()) {
    return;
  }

  /**
   *
   * add validate function to test for ldap authentication
   * should be placed after user_login_authenticate_validate
   * 1. user_login_name_validate
   * 2. user_login_authenticate_validate
   * 3. external authentication validate functions
   * 4. user_login_final_validate
   *
   * as articulated above user_login_default_validators() in user.module
   *
   * without any other external authentication modules, this array will start out as:
   *    array('user_login_name_validate', 'user_login_authenticate_validate', 'user_login_final_validate')
   */
  if (@in_array('user_login_authenticate_validate', $form['#validate'])) {
    $new_validation_sequence = array();
    foreach ($form['#validate'] as $validate_function_name) {
      if ($validate_function_name == 'user_login_authenticate_validate') {
        if ($auth_conf->authenticationMode == LDAP_AUTHENTICATION_MIXED) {

          // if mixed mode, allow drupal authentication first
          $new_validation_sequence[] = 'user_login_authenticate_validate';
          $new_validation_sequence[] = 'ldap_authentication_user_login_authenticate_validate';
        }
        elseif ($auth_conf->authenticationMode == LDAP_AUTHENTICATION_EXCLUSIVE) {

          // see drupal.org/node/1009990 and drupal.org/node/1022362 change back when fixed.
          $new_validation_sequence[] = 'user_login_authenticate_validate';
          $new_validation_sequence[] = 'ldap_authentication_user_login_authenticate_validate';
        }
        else {

          // misconfigured ldap authentication, restore to original validation sequence
          $new_validation_sequence[] = 'user_login_authenticate_validate';
        }
      }
      else {
        $new_validation_sequence[] = $validate_function_name;
      }
    }
    $form['#validate'] = $new_validation_sequence;
  }
  if ($form_id == 'user_login_block') {
    $user_register = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
    $vars = array(
      'show_reset_pwd' => ldap_authentication_show_reset_pwd(),
      'auth_conf' => $auth_conf,
    );
    $form['links']['#markup'] = theme('ldap_authentication_user_login_block_links', $vars);
  }
  ldap_servers_disable_http_check($form);

  // Add help information for entering in username/password
  $auth_conf = ldap_authentication_get_valid_conf();
  if ($auth_conf) {
    if (isset($auth_conf->loginUIUsernameTxt)) {
      $form['name']['#description'] = t($auth_conf->loginUIUsernameTxt);
    }
    if (isset($auth_conf->loginUIPasswordTxt)) {
      $form['pass']['#description'] = t($auth_conf->loginUIPasswordTxt);
    }
  }
}