|
9 | 9 |
|
10 | 10 | namespace OxidEsales\GraphQL\Base\Api\Controller; |
11 | 11 |
|
12 | | -use OxidEsales\Eshop\Core\Registry; |
| 12 | +use GraphQL\Error\FormattedError; |
| 13 | +use OxidEsales\Eshop\Core\Registry as EshopRegistry; |
| 14 | +use OxidEsales\EshopCommunity\Core\Di\ContainerFacade; |
13 | 15 | use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Dao\ModuleConfigurationDaoInterface; |
| 16 | +use OxidEsales\GraphQL\Base\Exception\Error; |
| 17 | +use OxidEsales\GraphQL\Base\Exception\ErrorCategories; |
| 18 | +use OxidEsales\GraphQL\Base\Exception\InvalidRequest; |
| 19 | +use OxidEsales\GraphQL\Base\Framework\GraphQLQueryHandler; |
| 20 | +use OxidEsales\GraphQL\Base\Framework\RequestReader; |
| 21 | +use OxidEsales\GraphQL\Base\Framework\TimerHandler; |
| 22 | +use OxidEsales\GraphQL\Base\Service\Authentication as GraphQLAuthenticationService; |
14 | 23 | use Symfony\Component\HttpFoundation\JsonResponse; |
15 | 24 | use Symfony\Component\HttpFoundation\Response; |
16 | 25 | use Symfony\Component\Routing\Attribute\Route; |
17 | 26 | use Symfony\Component\Routing\Requirement\Requirement; |
18 | 27 |
|
19 | 28 | readonly class OxapiController |
20 | 29 | { |
21 | | - public function __construct(private ModuleConfigurationDaoInterface $moduleConfigurationDao) |
| 30 | + public const SESSION_ERROR_MESSAGE = 'OXID eShop PHP session spotted. Ensure you have skipSession=1 ' |
| 31 | + . 'parameter sent to the widget.php. For more information about the problem, check ' |
| 32 | + . 'Troubleshooting section in documentation.'; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly TimerHandler $timerHandler, |
| 36 | + private readonly GraphQLQueryHandler $queryHandler |
| 37 | + ) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + #[Route('/oxapi/{shp}/{lang}/', requirements: ['shp' => Requirement::DIGITS, 'lang' => Requirement::DIGITS], methods: ['GET', 'POST'])] |
| 42 | + public function process(int $shp, int $lang): Response |
| 43 | + { |
| 44 | + $_POST['shp'] = $shp; |
| 45 | + $_POST['lang'] = $lang; |
| 46 | + |
| 47 | + /** @var TimerHandler $timerHandler */ |
| 48 | + $this->timerHandler |
| 49 | + ->create('bootstrap') |
| 50 | + ->startAt($_SERVER['REQUEST_TIME_FLOAT']) |
| 51 | + ->stop(); |
| 52 | + |
| 53 | + $headers = [ |
| 54 | + 'Content-Type: application/json', |
| 55 | + $this->generateServerTimingHeader() |
| 56 | + ]; |
| 57 | + $status = 200; |
| 58 | + |
| 59 | + try { |
| 60 | + $this->handleShopSession(); |
| 61 | + $result = $this->queryHandler |
| 62 | + ->executeGraphQLQuery(); |
| 63 | + } catch (Error $e) { |
| 64 | + $result = [ |
| 65 | + 'errors' => [ |
| 66 | + FormattedError::createFromException($e) |
| 67 | + ] |
| 68 | + ]; |
| 69 | + if (!$this->isAuthenticated($e)) { |
| 70 | + $headers[] = 'WWW-Authenticate: Bearer'; |
| 71 | + } |
| 72 | + } catch (\Throwable $e) { |
| 73 | + EshopRegistry::getLogger()->error($e->getMessage(), [$e]); |
| 74 | + $result = [ |
| 75 | + 'errors' => [ |
| 76 | + FormattedError::createFromException($e) |
| 77 | + ] |
| 78 | + ]; |
| 79 | + $status = 500; |
| 80 | + } |
| 81 | + |
| 82 | + return new JsonResponse($result, $status, $headers); |
| 83 | + } |
| 84 | + |
| 85 | + private function handleShopSession(): void |
22 | 86 | { |
| 87 | + //if there's already a php session running, bail out to prevent inconsistent behaviour |
| 88 | + if (PHP_SESSION_NONE !== session_status()) { |
| 89 | + throw new InvalidRequest(self::SESSION_ERROR_MESSAGE); |
| 90 | + } |
| 91 | + |
| 92 | + $this->setShopUserFromToken(); |
| 93 | + } |
| 94 | + |
| 95 | + private function setShopUserFromToken(): void |
| 96 | + { |
| 97 | + $session = EshopRegistry::getSession(); |
| 98 | + $session->setUser(null); |
| 99 | + $session->setBasket(null); |
| 100 | + $session->setVariable('usr', null); |
| 101 | + |
| 102 | + $authentication = ContainerFacade::get(GraphQLAuthenticationService::class); |
| 103 | + $userId = $authentication->getUser()->id()->val(); |
| 104 | + |
| 105 | + if ($userId) { |
| 106 | + $session->setVariable('usr', $userId); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private function generateServerTimingHeader(): string |
| 111 | + { |
| 112 | + $timings = []; |
| 113 | + |
| 114 | + foreach ($this->timerHandler->getTimers() as $name => $timer) { |
| 115 | + $timings[] = sprintf('%s;dur=%.3f', $name, $timer->getDuration() * 1000); |
| 116 | + } |
| 117 | + |
| 118 | + return 'Server-Timing: ' . implode(',', $timings); |
23 | 119 | } |
24 | 120 |
|
25 | | - #[Route('/oxapi/{shp}/{lang}/', requirements: ['lang' => Requirement::DIGITS], methods: ['GET'])] |
26 | | - public function foo(int $shp, int $lang): Response |
| 121 | + private function isAuthenticated(Error $error): bool |
27 | 122 | { |
28 | | - #Registry::getConfig()->saveShopConfVar('string', 'testControllers', 'hello'); |
29 | | - return new JsonResponse( |
30 | | - [ |
31 | | - 'shp' => $shp, |
32 | | - 'lang' => $lang, |
33 | | - 'bla' => 'foo' |
34 | | - ] |
35 | | - ); |
| 123 | + return $error->getCategory() !== ErrorCategories::PERMISSIONERRORS; |
36 | 124 | } |
37 | 125 | } |
0 commit comments