ApiController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\HtmlResponse;
  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 HtmlResponse
  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 HtmlResponse("A new website has been created with root page uid=" . $rootUid);
  67. }
  68. /**
  69. * -- Target of the route 'site_update' --
  70. * >> Requires a query param named 'organization-id' (int)
  71. *
  72. * Update the settings of the organization's website
  73. *
  74. * @param ServerRequest $request
  75. * @return HtmlResponse
  76. * @throws \Exception
  77. */
  78. public function updateSiteConstantsAction(ServerRequest $request) {
  79. $this->assertIpAllowed();
  80. $organizationId = $this->getOrganizationId($request);
  81. $controller = new SiteController();
  82. $rootUid = $controller->updateSiteConstantsAction($organizationId);
  83. $this->logger->info(sprintf(
  84. "OtAdmin API: The website with root uid " . $rootUid . " has been updated " .
  85. " (organization: " . $organizationId . ")"));
  86. return new HtmlResponse("The website with root uid " . $rootUid . " has been updated");
  87. }
  88. /**
  89. * -- Target of the route 'site_delete' --
  90. * >> Requires a query param named 'organization-id' (int)
  91. *
  92. * Proceeds to a soft-deletion of the organization's website
  93. *
  94. * @param ServerRequest $request
  95. * @return HtmlResponse
  96. * @throws \Exception
  97. */
  98. public function deleteSiteAction(ServerRequest $request) {
  99. $this->assertIpAllowed();
  100. $organizationId = $this->getOrganizationId($request);
  101. $controller = new SiteController();
  102. $rootUid = $controller->deleteSiteAction($organizationId, false);
  103. $this->logger->info(sprintf(
  104. "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .
  105. " (organization: " . $organizationId . ")"));
  106. return new HtmlResponse("The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.");
  107. }
  108. /**
  109. * -- Target of the route 'site_undelete' --
  110. * >> Requires a query param named 'organization-id' (int)
  111. *
  112. * Restore a soft-deleted organization's website
  113. *
  114. * @param ServerRequest $request
  115. * @return HtmlResponse
  116. * @throws \Exception
  117. */
  118. public function undeleteSiteAction(ServerRequest $request) {
  119. $this->assertIpAllowed();
  120. $organizationId = $this->getOrganizationId($request);
  121. $controller = new SiteController();
  122. $rootUid = $controller->undeleteSiteAction($organizationId);
  123. $this->logger->info(sprintf(
  124. "OtAdmin API: The website with root uid " . $rootUid . " has been restored " .
  125. " (organization: " . $organizationId . ")"));
  126. return new HtmlResponse("The website with root uid " . $rootUid . " has been restored");
  127. }
  128. }