You are here

function ldap_authentication_form_user_form_alter in Lightweight Directory Access Protocol (LDAP) 8.3

Same name and namespace in other branches
  1. 8.4 ldap_authentication/ldap_authentication.module \ldap_authentication_form_user_form_alter()

Implements hook_form_FORM_ID_alter().

Alter user editing form (profile form) based on LDAP authentication configuration.

@TODO: This function hides corner cases and does not consistently embed the help text.

File

ldap_authentication/ldap_authentication.module, line 129
This module injects itself into Drupal's Authentication stack.

Code

function ldap_authentication_form_user_form_alter(&$form, FormStateInterface $form_state) {
  $user = $form_state
    ->getBuildInfo()['callback_object']
    ->getEntity();
  $config = \Drupal::config('ldap_authentication.settings');
  if (ldap_authentication_ldap_authenticated($user)) {
    if ($config
      ->get('emailOption') == LdapAuthenticationConfiguration::$emailFieldRemove) {
      $form['account']['mail']['#access'] = FALSE;
    }
    elseif ($config
      ->get('emailOption') == LdapAuthenticationConfiguration::$emailFieldDisable) {
      $form['account']['mail']['#disabled'] = TRUE;
      $form['account']['mail']['#description'] = t('This email address is automatically set and may not be changed.');
    }
    if (!LdapAuthenticationConfiguration::showPasswordField($user)) {
      if ($config
        ->get('passwordOption') == LdapAuthenticationConfiguration::$passwordFieldHide) {

        // TODO: Allow for the case where email changes are allowed to show
        // current pass.
        $form['account']['current_pass']['#access'] = FALSE;
        $form['account']['pass']['#access'] = FALSE;
      }
      elseif ($config
        ->get('emailOption') == LdapAuthenticationConfiguration::$emailFieldDisable) {
        $form['account']['current_pass']['#disabled'] = TRUE;
        $form['account']['pass']['#disabled'] = TRUE;
        if ($config
          ->get('ldapUserHelpLinkUrl')) {
          $form['account']['current_pass']['#description'] = \Drupal::l($config
            ->get('ldapUserHelpLinkText'), Url::fromUri($config
            ->get('ldapUserHelpLinkUrl')));
        }
        else {
          $form['account']['current_pass']['#description'] = t('The password cannot be changed using this website.');
        }
      }
    }
  }
}