app/Customize/Controller/Mypage/MypageController.php line 63

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller\Mypage;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Entity\Customer;
  5. use Eccube\Form\Type\Front\CustomerLoginType;
  6. use Eccube\Event\EccubeEvents;
  7. use Eccube\Event\EventArgs;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Customize\Helper\BaseHelper;
  14. use Customize\Service\LogoutDebugService;
  15. class MypageController extends AbstractController
  16. {
  17.     use BaseHelper;
  18.     /**
  19.      * @var TokenStorageInterface
  20.      */
  21.     protected TokenStorageInterface $tokenStorage;
  22.     /**
  23.      * MypageController constructor.
  24.      *
  25.      * @param TokenStorageInterface $tokenStorage
  26.      */
  27.     public function __construct(TokenStorageInterface $tokenStorage)
  28.     {
  29.         $this->tokenStorage $tokenStorage;
  30.     }
  31.     /**
  32.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  33.      * @Template("Mypage/login.twig")
  34.      */
  35.     public function login(Request $requestAuthenticationUtils $utils)
  36.     {
  37.         $redirect $_GET['redirect'] ?? null;
  38.         $this->session->set('redirect'$redirect);
  39.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  40.             log_info('認証済のためログイン処理をスキップ');
  41.             if ($myPageUrl = (env('MY_PAGE_URL') ?? null)) {
  42.                 if ($this->session->get('redirect'null) === $myPageUrl) {
  43.                     $redirectMyPage $this->generateRedirectMyPage();
  44.                     $this->session->set('has_redirect_mypage'true);
  45.                     return $this->redirect($redirectMyPage);
  46.                 }
  47.             }
  48.             return $this->redirectToRoute('mypage');
  49.         }
  50.         /* @var $form \Symfony\Component\Form\FormInterface */
  51.         $builder $this->formFactory
  52.             ->createNamedBuilder(''CustomerLoginType::class);
  53.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  54.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  55.             $Customer $this->getUser();
  56.             if ($Customer instanceof Customer) {
  57.                 $builder->get('login_email')
  58.                     ->setData($Customer->getEmail());
  59.             }
  60.         }
  61.         $event = new EventArgs(
  62.             [
  63.                 'builder' => $builder,
  64.             ],
  65.             $request
  66.         );
  67.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  68.         $form $builder->getForm();
  69.         return [
  70.             'error' => $utils->getLastAuthenticationError(),
  71.             'form' => $form->createView(),
  72.             'route' => 'mypage_login',
  73.         ];
  74.     }
  75.     /**
  76.      * @Route("/logout", name="logout", methods={"GET"})
  77.      */
  78.     public function logout(Request $request)
  79.     {
  80.         // --- Log bằng service ---
  81.         $debugService = new LogoutDebugService();
  82.         $user $this->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser() : null;
  83.         $debugService->logLogoutAttempt($request$user);
  84.         // --- Xoá session ---
  85.         $this->tokenStorage->setToken(null);
  86.         $this->session->invalidate();
  87.         // --- Lấy iframe links ---
  88.         $iframeLinks = [];
  89.         $envLinks env('SSO_LOGOUT_LINKS');
  90.         if ($envLinks) {
  91.             $iframeLinks array_map('trim'explode(','$envLinks));
  92.         }
  93.         // --- Tạo response ---
  94.         $response $this->render('Mypage/logout.twig', [
  95.             'iframe_links' => $iframeLinks,
  96.         ]);
  97.         $host $request->getHost();
  98.         $isSecure = ($host === 'localhost' false $request->isSecure());
  99.         $response->headers->clearCookie(
  100.             'eccube_remember_me',
  101.             '/',
  102.             $host,
  103.             $isSecure,
  104.             true,  // HttpOnly
  105.             false// Raw
  106.             null   // SameSite
  107.         );
  108.         // --- 2. Xóa cookie với domain null (để đảm bảo xóa được) ---
  109.         $response->headers->clearCookie(
  110.             'eccube_remember_me',
  111.             '/',
  112.             null,  // Domain null
  113.             $isSecure,
  114.             true,  // HttpOnly
  115.             false// Raw
  116.             null   // SameSite
  117.         );
  118.         // --- 3. Xóa session login_memory ---
  119.         $this->session->remove('_security.login_memory');
  120.         // --- 4. Log để debug ---
  121.         error_log('MypageController: Đã xóa cookie remember_me với host: ' $host ', secure: ' . ($isSecure 'true' 'false'));
  122.         return $response;
  123.     }
  124. }