src/Eccube/Security/Http/Authentication/EccubeLogoutSuccessHandler.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Security\Http\Authentication;
  13. require 'vendor/autoload.php';
  14. use GuzzleHttp\Client;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Eccube\Request\Context;
  19. use Eccube\Service\SystemService;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Security\Http\HttpUtils;
  22. use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler;
  23. use Eccube\Service\OrderHelper;
  24. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  25. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  26. use Eccube\Service\CartService;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. use Symfony\Component\Form\FormFactoryInterface;
  29. class EccubeLogoutSuccessHandler extends DefaultLogoutSuccessHandler
  30. {
  31.     /**
  32.      * @var Session
  33.      */
  34.     protected $session;
  35.     /** @var Context */
  36.     protected $context;
  37.     private $orderHelper;
  38.     /**
  39.      * @var FormFactoryInterface
  40.      */
  41.     protected $formFactory;
  42.     /**
  43.      * @var EventDispatcherInterface
  44.      */
  45.     protected $eventDispatcher;
  46.     public function __construct(
  47.         HttpUtils $httpUtils,
  48.         Context $context,
  49.         $targetUrl '/',
  50.         TokenStorageInterface $tokenStorage,
  51.         CartService $cartService,
  52.         OrderHelper $orderHelper,
  53.         FormFactoryInterface $formFactory,
  54.         EventDispatcherInterface $eventDispatcher
  55.     )
  56.     {
  57.         parent::__construct($httpUtils$targetUrl);
  58.         $this->context $context;
  59.         $this->tokenStorage $tokenStorage;
  60.         $this->cartService $cartService;
  61.         $this->orderHelper $orderHelper;
  62.         $this->formFactory $formFactory;
  63.         $this->eventDispatcher $eventDispatcher;
  64.     }
  65.     /**
  66.      * @param SessionInterface $session
  67.      * @required
  68.      */
  69.     public function setSession(SessionInterface $session)
  70.     {
  71.         $this->session $session;
  72.     }
  73.     public function onLogoutSuccess(Request $request)
  74.     {
  75.         // $referer = $request->headers->get('referer');
  76.         log_info('ログアウト処理1');
  77.         $token $this->tokenStorage->getToken();
  78.         log_info('ログアウト処理2');
  79.         $isAdmin false;
  80.         $roleNames $token->getRoleNames();
  81.         if (!empty($roleNames) && in_array('ROLE_ADMIN'$roleNames)) {
  82.             $isAdmin true;
  83.         }
  84.         if ($isAdmin) {
  85.             $response parent::onLogoutSuccess($request);
  86.             $response = new RedirectResponse('/administrator/login');
  87.             $response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
  88.             return $response;
  89.         }
  90.         $User $token->getUser();
  91.         log_info('ログアウト処理3');
  92.         $aes_key getenv('API_AES_KEY');
  93.         log_info('ログアウト処理4');
  94.         $user_id openssl_encrypt($User['id'],'aes-256-ecb',$aes_key);
  95.         log_info('ログアウト処理5');
  96.         $client = new Client();
  97.         log_info('ログアウト処理6');
  98.         $options = [
  99.             'headers' => [
  100.                 'Content-Type' => 'application/x-www-form-urlencoded'],
  101.             'form_params' => [
  102.                 "site_id" => 1,
  103.                 "user_id" => $user_id
  104.                ]
  105.             ];
  106.         $url getenv('API_KV_LOGOUT');
  107.         $response $client->request('POST'$url$options);
  108.         
  109.         log_info('ログアウト処理7');
  110.         $res json_decode($response->getBody());
  111.         if(!$res->status){
  112.             // ログアウト失敗
  113.             $message "ログアウトに失敗しました";
  114.             // $message = "ログアウトに失敗しました(".openssl_encrypt('2', 'AES-256-ECB','E793555B02BA635B2FF01C99379AC8D6').")";
  115.             $this->session->getFlashBag()->add('eccube.front.logout'$message);
  116.         }
  117.         if($this->session->getFlashBag()->has('eccube.front.request.error')){
  118.             $response '/mypage/login';
  119.             return new RedirectResponse($response);
  120.         }
  121.         $response parent::onLogoutSuccess($request);
  122.         if ($this->context->isAdmin()) {
  123.             $response = new RedirectResponse('/administrator/login');
  124.             $response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
  125.             return $response;
  126.         }
  127.         $response '/mypage/login';
  128.         return new RedirectResponse($response);
  129.     }
  130. }