You are here

function ldap_authentication_form_user_form_alter in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 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 124

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');

  /** @var \Drupal\externalauth\Authmap $authmap */
  $authmap = \Drupal::service('externalauth.authmap');
  $authname = $authmap
    ->get($user
    ->id(), 'ldap_user');
  if ($authname) {
    if ($config
      ->get('emailOption') === 'remove') {
      $form['account']['mail']['#access'] = FALSE;
    }
    elseif ($config
      ->get('emailOption') === 'disable') {
      $form['account']['mail']['#disabled'] = TRUE;
      $form['account']['mail']['#description'] = t('This email address is automatically set and may not be changed.');
    }
    if (!ldap_authentication_show_password_field($user)) {
      if ($config
        ->get('passwordOption') === 'hide') {

        // 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') === 'disable') {
        $form['account']['current_pass']['#disabled'] = TRUE;
        $form['account']['pass']['#disabled'] = TRUE;
        if ($config
          ->get('ldapUserHelpLinkUrl')) {
          $form['account']['current_pass']['#description'] = Link::fromTextAndUrl($config
            ->get('ldapUserHelpLinkText'), Url::fromUri($config
            ->get('ldapUserHelpLinkUrl')));
        }
        else {
          $form['account']['current_pass']['#description'] = t('The password cannot be changed using this website.');
        }
      }
    }
  }
}