ApiController.php 5.5 KB

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