ApiController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 'redirect_add' --
  152. * >> Requires query params named 'from-domain' (string) and 'to-domain' (string)
  153. *
  154. * Add or update a redirection from 'from-domain' to 'to-domain'
  155. *
  156. * @param ServerRequest $request
  157. * @return JsonResponse
  158. * @throws \Exception
  159. */
  160. public function addRedirectionAction(ServerRequest $request): JsonResponse
  161. {
  162. $this->assertIpAllowed();
  163. $fromDomain = (isset($queryParams['from-domain']) && $queryParams['from-domain']);
  164. $toDomain = (isset($queryParams['to-domain']) && $queryParams['to-domain']);
  165. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  166. $res = $controller->addRedirection($fromDomain, $toDomain);
  167. if ($res == SiteController::REDIRECTION_UPDATED) {
  168. $msg = "An existing redirection has been updated ";
  169. } elseif ($res == SiteController::REDIRECTION_CREATED) {
  170. $msg = "A redirection has been added ";
  171. }
  172. $this->logger->info(sprintf(
  173. "OtAdmin API: " . $msg . " from " . $fromDomain . " to " . $toDomain
  174. ));
  175. return new JsonResponse(
  176. [
  177. 'msg' => $msg . " from " . $fromDomain . " to " . $toDomain,
  178. ]
  179. );
  180. }
  181. /**
  182. * -- Target of the route 'site_delete' --
  183. * >> Requires a query param named 'organization-id' (int)
  184. *
  185. * Proceeds to a soft-deletion of the organization's website
  186. *
  187. * @param ServerRequest $request
  188. * @return JsonResponse
  189. * @throws \Exception
  190. */
  191. public function deleteSiteAction(ServerRequest $request): JsonResponse
  192. {
  193. $this->assertIpAllowed();
  194. $organizationId = $this->getOrganizationId($request);
  195. $params = $request->getQueryParams();
  196. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  197. $rootUid = $controller->deleteSiteAction($organizationId);
  198. $this->logger->info(sprintf(
  199. "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .
  200. " (organization: " . $organizationId . ")"));
  201. return new JsonResponse(
  202. [
  203. 'organization_id' => $organizationId,
  204. 'msg' => "The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.",
  205. 'root_uid' => $rootUid
  206. ]
  207. );
  208. }
  209. /**
  210. * -- Target of the route 'site_undelete' --
  211. * >> Requires a query param named 'organization-id' (int)
  212. *
  213. * Restore a soft-deleted organization's website
  214. *
  215. * @param ServerRequest $request
  216. * @return JsonResponse
  217. * @throws \Exception
  218. */
  219. public function undeleteSiteAction(ServerRequest $request): JsonResponse
  220. {
  221. $this->assertIpAllowed();
  222. $organizationId = $this->getOrganizationId($request);
  223. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  224. $rootUid = $controller->undeleteSiteAction($organizationId);
  225. $this->logger->info(sprintf(
  226. "OtAdmin API: The website with root uid " . $rootUid . " has been restored " .
  227. " (organization: " . $organizationId . ")"));
  228. return new JsonResponse(
  229. [
  230. 'organization_id' => $organizationId,
  231. 'msg' => "The website with root uid " . $rootUid . " has been restored",
  232. 'root_uid' => $rootUid
  233. ]
  234. );
  235. }
  236. /**
  237. * -- Target of the route 'site_clearcache' --
  238. * >> Requires a query param named 'organization-id' (int)
  239. *
  240. * Clear the cache of the organization's website
  241. *
  242. * @param ServerRequest $request
  243. * @return JsonResponse
  244. * @throws \Exception
  245. */
  246. public function clearSiteCacheAction(ServerRequest $request): JsonResponse
  247. {
  248. $this->assertIpAllowed();
  249. $organizationId = $this->getOrganizationId($request);
  250. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  251. $rootUid = $controller->clearSiteCacheAction($organizationId);
  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_setdomain' --
  262. * >> Requires a query param named 'organization-id' (int)
  263. * and a parameter named 'domain' (string)
  264. *
  265. * Set a new domain for the organization website
  266. *
  267. * @param ServerRequest $request
  268. * @return JsonResponse
  269. * @throws \Exception
  270. */
  271. public function setSiteCustomDomainAction(ServerRequest $request): JsonResponse
  272. {
  273. $this->assertIpAllowed();
  274. $organizationId = $this->getOrganizationId($request);
  275. $queryParams = $request->getQueryParams();
  276. $domain = $queryParams['domain'];
  277. if (!$domain) {
  278. throw new \RuntimeException("Missing 'domain' parameter");
  279. }
  280. $redirect = (isset($queryParams['redirect']) && $queryParams['redirect']);
  281. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  282. $rootUid = $controller->setSiteCustomDomainAction($organizationId, $domain, $redirect);
  283. return new JsonResponse(
  284. [
  285. 'organization_id' => $organizationId,
  286. 'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
  287. 'root_uid' => $rootUid
  288. ]
  289. );
  290. }
  291. /**
  292. * -- Target of the route 'site_resetperms' --
  293. * >> Requires a query param named 'organization-id' (int)
  294. *
  295. * Reset the permissions of the website be users (admin, editors...)
  296. *
  297. * @param ServerRequest $request
  298. * @return JsonResponse
  299. * @throws \Exception
  300. */
  301. public function resetBeUserPermsAction(ServerRequest $request): JsonResponse
  302. {
  303. $this->assertIpAllowed();
  304. $organizationId = $this->getOrganizationId($request);
  305. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  306. $rootUid = $controller->resetBeUserPermsAction($organizationId);
  307. return new JsonResponse(
  308. [
  309. 'organization_id' => $organizationId,
  310. 'msg' => "The website with root uid " . $rootUid . " had its be users permissions reset",
  311. 'root_uid' => $rootUid
  312. ]
  313. );
  314. }
  315. /**
  316. * -- Target of the route 'site_status' --
  317. * >> Requires a query param named 'organization-id' (int)
  318. *
  319. * Returns the current status of the website
  320. *
  321. * @param ServerRequest $request
  322. * @return JsonResponse
  323. * @throws \Exception
  324. */
  325. public function getSiteStatusAction(ServerRequest $request): JsonResponse
  326. {
  327. $this->assertIpAllowed();
  328. $organizationId = $this->getOrganizationId($request);
  329. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  330. $queryParams = $request->getQueryParams();
  331. $full = (isset($queryParams['full']) && $queryParams['full']);
  332. $status = $controller->getSiteStatusAction($organizationId, $full);
  333. return new JsonResponse($status->toArray());
  334. }
  335. /**
  336. * -- Target of the route 'scan' --
  337. *
  338. * Scan the whole Typo3 database and return the results
  339. *
  340. * @param ServerRequest $request
  341. * @return JsonResponse
  342. * @throws \Exception
  343. */
  344. public function scanAllAction(ServerRequest $request): JsonResponse
  345. {
  346. $this->assertIpAllowed();
  347. $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(ScanController::class);
  348. $queryParams = $request->getQueryParams();
  349. $full = (isset($queryParams['full']) && $queryParams['full']);
  350. $results = $controller->scanAllAction($full);
  351. return new JsonResponse($results);
  352. }
  353. }