src/Controller/Front/Api/Session/SessionController.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Api\Session;
  4. use App\Application\Service\Customer\CustomerGroupService;
  5. use App\Application\Service\Helper\CookieHelper;
  6. use App\Application\Service\Helper\ResponseHelper;
  7. use App\Application\Service\Services\ServiceCustomerService;
  8. use App\Application\Service\Session\SessionService;
  9. use App\Entity\System\Customer;
  10. use App\Entity\System\Service;
  11. use App\Factory\Account\SessionResponseFactory;
  12. use App\Manager\System\CatalogManager;
  13. use App\Service\Customer\CustomerPackService;
  14. use App\Service\SecurityService;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Component\HttpFoundation\Cookie;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class SessionController extends AbstractController
  22. {
  23. private CustomerPackService $customerPackService;
  24. private CatalogManager $catalogManager;
  25. private SecurityService $securityService;
  26. private CustomerGroupService $customerGroupService;
  27. private ServiceCustomerService $serviceCustomerService;
  28. private SessionService $sessionService;
  29. private ParameterBagInterface $parameterBag;
  30. public function __construct(
  31. CustomerPackService $customerPackService,
  32. CatalogManager $catalogManager,
  33. SecurityService $securityService,
  34. CustomerGroupService $customerGroupService,
  35. ServiceCustomerService $serviceCustomerService,
  36. SessionService $sessionService,
  37. ParameterBagInterface $parameterBag
  38. ) {
  39. $this->customerPackService = $customerPackService;
  40. $this->catalogManager = $catalogManager;
  41. $this->securityService = $securityService;
  42. $this->customerGroupService = $customerGroupService;
  43. $this->serviceCustomerService = $serviceCustomerService;
  44. $this->sessionService = $sessionService;
  45. $this->parameterBag = $parameterBag;
  46. }
  47. /**
  48. * @Route("/front/api/session/check", name="front_api_customer_check_cookie", methods={"GET"})
  49. * @Route("/{lang}/front/api/session/check", name="front_api_customer_check_cookie_lang", methods={"GET"})
  50. */
  51. public function check(Request $request): Response
  52. {
  53. $language = $this->sessionService->getLocale();
  54. $session = $request->getSession();
  55. $sessionName = $this->parameterBag->get('session_name');
  56. $cookieValue = $request->cookies->get($sessionName);
  57. if ($cookieValue === null) {
  58. $cookieValue = CookieHelper::generateValue();
  59. }
  60. $expireTime = \time() + 172800;
  61. $parentDomain = $this->parameterBag->get('parent_domain');
  62. $cookie = new Cookie($sessionName, $cookieValue, $expireTime, '/', $parentDomain);
  63. if (!$session->isStarted()) {
  64. $response = new Response(
  65. 'Session is not started',
  66. Response::HTTP_NO_CONTENT,
  67. ['content-type' => 'application/json']
  68. );
  69. ResponseHelper::preserveExistingCookies($response, $request->cookies->all(), $expireTime, $sessionName, $parentDomain);
  70. $response->headers->setCookie($cookie);
  71. return $response;
  72. }
  73. $lastUsed = $session->getMetadataBag()->getLastUsed();
  74. $maxLifetime = $session->getMetadataBag()->getLifetime();
  75. if (\time() - $lastUsed > $maxLifetime) {
  76. $session->invalidate();
  77. $response = new Response(
  78. 'Session has expired',
  79. Response::HTTP_NO_CONTENT,
  80. ['content-type' => 'application/json']
  81. );
  82. ResponseHelper::preserveExistingCookies($response, $request->cookies->all(), $expireTime, $sessionName, $parentDomain);
  83. $response->headers->setCookie($cookie);
  84. return $response;
  85. }
  86. $user = $this->securityService->findUser();
  87. if (!$user instanceof Customer) {
  88. $response = new Response(
  89. 'Session data not found',
  90. Response::HTTP_NO_CONTENT,
  91. ['content-type' => 'application/json']
  92. );
  93. ResponseHelper::preserveExistingCookies($response, $request->cookies->all(), $expireTime, $sessionName, $parentDomain);
  94. $response->headers->setCookie($cookie);
  95. return $response;
  96. }
  97. $customerId = $user->getId();
  98. $email = $user->getEmail();
  99. $name = $user->getName();
  100. $surname = $user->getSurnames();
  101. $isPartialCreation = $user->getPartialCreation() > 0;
  102. $languageId = $user->getLanguage()->getId();
  103. $showWholesalePrice = $this->customerGroupService->customerHasProfitWholesale($user->getId());
  104. $canDownloadProductCsv = $this->customerGroupService->customerHasProfitWholesale($user->getId())
  105. && $this->serviceCustomerService->customerHasService($customerId, Service::DOWNLOAD_PRODUCT_LIST);
  106. $customerPackInfo = $this->customerPackService->getCustomerPackInfo($customerId, $languageId);
  107. $customerCatalogReferences = $this->catalogManager->findCatalogReferencesByCustomerId($customerId);
  108. $customerCatalogNames = $this->catalogManager->findCatalogNamesByCustomerId($customerId);
  109. $isImpersonated = $this->securityService->isImpersonated();
  110. $adminPanel = $this->securityService->customerAllowedToLoginAsEmployee($user);
  111. $sessionResponse = SessionResponseFactory::build(
  112. $customerId,
  113. $email,
  114. $name,
  115. $surname,
  116. $language,
  117. $customerPackInfo,
  118. $isPartialCreation,
  119. $isImpersonated,
  120. $customerCatalogReferences,
  121. $customerCatalogNames,
  122. $canDownloadProductCsv,
  123. $showWholesalePrice,
  124. $adminPanel
  125. );
  126. return new Response(\json_encode($sessionResponse), Response::HTTP_OK, ['content-type' => 'application/json']);
  127. }
  128. }