Browse Source

Merge branch 'hotfix/V8-7844-site-internet-ne-se-cr-pas-pour-'

Olivier Massot 2 months ago
parent
commit
672c2a7e45

+ 77 - 0
ot_admin/Classes/Command/JustMakeItWorkSiteCommand.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace Opentalent\OtAdmin\Command;
+
+
+use Opentalent\OtAdmin\Controller\SiteController;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+/**
+ * This CLI command updates the organization's website if it exists, restores
+ * it if it has been deleted, or creates it if it does not exist.
+ * It also removes any redirections that may have been set on the domain.
+ *
+ * @package Opentalent\OtAdmin\Command
+ */
+class JustMakeItWorkSiteCommand extends Command
+{
+    public function __construct(
+        private readonly SiteController $siteController
+    ) {
+        parent::__construct();
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * Allows to configure the command.
+     * Allows to add a description, a help text, and / or define arguments.
+     *
+     */
+    protected function configure(): void
+    {
+        $this
+            ->setName("ot:site:just-make-it-work")
+            ->setDescription("Forces the website to be updated / restored / created, according to its state")
+            ->setHelp("Call this method by giving it the organization's id in the Opentalent DB. 
+                            If no site exists, it will create it; 
+                            If the site has been deleted, it will restore it; 
+                            If a site already exists, it will update it.
+                            This command will also remove any redirections that may have been set on the domain.")
+            ->addArgument(
+                'organization-id',
+                InputArgument::REQUIRED,
+                "The organization's id in the opentalent DB"
+            )->addOption(
+                'dev',
+                null,
+                InputOption::VALUE_NONE,
+                "Add this option to get url like 'http://host/subdomain' instead of 'http://subdomain.host'"
+            );
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @return int
+     * @throws \Exception
+     */
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $org_id = $input->getArgument('organization-id');
+
+        $io = new SymfonyStyle($input, $output);
+
+        $rootUid = $this->siteController->justeMakeItWorkSiteAction($org_id);
+
+        $io->success(sprintf("The website has been updated / restored / created (root page uid=" . $rootUid . ')'));
+        return 0;
+    }
+}

+ 33 - 3
ot_admin/Classes/Controller/SiteController.php

@@ -774,7 +774,37 @@ class SiteController extends ActionController
     }
 
     /**
-     * Delete the website with all its pages, contents and related records
+     * Update the organization's website if it exists, restore
+     * it if it has been deleted, or create it if it does not exist.
+     *
+     * It also removes any redirections that may have been set on the domain.
+     */
+    public function justeMakeItWorkSiteAction(int $organizationId): int
+    {
+        try {
+            $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId, false);
+        } catch (NoSuchWebsiteException $e) {
+            $website = null;
+        }
+
+        if ($website) {
+            if ($website['deleted'] === 1) {
+                $this->undeleteSiteAction($organizationId);
+            }
+            $rootUid = $this->updateSiteAction($organizationId);
+        } else {
+            $rootUid =  $this->createSiteAction($organizationId);
+        }
+
+        // Remove any existing redirection from this website production's domain
+        $originUrl = $this->otWebsiteRepository->resolveWebsiteDomain($website);
+        $this->removeRedirectionsFrom($originUrl);
+
+        return $rootUid;
+    }
+
+    /**
+     * Delete the website with all its pages, contents, and related records
      *
      * If the hard parameter is false, the records' `deleted` field will be set to true and
      * the files and directories will be renamed. This kind of 'soft' deletion can be undone.
@@ -2253,7 +2283,7 @@ class SiteController extends ActionController
     }
 
     /**
-     * Create the given directory, give its property to the www-data group and
+     * Create the given directory, give its property to the www-data group, and
      * record it as a newly created dir (for an eventual rollback)
      *
      * @param string $dirPath
@@ -2268,7 +2298,7 @@ class SiteController extends ActionController
     }
 
     /**
-     * Write the given file with content, give its property to the www-data group and
+     * Write the given file with content, give its property to the www-data group, and
      * record it as a newly created file (for an eventual rollback)
      *
      * @param string $path

+ 33 - 0
ot_admin/Classes/Http/ApiController.php

@@ -232,6 +232,39 @@ class ApiController implements LoggerAwareInterface
         );
     }
 
+    /**
+     * -- Target of the route 'site_make_it_work' --
+     * >> Requires a query param named 'organization-id' (int)
+     *
+     * Updates the organization's website if it exists, restores
+     * it if it has been deleted, or creates it if it does not exist.
+     * It also removes any redirections that may have been set on the domain.
+ *
+     * @param ServerRequest $request
+     * @return JsonResponse
+     * @throws \Exception
+     */
+    public function justMakeItWorkSiteAction(ServerRequest $request): JsonResponse
+    {
+        $this->assertIpAllowed();
+
+        $organizationId = $this->getOrganizationId($request);
+
+        $rootUid = $this->siteController->justeMakeItWorkSiteAction($organizationId);
+
+        $this->logger->info(sprintf(
+            "OtAdmin API: The website with root uid " . $rootUid . " has been updated / restored / created " .
+            " (organization: " . $organizationId . ")"));
+
+        return new JsonResponse(
+            [
+                'organization_id' => $organizationId,
+                'msg' => "The website with root uid " . $rootUid . " has been updated / restored / created",
+                'root_uid' => $rootUid
+            ]
+        );
+    }
+
     /**
      * -- Target of the route 'redirect_add' --
      * >> Requires query params named 'from-domain' (string) and 'to-domain' (string)

+ 5 - 0
ot_admin/Configuration/Backend/Routes.php

@@ -27,6 +27,11 @@ return [
         'target' => ApiController::class . '::updateSiteConstantsAction',
         'access' => 'public'
     ],
+    'site_just_make_it_work' => [
+        'path' => '/otadmin/site/just-make-it-work',
+        'target' => ApiController::class . '::justMakeItWorkSiteAction',
+        'access' => 'public'
+    ],
     'site_clearcache' => [
         'path' => '/otadmin/site/clear-cache',
         'target' => ApiController::class . '::clearSiteCacheAction',

+ 6 - 0
ot_admin/Configuration/Services.yaml

@@ -19,6 +19,12 @@ services:
         command: 'ot:site:create'
         schedulable: false
 
+  Opentalent\OtAdmin\Command\JustMakeItWorkSiteCommand:
+    tags:
+      - name: console.command
+        command: 'ot:site:just-make-it-work'
+        schedulable: false
+
   Opentalent\OtAdmin\Command\DeleteSiteCommand:
     tags:
       - name: console.command