Rest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Controller;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\Config\ConfigOptionsListConstants;
  9. use Magento\Framework\Exception\AuthorizationException;
  10. use Magento\Framework\Webapi\Authorization;
  11. use Magento\Framework\Webapi\ErrorProcessor;
  12. use Magento\Framework\Webapi\Request;
  13. use Magento\Framework\Webapi\Rest\Request as RestRequest;
  14. use Magento\Framework\Webapi\Rest\Response as RestResponse;
  15. use Magento\Framework\Webapi\ServiceInputProcessor;
  16. use Magento\Store\Model\Store;
  17. use Magento\Store\Model\StoreManagerInterface;
  18. use Magento\Webapi\Controller\Rest\ParamsOverrider;
  19. use Magento\Webapi\Controller\Rest\Router;
  20. use Magento\Webapi\Controller\Rest\Router\Route;
  21. use Magento\Webapi\Controller\Rest\RequestProcessorPool;
  22. /**
  23. * Front controller for WebAPI REST area.
  24. *
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. * @SuppressWarnings(PHPMD.TooManyFields)
  27. */
  28. class Rest implements \Magento\Framework\App\FrontControllerInterface
  29. {
  30. /**
  31. * Path for accessing REST API schema
  32. *
  33. * @deprecated 100.3.0
  34. */
  35. const SCHEMA_PATH = '/schema';
  36. /**
  37. * @var Router
  38. * @deprecated 100.1.0
  39. */
  40. protected $_router;
  41. /**
  42. * @var Route
  43. * @deprecated 100.1.0
  44. */
  45. protected $_route;
  46. /**
  47. * @var \Magento\Framework\Webapi\Rest\Request
  48. */
  49. protected $_request;
  50. /**
  51. * @var \Magento\Framework\Webapi\Rest\Response
  52. */
  53. protected $_response;
  54. /**
  55. * @var \Magento\Framework\ObjectManagerInterface
  56. */
  57. protected $_objectManager;
  58. /**
  59. * @var \Magento\Framework\App\State
  60. */
  61. protected $_appState;
  62. /**
  63. * @var Authorization
  64. * @deprecated 100.1.0
  65. */
  66. protected $authorization;
  67. /**
  68. * @var ServiceInputProcessor
  69. * @deprecated 100.1.0
  70. */
  71. protected $serviceInputProcessor;
  72. /**
  73. * @var \Magento\Framework\Webapi\ErrorProcessor
  74. */
  75. protected $_errorProcessor;
  76. /**
  77. * @var \Magento\Webapi\Controller\PathProcessor
  78. */
  79. protected $_pathProcessor;
  80. /**
  81. * @var \Magento\Framework\App\AreaList
  82. */
  83. protected $areaList;
  84. /**
  85. * @var \Magento\Framework\Session\Generic
  86. */
  87. protected $session;
  88. /**
  89. * @var ParamsOverrider
  90. * @deprecated 100.1.0
  91. */
  92. protected $paramsOverrider;
  93. /**
  94. * @var RequestProcessorPool
  95. */
  96. protected $requestProcessorPool;
  97. /**
  98. * @var StoreManagerInterface
  99. * @deprecated 100.1.0
  100. */
  101. private $storeManager;
  102. /**
  103. * Initialize dependencies
  104. *
  105. * @param RestRequest $request
  106. * @param RestResponse $response
  107. * @param Router $router
  108. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  109. * @param \Magento\Framework\App\State $appState
  110. * @param Authorization $authorization
  111. * @param ServiceInputProcessor $serviceInputProcessor
  112. * @param ErrorProcessor $errorProcessor
  113. * @param PathProcessor $pathProcessor
  114. * @param \Magento\Framework\App\AreaList $areaList
  115. * @param ParamsOverrider $paramsOverrider
  116. * @param StoreManagerInterface $storeManager
  117. * @param RequestProcessorPool $requestProcessorPool
  118. *
  119. * TODO: Consider removal of warning suppression
  120. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  121. */
  122. public function __construct(
  123. RestRequest $request,
  124. RestResponse $response,
  125. Router $router,
  126. \Magento\Framework\ObjectManagerInterface $objectManager,
  127. \Magento\Framework\App\State $appState,
  128. Authorization $authorization,
  129. ServiceInputProcessor $serviceInputProcessor,
  130. ErrorProcessor $errorProcessor,
  131. PathProcessor $pathProcessor,
  132. \Magento\Framework\App\AreaList $areaList,
  133. ParamsOverrider $paramsOverrider,
  134. StoreManagerInterface $storeManager,
  135. RequestProcessorPool $requestProcessorPool
  136. ) {
  137. $this->_router = $router;
  138. $this->_request = $request;
  139. $this->_response = $response;
  140. $this->_objectManager = $objectManager;
  141. $this->_appState = $appState;
  142. $this->authorization = $authorization;
  143. $this->serviceInputProcessor = $serviceInputProcessor;
  144. $this->_errorProcessor = $errorProcessor;
  145. $this->_pathProcessor = $pathProcessor;
  146. $this->areaList = $areaList;
  147. $this->paramsOverrider = $paramsOverrider;
  148. $this->storeManager = $storeManager;
  149. $this->requestProcessorPool = $requestProcessorPool;
  150. }
  151. /**
  152. * Handle REST request
  153. *
  154. * Based on request decide is it schema request or API request and process accordingly.
  155. * Throws Exception in case if cannot be processed properly.
  156. *
  157. * @param \Magento\Framework\App\RequestInterface $request
  158. * @return \Magento\Framework\App\ResponseInterface
  159. */
  160. public function dispatch(\Magento\Framework\App\RequestInterface $request)
  161. {
  162. $path = $this->_pathProcessor->process($request->getPathInfo());
  163. $this->_request->setPathInfo($path);
  164. $this->areaList->getArea($this->_appState->getAreaCode())
  165. ->load(\Magento\Framework\App\Area::PART_TRANSLATE);
  166. try {
  167. $processor = $this->requestProcessorPool->getProcessor($this->_request);
  168. $processor->process($this->_request);
  169. } catch (\Exception $e) {
  170. $maskedException = $this->_errorProcessor->maskException($e);
  171. $this->_response->setException($maskedException);
  172. }
  173. return $this->_response;
  174. }
  175. /**
  176. * Check if current request is schema request.
  177. *
  178. * @return bool
  179. */
  180. protected function isSchemaRequest()
  181. {
  182. return $this->_request->getPathInfo() === self::SCHEMA_PATH;
  183. }
  184. /**
  185. * Retrieve current route.
  186. *
  187. * @return Route
  188. * @deprecated 100.1.0
  189. * @see \Magento\Webapi\Controller\Rest\InputParamsResolver::getRoute
  190. */
  191. protected function getCurrentRoute()
  192. {
  193. if (!$this->_route) {
  194. $this->_route = $this->_router->match($this->_request);
  195. }
  196. return $this->_route;
  197. }
  198. /**
  199. * Perform authentication and authorization.
  200. *
  201. * @throws \Magento\Framework\Exception\AuthorizationException
  202. * @return void
  203. * @deprecated 100.1.0
  204. * @see \Magento\Webapi\Controller\Rest\RequestValidator::checkPermissions
  205. */
  206. protected function checkPermissions()
  207. {
  208. $route = $this->getCurrentRoute();
  209. if (!$this->authorization->isAllowed($route->getAclResources())) {
  210. $params = ['resources' => implode(', ', $route->getAclResources())];
  211. throw new AuthorizationException(
  212. __("The consumer isn't authorized to access %resources.", $params)
  213. );
  214. }
  215. }
  216. /**
  217. * Validate request
  218. *
  219. * @throws AuthorizationException
  220. * @throws \Magento\Framework\Webapi\Exception
  221. * @return void
  222. * @deprecated 100.1.0
  223. * @see \Magento\Webapi\Controller\Rest\RequestValidator::validate
  224. */
  225. protected function validateRequest()
  226. {
  227. $this->checkPermissions();
  228. if ($this->getCurrentRoute()->isSecure() && !$this->_request->isSecure()) {
  229. throw new \Magento\Framework\Webapi\Exception(__('Operation allowed only in HTTPS'));
  230. }
  231. if ($this->storeManager->getStore()->getCode() === Store::ADMIN_CODE
  232. && strtoupper($this->_request->getMethod()) === RestRequest::HTTP_METHOD_GET
  233. ) {
  234. throw new \Magento\Framework\Webapi\Exception(__('Cannot perform GET operation with store code \'all\''));
  235. }
  236. }
  237. }