ApiController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Opentalent\OtAdmin\Http;
  3. use Opentalent\OtAdmin\Controller\SiteController;
  4. use Psr\Log\LoggerAwareInterface;
  5. use Psr\Log\LoggerAwareTrait;
  6. use TYPO3\CMS\Core\Core\Bootstrap;
  7. use TYPO3\CMS\Core\Http\JsonResponse;
  8. use TYPO3\CMS\Core\Http\ServerRequest;
  9. use TYPO3\CMS\Core\Utility\GeneralUtility;
  10. use TYPO3\CMS\Extbase\Object\ObjectManager;
  11. class ApiController implements LoggerAwareInterface
  12. {
  13. use LoggerAwareTrait;
  14. const ALLOWED_IPS = [
  15. '/^127\.0\.0\.[0-1]$/',
  16. '/^localhost$/',
  17. '/^10\.8\.0\.\d{1,3}$/',
  18. '/^80\.245\.24\.68$/', // prod-front
  19. '/^80\.245\.24\.70$/', // prod-back
  20. '/^80\.245\.24\.72$/', // test
  21. '/^80\.245\.24\.74$/' // preprod
  22. ];
  23. /**
  24. * Returns true if the client Ip is allowed
  25. *
  26. * @param string $clientIp
  27. * @return bool
  28. */
  29. public static function isIpAllowed(string $clientIp) {
  30. foreach (self::ALLOWED_IPS as $ipRule) {
  31. if (preg_match($ipRule, $clientIp)) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. /**
  38. * Check that the client Ip is allowed, else throw a Runtime error
  39. *
  40. * @return bool
  41. */
  42. private function assertIpAllowed() {
  43. $clientIp = $_SERVER['REMOTE_ADDR'];
  44. if (!self::isIpAllowed($clientIp)){
  45. $route = $_REQUEST['route'];
  46. $this->logger->error(sprintf(
  47. "OtAdmin API: an attempt was made to call the route " .
  48. $route . " from an non-allowed IP (" . $clientIp . ")"));
  49. throw new \RuntimeException("Not allowed");
  50. }
  51. return true;
  52. }
  53. /**
  54. * Retrieve the organization's id from the given request parameters
  55. *
  56. * @param ServerRequest $request
  57. * @return int
  58. */
  59. private function getOrganizationId(ServerRequest $request) {
  60. $params = $request->getQueryParams();
  61. $organizationId = $params['organization-id'];
  62. if (!$organizationId) {
  63. throw new \RuntimeException("Missing parameter: 'organization-id'");
  64. }
  65. return (int)$organizationId;
  66. }
  67. /**
  68. * -- Target of the route 'site_create' --
  69. * >> Requires a query param named 'organization-id' (int)
  70. *
  71. * Create the organization's website
  72. *
  73. * @param ServerRequest $request
  74. * @return JsonResponse
  75. * @throws \Exception
  76. */
  77. public function createSiteAction(ServerRequest $request) {
  78. $this->assertIpAllowed();
  79. $organizationId = $this->getOrganizationId($request);
  80. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  81. $rootUid = $controller->createSiteAction($organizationId);
  82. $this->logger->info(sprintf(
  83. "OtAdmin API: A new website has been created with root page uid=" . $rootUid .
  84. " for the organization " . $organizationId));
  85. return new JsonResponse(
  86. [
  87. 'organization_id' => $organizationId,
  88. 'msg' => "A new website has been created with root page uid=" . $rootUid,
  89. 'root_uid' => $rootUid
  90. ]
  91. );
  92. }
  93. /**
  94. * -- Target of the route 'site_update' --
  95. * >> Requires a query param named 'organization-id' (int)
  96. *
  97. * Update the settings of the organization's website
  98. *
  99. * @param ServerRequest $request
  100. * @return JsonResponse
  101. * @throws \Exception
  102. */
  103. public function updateSiteConstantsAction(ServerRequest $request) {
  104. $this->assertIpAllowed();
  105. $organizationId = $this->getOrganizationId($request);
  106. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  107. $rootUid = $controller->updateSiteConstantsAction($organizationId);
  108. $this->logger->info(sprintf(
  109. "OtAdmin API: The website with root uid " . $rootUid . " has been updated " .
  110. " (organization: " . $organizationId . ")"));
  111. return new JsonResponse(
  112. [
  113. 'organization_id' => $organizationId,
  114. 'msg' => "The website with root uid " . $rootUid . " has been updated",
  115. 'root_uid' => $rootUid
  116. ]
  117. );
  118. }
  119. /**
  120. * -- Target of the route 'site_delete' --
  121. * >> Requires a query param named 'organization-id' (int)
  122. *
  123. * Proceeds to a soft-deletion of the organization's website
  124. *
  125. * @param ServerRequest $request
  126. * @return JsonResponse
  127. * @throws \Exception
  128. */
  129. public function deleteSiteAction(ServerRequest $request) {
  130. $this->assertIpAllowed();
  131. $organizationId = $this->getOrganizationId($request);
  132. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  133. $rootUid = $controller->deleteSiteAction($organizationId, false);
  134. $this->logger->info(sprintf(
  135. "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .
  136. " (organization: " . $organizationId . ")"));
  137. return new JsonResponse(
  138. [
  139. 'organization_id' => $organizationId,
  140. 'msg' => "The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.",
  141. 'root_uid' => $rootUid
  142. ]
  143. );
  144. }
  145. /**
  146. * -- Target of the route 'site_undelete' --
  147. * >> Requires a query param named 'organization-id' (int)
  148. *
  149. * Restore a soft-deleted organization's website
  150. *
  151. * @param ServerRequest $request
  152. * @return JsonResponse
  153. * @throws \Exception
  154. */
  155. public function undeleteSiteAction(ServerRequest $request) {
  156. $this->assertIpAllowed();
  157. $organizationId = $this->getOrganizationId($request);
  158. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  159. $rootUid = $controller->undeleteSiteAction($organizationId);
  160. $this->logger->info(sprintf(
  161. "OtAdmin API: The website with root uid " . $rootUid . " has been restored " .
  162. " (organization: " . $organizationId . ")"));
  163. return new JsonResponse(
  164. [
  165. 'organization_id' => $organizationId,
  166. 'msg' => "The website with root uid " . $rootUid . " has been restored",
  167. 'root_uid' => $rootUid
  168. ]
  169. );
  170. }
  171. /**
  172. * -- Target of the route 'site_clearcache' --
  173. * >> Requires a query param named 'organization-id' (int)
  174. *
  175. * Clear the cache of the organization's website
  176. *
  177. * @param ServerRequest $request
  178. * @return JsonResponse
  179. * @throws \Exception
  180. */
  181. public function clearSiteCacheAction(ServerRequest $request) {
  182. $this->assertIpAllowed();
  183. $organizationId = $this->getOrganizationId($request);
  184. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  185. $rootUid = $controller->clearSiteCacheAction($organizationId);
  186. return new JsonResponse(
  187. [
  188. 'organization_id' => $organizationId,
  189. 'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
  190. 'root_uid' => $rootUid
  191. ]
  192. );
  193. }
  194. }