vendor/contao/core-bundle/src/Security/Logout/LogoutHandler.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Security\Logout;
  11. use Contao\CoreBundle\Framework\ContaoFramework;
  12. use Contao\CoreBundle\Monolog\ContaoContext;
  13. use Contao\System;
  14. use Contao\User;
  15. use Psr\Log\LoggerInterface;
  16. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
  21. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  22. class LogoutHandler implements LogoutHandlerInterface
  23. {
  24.     use TargetPathTrait;
  25.     /**
  26.      * @var ContaoFramework
  27.      */
  28.     private $framework;
  29.     /**
  30.      * @var LoggerInterface|null
  31.      */
  32.     private $logger;
  33.     /**
  34.      * @internal Do not inherit from this class; decorate the "contao.security.logout_handler" service instead
  35.      */
  36.     public function __construct(ContaoFramework $frameworkLoggerInterface $logger null)
  37.     {
  38.         $this->framework $framework;
  39.         $this->logger $logger;
  40.     }
  41.     public function logout(Request $requestResponse $responseTokenInterface $token): void
  42.     {
  43.         if ($request->hasSession() && method_exists($token'getProviderKey')) {
  44.             $this->removeTargetPath($request->getSession(), $token->getProviderKey());
  45.         }
  46.         $user $token->getUser();
  47.         if (!$user instanceof User || $token instanceof TwoFactorTokenInterface) {
  48.             return;
  49.         }
  50.         if (null !== $this->logger) {
  51.             $this->logger->info(
  52.                 sprintf('User "%s" has logged out'$user->username),
  53.                 ['contao' => new ContaoContext(__METHOD__ContaoContext::ACCESS$user->username)]
  54.             );
  55.         }
  56.         $this->triggerPostLogoutHook($user);
  57.     }
  58.     private function triggerPostLogoutHook(User $user): void
  59.     {
  60.         $this->framework->initialize();
  61.         if (empty($GLOBALS['TL_HOOKS']['postLogout']) || !\is_array($GLOBALS['TL_HOOKS']['postLogout'])) {
  62.             return;
  63.         }
  64.         @trigger_error('Using the "postLogout" hook has been deprecated and will no longer work in Contao 5.0.'E_USER_DEPRECATED);
  65.         /** @var System $system */
  66.         $system $this->framework->getAdapter(System::class);
  67.         $GLOBALS['TL_USERNAME'] = $user->getUsername();
  68.         foreach ($GLOBALS['TL_HOOKS']['postLogout'] as $callback) {
  69.             $system->importStatic($callback[0])->{$callback[1]}($user);
  70.         }
  71.     }
  72. }