ApiController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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\Http\JsonResponse;
  8. use TYPO3\CMS\Core\Http\ServerRequest;
  9. use TYPO3\CMS\Core\Utility\GeneralUtility;
  10. use TYPO3\CMS\Extbase\Object\ObjectManager;
  11. /**
  12. * Actions for Http API calls
  13. *
  14. * @package Opentalent\OtAdmin\Http
  15. */
  16. class ApiController implements LoggerAwareInterface
  17. {
  18. use LoggerAwareTrait;
  19. const ALLOWED_IPS = [
  20. '/^127\.0\.0\.[0-1]$/',
  21. '/^localhost$/',
  22. '/^10\.8\.0\.\d{1,3}$/',
  23. '/^80\.245\.24\.68$/', // prod-front
  24. '/^80\.245\.24\.70$/', // prod-back
  25. '/^80\.245\.24\.72$/', // test
  26. '/^80\.245\.24\.74$/', // preprod
  27. '/^172\.1[6-9]\.\d{1,3}\.\d{1,3}$/', // local (docker)
  28. '/^172\.2[0-9]\.\d{1,3}\.\d{1,3}$/', // local (docker)
  29. '/^172\.3[0-1]\.\d{1,3}\.\d{1,3}$/' // local (docker)
  30. ];
  31. /**
  32. * Returns true if the client Ip is allowed
  33. *
  34. * @param string $clientIp
  35. * @return bool
  36. */
  37. public static function isIpAllowed(string $clientIp): bool
  38. {
  39. foreach (self::ALLOWED_IPS as $ipRule) {
  40. if (preg_match($ipRule, $clientIp)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * Check that the client Ip is allowed, else throw a Runtime error
  48. *
  49. * @return bool
  50. */
  51. private function assertIpAllowed(): bool
  52. {
  53. $clientIp = $_SERVER['REMOTE_ADDR'];
  54. if (!self::isIpAllowed($clientIp)){
  55. $route = $_REQUEST['route'];
  56. $this->logger->error(sprintf(
  57. "OtAdmin API: an attempt was made to call the route " .
  58. $route . " from an non-allowed IP (" . $clientIp . ")"));
  59. throw new \RuntimeException("Not allowed");
  60. }
  61. return true;
  62. }
  63. /**
  64. * Retrieve the organization's id from the given request parameters
  65. *
  66. * @param ServerRequest $request
  67. * @return int
  68. */
  69. private function getOrganizationId(ServerRequest $request): int
  70. {
  71. $params = $request->getQueryParams();
  72. $organizationId = $params['organization-id'];
  73. if (!$organizationId) {
  74. throw new \RuntimeException("Missing parameter: 'organization-id'");
  75. }
  76. return (int)$organizationId;
  77. }
  78. /**
  79. * -- Target of the route 'site_infos' --
  80. *
  81. * Return the main informations about the organization's website
  82. *
  83. * @param ServerRequest $request
  84. * @return JsonResponse
  85. * @throws \Exception
  86. */
  87. public function getSiteInfosAction(ServerRequest $request): JsonResponse
  88. {
  89. $this->assertIpAllowed();
  90. $organizationId = $this->getOrganizationId($request);
  91. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  92. $infos = $controller->getSiteInfosAction($organizationId);
  93. return new JsonResponse($infos);
  94. }
  95. /**
  96. * -- Target of the route 'site_create' --
  97. * >> Requires a query param named 'organization-id' (int)
  98. *
  99. * Create the organization's website
  100. *
  101. * @param ServerRequest $request
  102. * @return JsonResponse
  103. * @throws \Exception
  104. */
  105. public function createSiteAction(ServerRequest $request): JsonResponse
  106. {
  107. $this->assertIpAllowed();
  108. $organizationId = $this->getOrganizationId($request);
  109. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  110. $rootUid = $controller->createSiteAction($organizationId);
  111. $this->logger->info(sprintf(
  112. "OtAdmin API: A new website has been created with root page uid=" . $rootUid .
  113. " for the organization " . $organizationId));
  114. return new JsonResponse(
  115. [
  116. 'organization_id' => $organizationId,
  117. 'msg' => "A new website has been created with root page uid=" . $rootUid,
  118. 'root_uid' => $rootUid
  119. ]
  120. );
  121. }
  122. /**
  123. * -- Target of the route 'site_update' --
  124. * >> Requires a query param named 'organization-id' (int)
  125. *
  126. * Update the settings of the organization's website
  127. *
  128. * @param ServerRequest $request
  129. * @return JsonResponse
  130. * @throws \Exception
  131. */
  132. public function updateSiteConstantsAction(ServerRequest $request): JsonResponse
  133. {
  134. $this->assertIpAllowed();
  135. $organizationId = $this->getOrganizationId($request);
  136. $deep = (isset($queryParams['deep']) && $queryParams['deep']);
  137. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  138. $rootUid = $controller->updateSiteAction($organizationId, $deep);
  139. $this->logger->info(sprintf(
  140. "OtAdmin API: The website with root uid " . $rootUid . " has been updated " .
  141. " (organization: " . $organizationId . ")"));
  142. return new JsonResponse(
  143. [
  144. 'organization_id' => $organizationId,
  145. 'msg' => "The website with root uid " . $rootUid . " has been updated",
  146. 'root_uid' => $rootUid
  147. ]
  148. );
  149. }
  150. /**
  151. * -- Target of the route 'site_delete' --
  152. * >> Requires a query param named 'organization-id' (int)
  153. *
  154. * Proceeds to a soft-deletion of the organization's website
  155. *
  156. * @param ServerRequest $request
  157. * @return JsonResponse
  158. * @throws \Exception
  159. */
  160. public function deleteSiteAction(ServerRequest $request): JsonResponse
  161. {
  162. $this->assertIpAllowed();
  163. $organizationId = $this->getOrganizationId($request);
  164. $params = $request->getQueryParams();
  165. $redirectTo = isset($params['redirect-to']) ? $params['redirect-to'] : null;
  166. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  167. $rootUid = $controller->deleteSiteAction($organizationId, false, $redirectTo);
  168. $this->logger->info(sprintf(
  169. "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .
  170. " (organization: " . $organizationId . ")"));
  171. return new JsonResponse(
  172. [
  173. 'organization_id' => $organizationId,
  174. 'msg' => "The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.",
  175. 'root_uid' => $rootUid
  176. ]
  177. );
  178. }
  179. /**
  180. * -- Target of the route 'site_undelete' --
  181. * >> Requires a query param named 'organization-id' (int)
  182. *
  183. * Restore a soft-deleted organization's website
  184. *
  185. * @param ServerRequest $request
  186. * @return JsonResponse
  187. * @throws \Exception
  188. */
  189. public function undeleteSiteAction(ServerRequest $request): JsonResponse
  190. {
  191. $this->assertIpAllowed();
  192. $organizationId = $this->getOrganizationId($request);
  193. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  194. $rootUid = $controller->undeleteSiteAction($organizationId);
  195. $this->logger->info(sprintf(
  196. "OtAdmin API: The website with root uid " . $rootUid . " has been restored " .
  197. " (organization: " . $organizationId . ")"));
  198. return new JsonResponse(
  199. [
  200. 'organization_id' => $organizationId,
  201. 'msg' => "The website with root uid " . $rootUid . " has been restored",
  202. 'root_uid' => $rootUid
  203. ]
  204. );
  205. }
  206. /**
  207. * -- Target of the route 'site_clearcache' --
  208. * >> Requires a query param named 'organization-id' (int)
  209. *
  210. * Clear the cache of the organization's website
  211. *
  212. * @param ServerRequest $request
  213. * @return JsonResponse
  214. * @throws \Exception
  215. */
  216. public function clearSiteCacheAction(ServerRequest $request): JsonResponse
  217. {
  218. $this->assertIpAllowed();
  219. $organizationId = $this->getOrganizationId($request);
  220. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  221. $rootUid = $controller->clearSiteCacheAction($organizationId);
  222. return new JsonResponse(
  223. [
  224. 'organization_id' => $organizationId,
  225. 'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
  226. 'root_uid' => $rootUid
  227. ]
  228. );
  229. }
  230. /**
  231. * -- Target of the route 'site_setdomain' --
  232. * >> Requires a query param named 'organization-id' (int)
  233. *
  234. * Set a new domain for the organization website
  235. *
  236. * @param ServerRequest $request
  237. * @return JsonResponse
  238. * @throws \Exception
  239. */
  240. public function setSiteCustomDomainAction(ServerRequest $request): JsonResponse
  241. {
  242. $this->assertIpAllowed();
  243. $organizationId = $this->getOrganizationId($request);
  244. $queryParams = $request->getQueryParams();
  245. $domain = $queryParams['domain'];
  246. if (!$domain) {
  247. throw new \RuntimeException("Missing 'domain' parameter");
  248. }
  249. $redirect = (isset($queryParams['redirect']) && $queryParams['redirect']);
  250. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  251. $rootUid = $controller->setSiteCustomDomainAction($organizationId, $domain, $redirect);
  252. return new JsonResponse(
  253. [
  254. 'organization_id' => $organizationId,
  255. 'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
  256. 'root_uid' => $rootUid
  257. ]
  258. );
  259. }
  260. /**
  261. * -- Target of the route 'site_resetperms' --
  262. * >> Requires a query param named 'organization-id' (int)
  263. *
  264. * Reset the permissions of the website be users (admin, editors...)
  265. *
  266. * @param ServerRequest $request
  267. * @return JsonResponse
  268. * @throws \Exception
  269. */
  270. public function resetBeUserPermsAction(ServerRequest $request): JsonResponse
  271. {
  272. $this->assertIpAllowed();
  273. $organizationId = $this->getOrganizationId($request);
  274. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  275. $rootUid = $controller->resetBeUserPermsAction($organizationId);
  276. return new JsonResponse(
  277. [
  278. 'organization_id' => $organizationId,
  279. 'msg' => "The website with root uid " . $rootUid . " had its be users permissions reset",
  280. 'root_uid' => $rootUid
  281. ]
  282. );
  283. }
  284. /**
  285. * -- Target of the route 'site_status' --
  286. * >> Requires a query param named 'organization-id' (int)
  287. *
  288. * Returns the current status of the website
  289. *
  290. * @param ServerRequest $request
  291. * @return JsonResponse
  292. * @throws \Exception
  293. */
  294. public function getSiteStatusAction(ServerRequest $request): JsonResponse
  295. {
  296. $this->assertIpAllowed();
  297. $organizationId = $this->getOrganizationId($request);
  298. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  299. $queryParams = $request->getQueryParams();
  300. $full = (isset($queryParams['full']) && $queryParams['full']);
  301. $status = $controller->getSiteStatusAction($organizationId, $full);
  302. return new JsonResponse($status->toArray());
  303. }
  304. /**
  305. * -- Target of the route 'scan' --
  306. *
  307. * Scan the whole Typo3 database and return the results
  308. *
  309. * @param ServerRequest $request
  310. * @return JsonResponse
  311. * @throws \Exception
  312. */
  313. public function scanAllAction(ServerRequest $request): JsonResponse
  314. {
  315. $this->assertIpAllowed();
  316. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(ScanController::class);
  317. $queryParams = $request->getQueryParams();
  318. $full = (isset($queryParams['full']) && $queryParams['full']);
  319. $results = $controller->scanAllAction($full);
  320. return new JsonResponse($results);
  321. }
  322. }