ApiController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_infos' --
  70. *
  71. * Return the main informations about the organization's website
  72. *
  73. * @param ServerRequest $request
  74. * @return JsonResponse
  75. * @throws \Exception
  76. */
  77. public function getSiteInfosAction(ServerRequest $request) {
  78. $this->assertIpAllowed();
  79. $organizationId = $this->getOrganizationId($request);
  80. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  81. $infos = $controller->getSiteInfosAction($organizationId);
  82. return new JsonResponse($infos);
  83. }
  84. /**
  85. * -- Target of the route 'site_create' --
  86. * >> Requires a query param named 'organization-id' (int)
  87. *
  88. * Create the organization's website
  89. *
  90. * @param ServerRequest $request
  91. * @return JsonResponse
  92. * @throws \Exception
  93. */
  94. public function createSiteAction(ServerRequest $request) {
  95. $this->assertIpAllowed();
  96. $organizationId = $this->getOrganizationId($request);
  97. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  98. $rootUid = $controller->createSiteAction($organizationId);
  99. $this->logger->info(sprintf(
  100. "OtAdmin API: A new website has been created with root page uid=" . $rootUid .
  101. " for the organization " . $organizationId));
  102. return new JsonResponse(
  103. [
  104. 'organization_id' => $organizationId,
  105. 'msg' => "A new website has been created with root page uid=" . $rootUid,
  106. 'root_uid' => $rootUid
  107. ]
  108. );
  109. }
  110. /**
  111. * -- Target of the route 'site_update' --
  112. * >> Requires a query param named 'organization-id' (int)
  113. *
  114. * Update the settings of the organization's website
  115. *
  116. * @param ServerRequest $request
  117. * @return JsonResponse
  118. * @throws \Exception
  119. */
  120. public function updateSiteConstantsAction(ServerRequest $request) {
  121. $this->assertIpAllowed();
  122. $organizationId = $this->getOrganizationId($request);
  123. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  124. $rootUid = $controller->updateSiteConstantsAction($organizationId);
  125. $this->logger->info(sprintf(
  126. "OtAdmin API: The website with root uid " . $rootUid . " has been updated " .
  127. " (organization: " . $organizationId . ")"));
  128. return new JsonResponse(
  129. [
  130. 'organization_id' => $organizationId,
  131. 'msg' => "The website with root uid " . $rootUid . " has been updated",
  132. 'root_uid' => $rootUid
  133. ]
  134. );
  135. }
  136. /**
  137. * -- Target of the route 'site_delete' --
  138. * >> Requires a query param named 'organization-id' (int)
  139. *
  140. * Proceeds to a soft-deletion of the organization's website
  141. *
  142. * @param ServerRequest $request
  143. * @return JsonResponse
  144. * @throws \Exception
  145. */
  146. public function deleteSiteAction(ServerRequest $request) {
  147. $this->assertIpAllowed();
  148. $organizationId = $this->getOrganizationId($request);
  149. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  150. $rootUid = $controller->deleteSiteAction($organizationId, false);
  151. $this->logger->info(sprintf(
  152. "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .
  153. " (organization: " . $organizationId . ")"));
  154. return new JsonResponse(
  155. [
  156. 'organization_id' => $organizationId,
  157. 'msg' => "The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.",
  158. 'root_uid' => $rootUid
  159. ]
  160. );
  161. }
  162. /**
  163. * -- Target of the route 'site_undelete' --
  164. * >> Requires a query param named 'organization-id' (int)
  165. *
  166. * Restore a soft-deleted organization's website
  167. *
  168. * @param ServerRequest $request
  169. * @return JsonResponse
  170. * @throws \Exception
  171. */
  172. public function undeleteSiteAction(ServerRequest $request) {
  173. $this->assertIpAllowed();
  174. $organizationId = $this->getOrganizationId($request);
  175. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  176. $rootUid = $controller->undeleteSiteAction($organizationId);
  177. $this->logger->info(sprintf(
  178. "OtAdmin API: The website with root uid " . $rootUid . " has been restored " .
  179. " (organization: " . $organizationId . ")"));
  180. return new JsonResponse(
  181. [
  182. 'organization_id' => $organizationId,
  183. 'msg' => "The website with root uid " . $rootUid . " has been restored",
  184. 'root_uid' => $rootUid
  185. ]
  186. );
  187. }
  188. /**
  189. * -- Target of the route 'site_clearcache' --
  190. * >> Requires a query param named 'organization-id' (int)
  191. *
  192. * Clear the cache of the organization's website
  193. *
  194. * @param ServerRequest $request
  195. * @return JsonResponse
  196. * @throws \Exception
  197. */
  198. public function clearSiteCacheAction(ServerRequest $request) {
  199. $this->assertIpAllowed();
  200. $organizationId = $this->getOrganizationId($request);
  201. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  202. $rootUid = $controller->clearSiteCacheAction($organizationId);
  203. return new JsonResponse(
  204. [
  205. 'organization_id' => $organizationId,
  206. 'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
  207. 'root_uid' => $rootUid
  208. ]
  209. );
  210. }
  211. /**
  212. * -- Target of the route 'site_set_domain' --
  213. * >> Requires a query param named 'organization-id' (int)
  214. *
  215. * Set a new domain for the organization website
  216. *
  217. * @param ServerRequest $request
  218. * @return JsonResponse
  219. * @throws \Exception
  220. */
  221. public function setSiteDomainAction(ServerRequest $request) {
  222. $this->assertIpAllowed();
  223. $organizationId = $this->getOrganizationId($request);
  224. $queryParams = $request->getQueryParams();
  225. $domain = $queryParams['domain'];
  226. if (!$domain) {
  227. throw new \RuntimeException("Missing 'domain' parameter");
  228. }
  229. $redirect = (isset($queryParams['redirect']) && $queryParams['redirect']);
  230. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  231. $rootUid = $controller->setSiteDomainAction($organizationId, $domain, $redirect);
  232. return new JsonResponse(
  233. [
  234. 'organization_id' => $organizationId,
  235. 'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
  236. 'root_uid' => $rootUid
  237. ]
  238. );
  239. }
  240. /**
  241. * -- Target of the route 'site_status' --
  242. * >> Requires a query param named 'organization-id' (int)
  243. *
  244. * Returns the current status of the website
  245. *
  246. * @param ServerRequest $request
  247. * @return JsonResponse
  248. * @throws \Exception
  249. */
  250. public function getSiteStatusAction(ServerRequest $request) {
  251. $this->assertIpAllowed();
  252. $organizationId = $this->getOrganizationId($request);
  253. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  254. $queryParams = $request->getQueryParams();
  255. $full = (isset($queryParams['full']) && $queryParams['full']);
  256. $status = $controller->getSiteStatusAction($organizationId, $full);
  257. return new JsonResponse($status);
  258. }
  259. /**
  260. * -- Target of the route 'scan' --
  261. *
  262. * Scan the whole Typo3 database and return the results
  263. *
  264. * @param ServerRequest $request
  265. * @return JsonResponse
  266. * @throws \Exception
  267. */
  268. public function scanAllAction(ServerRequest $request) {
  269. $this->assertIpAllowed();
  270. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(ScanController::class);
  271. $queryParams = $request->getQueryParams();
  272. $full = (isset($queryParams['full']) && $queryParams['full']);
  273. $results = $controller->scanAllAction($full);
  274. return new JsonResponse($results);
  275. }
  276. }