You are here

class CasRedirector in CAS 8

Same name and namespace in other branches
  1. 2.x src/Service/CasRedirector.php \Drupal\cas\Service\CasRedirector

Class CasRedirector.

Hierarchy

Expanded class hierarchy of CasRedirector

3 files declare their use of CasRedirector
CasRedirectorTest.php in tests/src/Unit/Service/CasRedirectorTest.php
CasSubscriber.php in src/Subscriber/CasSubscriber.php
ForceLoginController.php in src/Controller/ForceLoginController.php
1 string reference to 'CasRedirector'
cas.services.yml in ./cas.services.yml
cas.services.yml
1 service uses CasRedirector
cas.redirector in ./cas.services.yml
Drupal\cas\Service\CasRedirector

File

src/Service/CasRedirector.php, line 20

Namespace

Drupal\cas\Service
View source
class CasRedirector {

  /**
   * The CasHelper.
   *
   * @var CasHelper
   */
  protected $casHelper;

  /**
   * The EventDispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcher
   */
  protected $eventDispatcher;

  /**
   * Stores URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * Stores CAS settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $settings;

  /**
   * CasRedirector constructor.
   *
   * @param \Drupal\cas\Service\CasHelper $cas_helper
   *   The CasHelper service.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The EventDispatcher service.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(CasHelper $cas_helper, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, ConfigFactoryInterface $config_factory) {
    $this->casHelper = $cas_helper;
    $this->eventDispatcher = $event_dispatcher;
    $this->urlGenerator = $url_generator;
    $this->settings = $config_factory
      ->get('cas.settings');
  }

  /**
   * Determine login URL response.
   *
   * @param \Drupal\cas\CasRedirectData $data
   *   Data used to generate redirector.
   * @param bool $force
   *   True implies that you always want to generate a redirector as occurs with
   *   the ForceRedirectController. False implies redirector is controlled by
   *   the allow_redirect property in the CasRedirectData object.
   *
   * @return \Drupal\Core\Routing\TrustedRedirectResponse|\Drupal\cas\CasRedirectResponse|null
   *   The RedirectResponse or NULL if a redirect shouldn't be done.
   */
  public function buildRedirectResponse(CasRedirectData $data, $force = FALSE) {
    $response = NULL;
    $casServerConfig = CasServerConfig::createFromModuleConfig($this->settings);

    // Dispatch an event that allows modules to alter or prevent the redirect,
    // or to change the CAS server that we're redirected to.
    $pre_redirect_event = new CasPreRedirectEvent($data, $casServerConfig);
    $this->eventDispatcher
      ->dispatch(CasHelper::EVENT_PRE_REDIRECT, $pre_redirect_event);

    // Build the service URL, which is where the CAS server will send users
    // back to after authenticating them. We always send users back to our main
    // service controller, but there can be additional query params to attach
    // to that request as well.
    $service_parameters = $data
      ->getAllServiceParameters();
    $parameters = $data
      ->getAllParameters();
    $parameters['service'] = $this->urlGenerator
      ->generate('cas.service', $service_parameters, UrlGeneratorInterface::ABSOLUTE_URL);
    $login_url = $casServerConfig
      ->getServerBaseUrl() . 'login?' . UrlHelper::buildQuery($parameters);

    // Get the redirection response.
    if ($force || $data
      ->willRedirect()) {

      // $force implies we are on the /cas url or equivalent, so we
      // always want to redirect and data is always cacheable.
      if (!$force && !$data
        ->getIsCacheable()) {
        return new CasRedirectResponse($login_url);
      }
      else {
        $cacheable_metadata = new CacheableMetadata();

        // Add caching metadata from CasRedirectData.
        if (!empty($data
          ->getCacheTags())) {
          $cacheable_metadata
            ->addCacheTags($data
            ->getCacheTags());
        }
        if (!empty($data
          ->getCacheContexts())) {
          $cacheable_metadata
            ->addCacheContexts($data
            ->getCacheContexts());
        }
        $response = new TrustedRedirectResponse($login_url);
        $response
          ->addCacheableDependency($cacheable_metadata);
      }
      $this->casHelper
        ->log(LogLevel::DEBUG, "Cas redirecting to %url", [
        '%url' => $login_url,
      ]);
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CasRedirector::$casHelper protected property The CasHelper.
CasRedirector::$eventDispatcher protected property The EventDispatcher.
CasRedirector::$settings protected property Stores CAS settings.
CasRedirector::$urlGenerator protected property Stores URL generator.
CasRedirector::buildRedirectResponse public function Determine login URL response.
CasRedirector::__construct public function CasRedirector constructor.