class CasRedirector in CAS 8
Same name and namespace in other branches
- 2.x src/Service/CasRedirector.php \Drupal\cas\Service\CasRedirector
Class CasRedirector.
Hierarchy
- class \Drupal\cas\Service\CasRedirector
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'
1 service uses CasRedirector
File
- src/
Service/ CasRedirector.php, line 20
Namespace
Drupal\cas\ServiceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CasRedirector:: |
protected | property | The CasHelper. | |
CasRedirector:: |
protected | property | The EventDispatcher. | |
CasRedirector:: |
protected | property | Stores CAS settings. | |
CasRedirector:: |
protected | property | Stores URL generator. | |
CasRedirector:: |
public | function | Determine login URL response. | |
CasRedirector:: |
public | function | CasRedirector constructor. |