<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Security\Http\Authentication;
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Eccube\Request\Context;
use Eccube\Service\SystemService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler;
use Eccube\Service\OrderHelper;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Eccube\Service\CartService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
class EccubeLogoutSuccessHandler extends DefaultLogoutSuccessHandler
{
/**
* @var Session
*/
protected $session;
/** @var Context */
protected $context;
private $orderHelper;
/**
* @var FormFactoryInterface
*/
protected $formFactory;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
public function __construct(
HttpUtils $httpUtils,
Context $context,
$targetUrl = '/',
TokenStorageInterface $tokenStorage,
CartService $cartService,
OrderHelper $orderHelper,
FormFactoryInterface $formFactory,
EventDispatcherInterface $eventDispatcher
)
{
parent::__construct($httpUtils, $targetUrl);
$this->context = $context;
$this->tokenStorage = $tokenStorage;
$this->cartService = $cartService;
$this->orderHelper = $orderHelper;
$this->formFactory = $formFactory;
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param SessionInterface $session
* @required
*/
public function setSession(SessionInterface $session)
{
$this->session = $session;
}
public function onLogoutSuccess(Request $request)
{
// $referer = $request->headers->get('referer');
log_info('ログアウト処理1');
$token = $this->tokenStorage->getToken();
log_info('ログアウト処理2');
$isAdmin = false;
$roleNames = $token->getRoleNames();
if (!empty($roleNames) && in_array('ROLE_ADMIN', $roleNames)) {
$isAdmin = true;
}
if ($isAdmin) {
$response = parent::onLogoutSuccess($request);
$response = new RedirectResponse('/administrator/login');
$response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
return $response;
}
$User = $token->getUser();
log_info('ログアウト処理3');
$aes_key = getenv('API_AES_KEY');
log_info('ログアウト処理4');
$user_id = openssl_encrypt($User['id'],'aes-256-ecb',$aes_key);
log_info('ログアウト処理5');
$client = new Client();
log_info('ログアウト処理6');
$options = [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
"site_id" => 1,
"user_id" => $user_id
]
];
$url = getenv('API_KV_LOGOUT');
$response = $client->request('POST', $url, $options);
log_info('ログアウト処理7');
$res = json_decode($response->getBody());
if(!$res->status){
// ログアウト失敗
$message = "ログアウトに失敗しました";
// $message = "ログアウトに失敗しました(".openssl_encrypt('2', 'AES-256-ECB','E793555B02BA635B2FF01C99379AC8D6').")";
$this->session->getFlashBag()->add('eccube.front.logout', $message);
}
if($this->session->getFlashBag()->has('eccube.front.request.error')){
$response = '/mypage/login';
return new RedirectResponse($response);
}
$response = parent::onLogoutSuccess($request);
if ($this->context->isAdmin()) {
$response = new RedirectResponse('/administrator/login');
$response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
return $response;
}
$response = '/mypage/login';
return new RedirectResponse($response);
}
}