<?php
namespace Customize\Controller\Mypage;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Customer;
use Eccube\Form\Type\Front\CustomerLoginType;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Customize\Helper\BaseHelper;
use Customize\Service\LogoutDebugService;
class MypageController extends AbstractController
{
use BaseHelper;
/**
* @var TokenStorageInterface
*/
protected TokenStorageInterface $tokenStorage;
/**
* MypageController constructor.
*
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
* @Template("Mypage/login.twig")
*/
public function login(Request $request, AuthenticationUtils $utils)
{
$redirect = $_GET['redirect'] ?? null;
$this->session->set('redirect', $redirect);
if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
log_info('認証済のためログイン処理をスキップ');
if ($myPageUrl = (env('MY_PAGE_URL') ?? null)) {
if ($this->session->get('redirect', null) === $myPageUrl) {
$redirectMyPage = $this->generateRedirectMyPage();
$this->session->set('has_redirect_mypage', true);
return $this->redirect($redirectMyPage);
}
}
return $this->redirectToRoute('mypage');
}
/* @var $form \Symfony\Component\Form\FormInterface */
$builder = $this->formFactory
->createNamedBuilder('', CustomerLoginType::class);
$builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$Customer = $this->getUser();
if ($Customer instanceof Customer) {
$builder->get('login_email')
->setData($Customer->getEmail());
}
}
$event = new EventArgs(
[
'builder' => $builder,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
$form = $builder->getForm();
return [
'error' => $utils->getLastAuthenticationError(),
'form' => $form->createView(),
'route' => 'mypage_login',
];
}
/**
* @Route("/logout", name="logout", methods={"GET"})
*/
public function logout(Request $request)
{
// --- Log bằng service ---
$debugService = new LogoutDebugService();
$user = $this->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser() : null;
$debugService->logLogoutAttempt($request, $user);
// --- Xoá session ---
$this->tokenStorage->setToken(null);
$this->session->invalidate();
// --- Lấy iframe links ---
$iframeLinks = [];
$envLinks = env('SSO_LOGOUT_LINKS');
if ($envLinks) {
$iframeLinks = array_map('trim', explode(',', $envLinks));
}
// --- Tạo response ---
$response = $this->render('Mypage/logout.twig', [
'iframe_links' => $iframeLinks,
]);
$host = $request->getHost();
$isSecure = ($host === 'localhost' ? false : $request->isSecure());
$response->headers->clearCookie(
'eccube_remember_me',
'/',
$host,
$isSecure,
true, // HttpOnly
false, // Raw
null // SameSite
);
// --- 2. Xóa cookie với domain null (để đảm bảo xóa được) ---
$response->headers->clearCookie(
'eccube_remember_me',
'/',
null, // Domain null
$isSecure,
true, // HttpOnly
false, // Raw
null // SameSite
);
// --- 3. Xóa session login_memory ---
$this->session->remove('_security.login_memory');
// --- 4. Log để debug ---
error_log('MypageController: Đã xóa cookie remember_me với host: ' . $host . ', secure: ' . ($isSecure ? 'true' : 'false'));
return $response;
}
}