瀏覽代碼

Merge branch 'feature/V8-6649-permettre-la-suppression-dfiniti' into develop

Olivier Massot 1 年之前
父節點
當前提交
f51ba47dbe
共有 2 個文件被更改,包括 64 次插入3 次删除
  1. 2 1
      composer.json
  2. 62 2
      ot_admin/Classes/Http/ApiController.php

+ 2 - 1
composer.json

@@ -40,7 +40,8 @@
         "opentalent/ot_optimizer": "^1.0",
         "opentalent/ot_connect": "^1.0",
         "typo3/cms-linkvalidator": "^12.4",
-        "typo3/cms-t3editor": "^12.4"
+        "typo3/cms-t3editor": "^12.4",
+        "phpstan/phpdoc-parser": "^1.0"
     },
     "repositories": [
         {

+ 62 - 2
ot_admin/Classes/Http/ApiController.php

@@ -12,6 +12,7 @@ use Opentalent\OtCore\Exception\NoSuchRecordException;
 use Opentalent\OtCore\Exception\NoSuchWebsiteException;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerAwareTrait;
+use Psr\Log\LoggerInterface;
 use TYPO3\CMS\Core\Http\JsonResponse;
 use TYPO3\CMS\Core\Http\ServerRequest;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -25,6 +26,11 @@ class ApiController implements LoggerAwareInterface
 {
     use LoggerAwareTrait;
 
+    const PROD_FRONT_IP = "172.16.0.68";
+    const PROD_V2_IP = "172.16.0.35";
+    const PUBLIC_PRODFRONT_IP = "141.94.117.38";
+    const PUBLIC_PROD_V2_IP = "141.94.117.35";
+
     const array ALLOWED_IPS = [
         '/^127\.0\.0\.[0-1]$/', // Localhost
         '/^localhost$/',  // Localhost
@@ -74,6 +80,46 @@ class ApiController implements LoggerAwareInterface
         return true;
     }
 
+    /**
+     * Lève une erreur si l'environnement est la prod et que la requête provient d'un autre environnement, car
+     * cette requête a probablement été envoyée à la prod par erreur.
+     *
+     * Permet de sécuriser certaines opérations destructives, comme la suppression d'organisation.
+     *
+     * @return void
+     */
+    private function preventIfIsDubious(): void
+    {
+        if (
+            $_SERVER &&
+            (
+                ($_SERVER['SERVER_ADDR'] === self::PROD_FRONT_IP && $_SERVER['REMOTE_ADDR'] !== self::PROD_V2_IP) ||
+                ($_SERVER['SERVER_ADDR'] === self::PUBLIC_PRODFRONT_IP && $_SERVER['REMOTE_ADDR'] !== self::PUBLIC_PROD_V2_IP)
+            )
+        ) {
+            throw new \RuntimeException("Invalid client ip");
+        }
+    }
+
+    /**
+     * Lève une erreur si le token de confirmation n'a pas était ajouté, ou si sa valeur est invalide.
+     *
+     * Permet de sécuriser certaines opérations destructives, comme la suppression d'organisation.
+     *
+     * @param int $organizationId
+     * @return void
+     */
+    private function preventOnMissingConfirmationToken(int $organizationId): void
+    {
+        $headers = getallheaders();
+        if (
+            !isset($headers['Confirmation-Token']) ||
+            $headers['Confirmation-Token'] !== 'DEL-'.$organizationId.'-'.date('Ymd')
+        ) {
+            throw new \RuntimeException("Missing or invalid confirmation token");
+        }
+    }
+
     /**
      * Retrieve the organization's id from the given request parameters
      *
@@ -218,6 +264,10 @@ class ApiController implements LoggerAwareInterface
      *
      * Proceeds to a soft-deletion of the organization's website
      *
+     * In the case of a hard deletion, a special header is requested as a confirmation token. The header
+     * shall be named 'Confirmation-Token' and its value shall be DEL-XXXX-YYYYMMDD, where XXXX is the id of
+     * the organization owning the website, and YYYYMMDD is the date of the current day.
+     *
      * @param ServerRequest $request
      * @return JsonResponse
      * @throws \Exception
@@ -229,17 +279,27 @@ class ApiController implements LoggerAwareInterface
         $organizationId = $this->getOrganizationId($request);
 
         $params = $request->getQueryParams();
+        $hard = (isset($params['hard']) && $params['hard']);
 
-        $rootUid = $this->siteController->deleteSiteAction($organizationId);
+        if ($hard) {
+            $this->preventIfIsDubious();
+            $this->preventOnMissingConfirmationToken($organizationId);
+        }
+
+        $rootUid = $this->siteController->deleteSiteAction($organizationId, $hard, true, true);
 
         $this->logger->info(sprintf(
             "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .
             " (organization: " . $organizationId . ")"));
 
+        $msg = $hard ?
+            "The website with root uid " . $rootUid . " has been hard-deleted." :
+            "The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.";
+
         return new JsonResponse(
             [
                 'organization_id' => $organizationId,
-                'msg' => "The website with root uid " . $rootUid . " has been soft-deleted. Use the /site/undelete route to restore it.",
+                'msg' => $msg,
                 'root_uid' => $rootUid
             ]
         );