You are here

function cas_form_user_form_alter in CAS 2.x

Same name and namespace in other branches
  1. 8 cas.module \cas_form_user_form_alter()

Implements hook_form_FORM_ID_alter().

Alters the user entity form.

File

./cas.module, line 56
Provides CAS authentication for Drupal.

Code

function cas_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  _cas_add_cas_username_to_user_form($form);
  $cas_user_manager = \Drupal::service('cas.user_manager');
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $cas_username = $cas_user_manager
    ->getCasUsernameForAccount($account
    ->id());

  // If a CAS username was found for this user, then populate the CAS username
  // form field with its current value. Also remove the password management
  // fields if configured to do so.
  if ($cas_username) {
    $form['account']['cas_enabled']['#default_value'] = TRUE;
    $form['account']['cas_username']['#default_value'] = $cas_username;
    if (!\Drupal::currentUser()
      ->hasPermission('administer users')) {
      if (\Drupal::config('cas.settings')
        ->get('user_accounts.restrict_password_management')) {
        $form['account']['pass']['#access'] = FALSE;

        // Users are normally required to entier their current password when
        // changing their password or email address. Since we are disabling
        // access to change their password, and since a CAS user would not know
        // their local Drupal password anyway, we remove this field as well.
        if (isset($form['account']['current_pass'])) {
          $form['account']['current_pass']['#access'] = FALSE;
        }
      }
      if (\Drupal::config('cas.settings')
        ->get('user_accounts.restrict_email_management')) {
        if (isset($form['account']['mail'])) {
          $form['account']['mail']['#disabled'] = TRUE;
        }
      }
    }
  }
}