ApiController.php 5.9 KB

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