Commit c7ef18e8 authored by Fabian de Rijk's avatar Fabian de Rijk
Browse files

Issue #3295765: Add option to hide username and related fields

parent 6e81d1a5
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -17,6 +17,29 @@ function o365_sso_form_user_login_form_alter(&$form, FormStateInterface $form_st
  o365_sso_alter_login_form($form, $form_state, $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function o365_sso_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'user_form') {
    $config = \Drupal::config('o365_sso.settings');
    // We want to hide email, password and email fields for o365 users.
    if (!empty($config->get('hide_user_fields'))) {
      /** @var \Drupal\user\Entity\User $user */
      $user = \Drupal::routeMatch()->getParameter('user');
      /** @var \Drupal\externalauth\Authmap $authMap */
      $authMap = \Drupal::service('externalauth.authmap');
      $authMapUser = $authMap->get($user->id(), 'o365_sso');
      // Only do this for users that are a o365_sso authmap user.
      if ($authMapUser) {
        $form['account']['mail']['#access'] = FALSE;
        $form['account']['name']['#access'] = FALSE;
        $form['account']['pass']['#access'] = FALSE;
      }
    }
  }
}

/**
 * Function that we can use to change the login form.
 *
+9 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ class SettingsForm extends ConfigFormBase {
      '#default_value' => $config->get('auto_redirect'),
      '#description' => $this->t('When enabled anonymous users that access the /user/login page will automatically be redirected to the SSO login page. Users that use "local" users (like admins) can use the /user/drupal_login path to use the normal Drupal login.'),
    ];

    $form['redirect_login_destination'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Redirect after login to destination URL'),
@@ -59,6 +60,13 @@ class SettingsForm extends ConfigFormBase {
      '#default_value' => $config->get('redirect_login_destination'),
    ];

    $form['hide_user_fields'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Hide the username, password and e-mail fields from the user edit form.'),
      '#default_value' => $config->get('hide_user_fields'),
      '#description' => $this->t('This is only applicable to users that have been added with the Office 365 connector module.'),
    ];

    return parent::buildForm($form, $form_state);
  }

@@ -73,6 +81,7 @@ class SettingsForm extends ConfigFormBase {
      ->set('link_text', $form_state->getValue('link_text'))
      ->set('link_position', $form_state->getValue('link_position'))
      ->set('redirect_login_destination', $form_state->getValue('redirect_login_destination'))
      ->set('hide_user_fields', $form_state->getValue('hide_user_fields'))
      ->save();
  }