LogoutController.php in CAS 8
File
src/Controller/LogoutController.php
View source
<?php
namespace Drupal\cas\Controller;
use Drupal\cas\CasRedirectResponse;
use Drupal\cas\CasServerConfig;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Psr\Log\LogLevel;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\cas\Service\CasHelper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class LogoutController implements ContainerInjectionInterface {
protected $casHelper;
protected $requestStack;
protected $settings;
protected $urlGenerator;
public function __construct(CasHelper $cas_helper, RequestStack $request_stack, ConfigFactoryInterface $config_factory, UrlGeneratorInterface $url_generator) {
$this->casHelper = $cas_helper;
$this->requestStack = $request_stack;
$this->settings = $config_factory
->get('cas.settings');
$this->urlGenerator = $url_generator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('cas.helper'), $container
->get('request_stack'), $container
->get('config.factory'), $container
->get('url_generator.non_bubbling'));
}
public function logout() {
user_logout();
$cas_server_logout_url = $this
->getServerLogoutUrl($this->requestStack
->getCurrentRequest());
$this->casHelper
->log(LogLevel::DEBUG, "Drupal session terminated; redirecting to CAS server logout at %url", [
'%url' => $cas_server_logout_url,
]);
return new CasRedirectResponse($cas_server_logout_url, 302);
}
public function getServerLogoutUrl(Request $request) {
$casServerConfig = CasServerConfig::createFromModuleConfig($this->settings);
$base_url = $casServerConfig
->getServerBaseUrl() . 'logout';
if (!empty($this->settings
->get('logout.logout_destination'))) {
$destination = $this->settings
->get('logout.logout_destination');
if ($destination == '<front>') {
$return_url = $this->urlGenerator
->generate($destination, [], UrlGeneratorInterface::ABSOLUTE_URL);
}
elseif (UrlHelper::isExternal($destination)) {
$return_url = $destination;
}
else {
$return_url = $request
->getSchemeAndHttpHost() . '/' . ltrim($destination, '/');
}
if ($casServerConfig
->getProtocolVerison() == '2.0') {
$params['url'] = $return_url;
}
else {
$params['service'] = $return_url;
}
return $base_url . '?' . UrlHelper::buildQuery($params);
}
else {
return $base_url;
}
}
}