You are here

function shib_auth_get_login_link in Shibboleth Authentication 8

Get the Shibboleth login link.

Return value

\Drupal\Core\GeneratedLink

2 calls to shib_auth_get_login_link()
ShibbolethLoginBlock::build in src/Plugin/Block/ShibbolethLoginBlock.php
Builds and returns the renderable array for this block plugin.
shib_auth_form_alter in ./shib_auth.module
Implements hook_form_alter().

File

./shib_auth.module, line 56
Contains shib_auth.module.

Code

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();
}