123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Webapi\Controller;
- use Magento\Framework\App\DeploymentConfig;
- use Magento\Framework\Config\ConfigOptionsListConstants;
- use Magento\Framework\Exception\AuthorizationException;
- use Magento\Framework\Webapi\Authorization;
- use Magento\Framework\Webapi\ErrorProcessor;
- use Magento\Framework\Webapi\Request;
- use Magento\Framework\Webapi\Rest\Request as RestRequest;
- use Magento\Framework\Webapi\Rest\Response as RestResponse;
- use Magento\Framework\Webapi\ServiceInputProcessor;
- use Magento\Store\Model\Store;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\Webapi\Controller\Rest\ParamsOverrider;
- use Magento\Webapi\Controller\Rest\Router;
- use Magento\Webapi\Controller\Rest\Router\Route;
- use Magento\Webapi\Controller\Rest\RequestProcessorPool;
- /**
- * Front controller for WebAPI REST area.
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class Rest implements \Magento\Framework\App\FrontControllerInterface
- {
- /**
- * Path for accessing REST API schema
- *
- * @deprecated 100.3.0
- */
- const SCHEMA_PATH = '/schema';
- /**
- * @var Router
- * @deprecated 100.1.0
- */
- protected $_router;
- /**
- * @var Route
- * @deprecated 100.1.0
- */
- protected $_route;
- /**
- * @var \Magento\Framework\Webapi\Rest\Request
- */
- protected $_request;
- /**
- * @var \Magento\Framework\Webapi\Rest\Response
- */
- protected $_response;
- /**
- * @var \Magento\Framework\ObjectManagerInterface
- */
- protected $_objectManager;
- /**
- * @var \Magento\Framework\App\State
- */
- protected $_appState;
- /**
- * @var Authorization
- * @deprecated 100.1.0
- */
- protected $authorization;
- /**
- * @var ServiceInputProcessor
- * @deprecated 100.1.0
- */
- protected $serviceInputProcessor;
- /**
- * @var \Magento\Framework\Webapi\ErrorProcessor
- */
- protected $_errorProcessor;
- /**
- * @var \Magento\Webapi\Controller\PathProcessor
- */
- protected $_pathProcessor;
- /**
- * @var \Magento\Framework\App\AreaList
- */
- protected $areaList;
- /**
- * @var \Magento\Framework\Session\Generic
- */
- protected $session;
- /**
- * @var ParamsOverrider
- * @deprecated 100.1.0
- */
- protected $paramsOverrider;
- /**
- * @var RequestProcessorPool
- */
- protected $requestProcessorPool;
- /**
- * @var StoreManagerInterface
- * @deprecated 100.1.0
- */
- private $storeManager;
- /**
- * Initialize dependencies
- *
- * @param RestRequest $request
- * @param RestResponse $response
- * @param Router $router
- * @param \Magento\Framework\ObjectManagerInterface $objectManager
- * @param \Magento\Framework\App\State $appState
- * @param Authorization $authorization
- * @param ServiceInputProcessor $serviceInputProcessor
- * @param ErrorProcessor $errorProcessor
- * @param PathProcessor $pathProcessor
- * @param \Magento\Framework\App\AreaList $areaList
- * @param ParamsOverrider $paramsOverrider
- * @param StoreManagerInterface $storeManager
- * @param RequestProcessorPool $requestProcessorPool
- *
- * TODO: Consider removal of warning suppression
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- RestRequest $request,
- RestResponse $response,
- Router $router,
- \Magento\Framework\ObjectManagerInterface $objectManager,
- \Magento\Framework\App\State $appState,
- Authorization $authorization,
- ServiceInputProcessor $serviceInputProcessor,
- ErrorProcessor $errorProcessor,
- PathProcessor $pathProcessor,
- \Magento\Framework\App\AreaList $areaList,
- ParamsOverrider $paramsOverrider,
- StoreManagerInterface $storeManager,
- RequestProcessorPool $requestProcessorPool
- ) {
- $this->_router = $router;
- $this->_request = $request;
- $this->_response = $response;
- $this->_objectManager = $objectManager;
- $this->_appState = $appState;
- $this->authorization = $authorization;
- $this->serviceInputProcessor = $serviceInputProcessor;
- $this->_errorProcessor = $errorProcessor;
- $this->_pathProcessor = $pathProcessor;
- $this->areaList = $areaList;
- $this->paramsOverrider = $paramsOverrider;
- $this->storeManager = $storeManager;
- $this->requestProcessorPool = $requestProcessorPool;
- }
- /**
- * Handle REST request
- *
- * Based on request decide is it schema request or API request and process accordingly.
- * Throws Exception in case if cannot be processed properly.
- *
- * @param \Magento\Framework\App\RequestInterface $request
- * @return \Magento\Framework\App\ResponseInterface
- */
- public function dispatch(\Magento\Framework\App\RequestInterface $request)
- {
- $path = $this->_pathProcessor->process($request->getPathInfo());
- $this->_request->setPathInfo($path);
- $this->areaList->getArea($this->_appState->getAreaCode())
- ->load(\Magento\Framework\App\Area::PART_TRANSLATE);
- try {
- $processor = $this->requestProcessorPool->getProcessor($this->_request);
- $processor->process($this->_request);
- } catch (\Exception $e) {
- $maskedException = $this->_errorProcessor->maskException($e);
- $this->_response->setException($maskedException);
- }
- return $this->_response;
- }
- /**
- * Check if current request is schema request.
- *
- * @return bool
- */
- protected function isSchemaRequest()
- {
- return $this->_request->getPathInfo() === self::SCHEMA_PATH;
- }
- /**
- * Retrieve current route.
- *
- * @return Route
- * @deprecated 100.1.0
- * @see \Magento\Webapi\Controller\Rest\InputParamsResolver::getRoute
- */
- protected function getCurrentRoute()
- {
- if (!$this->_route) {
- $this->_route = $this->_router->match($this->_request);
- }
- return $this->_route;
- }
- /**
- * Perform authentication and authorization.
- *
- * @throws \Magento\Framework\Exception\AuthorizationException
- * @return void
- * @deprecated 100.1.0
- * @see \Magento\Webapi\Controller\Rest\RequestValidator::checkPermissions
- */
- protected function checkPermissions()
- {
- $route = $this->getCurrentRoute();
- if (!$this->authorization->isAllowed($route->getAclResources())) {
- $params = ['resources' => implode(', ', $route->getAclResources())];
- throw new AuthorizationException(
- __("The consumer isn't authorized to access %resources.", $params)
- );
- }
- }
- /**
- * Validate request
- *
- * @throws AuthorizationException
- * @throws \Magento\Framework\Webapi\Exception
- * @return void
- * @deprecated 100.1.0
- * @see \Magento\Webapi\Controller\Rest\RequestValidator::validate
- */
- protected function validateRequest()
- {
- $this->checkPermissions();
- if ($this->getCurrentRoute()->isSecure() && !$this->_request->isSecure()) {
- throw new \Magento\Framework\Webapi\Exception(__('Operation allowed only in HTTPS'));
- }
- if ($this->storeManager->getStore()->getCode() === Store::ADMIN_CODE
- && strtoupper($this->_request->getMethod()) === RestRequest::HTTP_METHOD_GET
- ) {
- throw new \Magento\Framework\Webapi\Exception(__('Cannot perform GET operation with store code \'all\''));
- }
- }
- }
|