ApiController.php 6.0 KB

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