o365-2.0.3/modules/o365_sso/o365_sso.module

modules/o365_sso/o365_sso.module
<?php

/**
 * @file
 * Contains o365_sso.module.
 */

use Drupal\Core\Form\EnforcedResponseException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function o365_sso_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  o365_sso_alter_login_form($form, $form_state, $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function o365_sso_user_form_alter(&$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;
        $form['account']['current_pass']['#access'] = FALSE;
      }
    }
  }
}

/**
 * Function that we can use to change the login form.
 *
 * @param array $form
 *   The form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state object.
 * @param string $form_id
 *   The form id.
 */
function o365_sso_alter_login_form(array &$form, FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('o365_sso.settings');
  $form['#attached']['library'][] = 'o365/general';
  $form['#attached']['library'][] = 'o365/icons';
  $form['#cache']['tags'][] = 'config:o365_sso.settings';
  $form['#cache']['tags'][] = 'config:o365_connector_list';

  // Disable cache for this form. This makes the redirect work if enabled.
  $form['#cache']['max-age'] = 0;

  // Hide form fields when this option is enabled.
  $hideForm = $config->get('hide_login_form');
  $request = \Drupal::request();
  if (!empty($hideForm)) {
    $showLogin = $request->query->get('show_login');
    // Only hide when the show_login parameter is not true.
    if ($showLogin !== 'true') {
      $form['name']['#access'] = FALSE;
      $form['pass']['#access'] = FALSE;
      $form['more-links']['#access'] = FALSE;
      $form['actions']['submit']['#access'] = FALSE;
    }
  }

  $redirectDestination = $config->get('redirect_login_destination');
  $query = [];
  if (!empty($redirectDestination)) {
    $destination = $request->query->get('destination');
    if (!$destination) {
      $destination = $request->query->get('prev_path');
    }
    if ($destination) {
      $query['destination'] = $destination;
    }
  }

  // Only redirect when auto redirect is enabled and the path does not contain
  // the local login link.
  if (!empty($config->get('auto_redirect')) && \Drupal::routeMatch()->getRouteName() !== 'o365_sso.drupal_login') {
    $url = Url::fromRoute('o365_sso.login_controller_login_default', [], [
      'absolute' => TRUE,
      'query' => $query,
    ]);
    $response = new RedirectResponse($url->toString());
    throw new EnforcedResponseException($response);
  }

  $linkWeight = -99;
  if (empty($config->get('link_position'))) {
    $linkWeight = 99;
  }

  $form['o365_actions'] = [
    '#type' => 'actions',
    '#attributes' => [
      'class' => [
        'o365-login-links',
      ],
    ],
    '#weight' => $form['actions']['#weight'] ?? $linkWeight,
  ];

  // Get all Microsoft 365 connectors.
  /** @var \Drupal\o365\O365ConnectorInterface[] $o365Connectors */
  $o365Connectors = \Drupal::entityTypeManager()->getStorage('o365_connector')->loadByProperties(['status' => TRUE]);
  $loginLinkText = $config->get('link_text') ?? 'Login with @connector';

  foreach ($o365Connectors as $o365ConnectorId => $o365Connector) {
    $form['o365_actions']['sso_login_link'][$o365ConnectorId] = [
      '#title' => Markup::create('<span class="ms-Icon ms-Icon--OfficeLogo o365--mr-4"></span>' . strtr($loginLinkText, ['@connector' => $o365Connector->label()])),
      '#type' => 'link',
      '#url' => Url::fromRoute('o365_sso.login_controller_login', ['o365_connector' => $o365ConnectorId], ['query' => $query]),
      '#attributes' => [
        'class' => ['button--primary', 'button-o365-sso', 'button'],
      ],
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc