ApiController.php 8.7 KB

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