Просмотр исходного кода

Merge branch 'V8-6817_gardes_fous_suppression_orgas' into feature/V8-3988-suppression-orgas

Olivier Massot 1 год назад
Родитель
Сommit
f2bcd21afb
1 измененных файлов с 25 добавлено и 1 удалено
  1. 25 1
      src/Service/Organization/OrganizationFactory.php

+ 25 - 1
src/Service/Organization/OrganizationFactory.php

@@ -592,12 +592,19 @@ class OrganizationFactory
     /**
      * /!\ Danger zone /!\
      *
+     * Supprime définitivement une organisation, ses données, ses fichiers, son site internet, et son profil Dolibarr.
+     *
+     * Pour éviter une suppression accidentelle, cette méthode ne doit pouvoir être exécutée que si la requête a été
+     * envoyée depuis le localhost.
+     *
      * @param OrganizationDeletionRequest $organizationDeletionRequest
-     * @return void
+     * @return OrganizationDeletionRequest
      * @throws Exception
      */
     public function delete(OrganizationDeletionRequest $organizationDeletionRequest): OrganizationDeletionRequest
     {
+        $this->preventIfNotLocalhost();
+
         $organization = $this->organizationRepository->find($organizationDeletionRequest->getOrganizationId());
 
         $this->logger->info(
@@ -677,6 +684,23 @@ class OrganizationFactory
         return $organizationDeletionRequest;
     }
 
+    /**
+     * Lève une exception si la méthode a été appelée dans le cadre d'un appel API originaire d'un hôte
+     * différent de localhost.
+     *
+     * @return void
+     */
+    protected function preventIfNotLocalhost(): void
+    {
+        if (
+            $_SERVER &&
+            $_SERVER['APP_ENV'] !== 'docker' &&
+            $_SERVER['SERVER_ADDR'] !== $_SERVER['REMOTE_ADDR']
+        ) {
+            throw new \RuntimeException("This operation is restricted to localhost");
+        }
+    }
+
     /**
      * Supprime tous les Access d'une organisation, ainsi que la Person
      * rattachée (si celle-ci n'est pas liée à d'autres Access)