You are here

shib_auth.module in Shibboleth Authentication 8

Contains shib_auth.module.

File

shib_auth.module
View source
<?php

/**
 * @file
 * Contains shib_auth.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function shib_auth_help($route_name, RouteMatchInterface $route_match) {

  // @todo - add help.
}

/**
 *
 */
function shib_auth_user_delete(EntityInterface $entity) {

  /** @var \Drupal\Core\Database\Connection $db */
  $db = \Drupal::service('database');
  $db
    ->delete('shib_authmap')
    ->condition('uid', $entity
    ->id())
    ->execute();
}

/**
 * Implements hook_form_alter().
 */
function shib_auth_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id != 'user_login_form') {
    return;
  }

  // Add Shibboleth link to form.
  $form['shibboleth_login_block'] = [
    '#weight' => -10,
    '#markup' => '<div class="shibboleth-login">' . shib_auth_get_login_link() . '</div>',
  ];
}

/**
 * Get the Shibboleth login link.
 *
 * @return \Drupal\Core\GeneratedLink
 */
function shib_auth_get_login_link() {
  $config = \Drupal::config('shib_auth.shibbolethsettings');
  $url = $config
    ->get('shibboleth_login_handler_url');
  $link_text = $config
    ->get('shibboleth_login_link_text');
  $force_https = $config
    ->get('force_https_on_login');
  $config = \Drupal::config('shib_auth.advancedsettings');
  $redirect = $config
    ->get('url_redirect_login');
  if ($redirect) {
    $redirect = Url::fromUserInput($redirect)
      ->toString();
  }
  else {

    // Not set, use current page.
    $redirect = Url::fromRoute('<current>')
      ->toString();
  }
  if ($force_https) {
    $redirect = preg_replace('~^http://~', 'https://', $redirect);
  }
  $options = [
    'absolute' => TRUE,
    'query' => [
      'destination' => $redirect,
    ],
  ];
  if ($force_https) {
    $options['https'] = TRUE;
  }

  // This is the callback to process the Shib login with the destination for
  // the redirect when done.
  $shib_login_url = Url::fromRoute('shib_auth.login_controller_login', [], $options)
    ->toString();
  $options = [
    'query' => [
      'target' => $shib_login_url,
    ],
  ];
  if ($force_https) {
    $options['https'] = TRUE;
    if (empty($_SERVER['HTTPS'])) {
      $options['absolute'] = TRUE;
    }
  }
  if (parse_url($url, PHP_URL_HOST)) {
    $url = Url::fromUri($url, $options);
  }
  else {
    $url = Url::fromUserInput($url, $options);
  }
  return Link::fromTextAndUrl($link_text, $url)
    ->toString();
}

/**
 * Get the Shibboleth logout link.
 *
 * @return \Drupal\Core\GeneratedLink
 */
function shib_auth_get_logout_link() {
  return Link::createFromRoute(t('Shibboleth Logout'), 'shib_auth.logout_controller_logout')
    ->toString();
}

Functions

Namesort descending Description
shib_auth_form_alter Implements hook_form_alter().
shib_auth_get_login_link Get the Shibboleth login link.
shib_auth_get_logout_link Get the Shibboleth logout link.
shib_auth_help Implements hook_help().
shib_auth_user_delete