Quellcode durchsuchen

https://assistance.opentalent.fr/browse/V8-1927

Olivier Massot vor 4 Jahren
Ursprung
Commit
1c60cff1d8

+ 8 - 1
ot_admin/Classes/Command/DeleteSiteCommand.php

@@ -46,6 +46,12 @@ class DeleteSiteCommand extends Command
                 InputOption::VALUE_NONE,
                 "Permanently delete the records and files. Use with caution."
             )
+            ->addOption(
+                'redirect-to',
+                'r',
+                InputOption::VALUE_REQUIRED,
+                "Organization id of the website to which add a redirection."
+            )
             ->addArgument(
                 'organization_id',
                 InputArgument::REQUIRED,
@@ -66,11 +72,12 @@ class DeleteSiteCommand extends Command
     {
         $org_id = $input->getArgument('organization_id');
         $hard = $input->getOption('hard');
+        $redirectTo = $input->getOption('redirect-to');
 
         $io = new SymfonyStyle($input, $output);
 
         $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
-        $rootUid = $siteController->deleteSiteAction($org_id, $hard);
+        $rootUid = $siteController->deleteSiteAction($org_id, $hard, $redirectTo);
 
         if ($hard) {
             $io->success(sprintf("The website with root uid " . $rootUid . " has been permanently deleted"));

+ 3 - 3
ot_admin/Classes/Command/SetSiteDomainCommand.php

@@ -45,10 +45,10 @@ class SetSiteDomainCommand extends Command
                 "The new domain to set up"
             )
             ->addOption(
-                'redirect',
+                'no-redirection',
                 'r',
                 InputOption::VALUE_NONE,
-                'Use this option to add a redirection from the previous domain to the new one'
+                'Use this option to prevent the creation of a redirection from the previous domain to the new one'
             );
     }
 
@@ -65,7 +65,7 @@ class SetSiteDomainCommand extends Command
     {
         $org_id = $input->getArgument('organization_id');
         $domain = $input->getArgument('domain');
-        $redirect = $input->getOption('redirect');
+        $redirect = ($input->getOption('no-redirection') == null);
 
         $io = new SymfonyStyle($input, $output);
 

+ 38 - 6
ot_admin/Classes/Controller/SiteController.php

@@ -629,12 +629,17 @@ class SiteController extends ActionController
      * with no possibility of undoing anything. In this case, you'll have to confirm your intention
      * by creating a file in the Typo3 root directory, named 'DEL####' (#### is the organization id)
      *
+     * $redirectTo is the optional organization id to whom requests will be redirected
+     *
      * @param int $organizationId
      * @param bool $hard
+     * @param int|null $redirectTo
      * @return int
-     * @throws \Exception
+     * @throws NoSuchWebsiteException
+     * @throws \Doctrine\DBAL\ConnectionException
+     * @throws \Doctrine\DBAL\DBALException
      */
-    public function deleteSiteAction(int $organizationId, bool $hard=false) {
+    public function deleteSiteAction(int $organizationId, bool $hard=false, ?int $redirectTo=null) {
         $rootUid = $this->findRootUidFor($organizationId);
 
         $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
@@ -654,6 +659,12 @@ class SiteController extends ActionController
             );
         }
 
+        // Prepare the redirection
+        if ($redirectTo) {
+            $originUrl = $this->getSiteInfosAction($organizationId)['baseUrl'];
+            $targetUrl = $this->getSiteInfosAction($redirectTo)['baseUrl'];
+        }
+
         // start transactions
         $this->connectionPool->getConnectionByName('Default')->beginTransaction();
 
@@ -781,7 +792,12 @@ class SiteController extends ActionController
                 }
             }
 
-            // Try to commit the result (before any eventual file deletion or renaming)
+            // Add the redirection
+            if ($redirectTo) {
+                $this->addRedirection($originUrl, $targetUrl);
+            }
+
+            // Try to commit the result
             $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
             if (!$commitSuccess) {
                 throw new \RuntimeException('Something went wrong while commiting the result');
@@ -933,6 +949,10 @@ class SiteController extends ActionController
                 $renamed[$initialPath] = $newPath;
             }
 
+            // remove eventual redirection from this site to another
+            $originUrl = $this->getSiteInfosAction($organizationId)['baseUrl'];
+            $this->removeRedirectionsFrom($originUrl);
+
             // Try to commit the result
             $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
             if (!$commitSuccess) {
@@ -1206,9 +1226,17 @@ class SiteController extends ActionController
             throw new \InvalidArgumentException("The given domain does not seems to be a valid domain: " . $newDomain);
         }
 
-        $rootUid = $this->findRootUidFor($organizationId);
+        $infos = $this->getSiteInfosAction($organizationId);
+        $originUrl = $infos['baseUrl'];
+        $rootUid = $infos['rootUid'];
+
         $this->writeConfigFile($organizationId, $rootUid, $newDomain);
 
+        if ($redirect) {
+            // Add the redirection
+            $this->addRedirection($originUrl, $newDomain);
+        }
+
         return $rootUid;
     }
 
@@ -1260,7 +1288,6 @@ class SiteController extends ActionController
 
     /**
      * Add a new redirection from $fromDomain to $toDomain.
-     * Domains should not contain the http(s):// part
      * If this redirection already exists but has been deleted and/or disabled, it will be restored and enabled
      * If a redirection already exists but is not deleted and targets another domain, a RuntimeException will be thrown.
      *
@@ -1352,6 +1379,11 @@ class SiteController extends ActionController
      */
     public function removeRedirectionsFrom($fromDomain, $hard=false): int
     {
+        $fromDomain = preg_replace('/https?:\/\//', '', $fromDomain);
+        if (!preg_match(self::RX_DOMAIN, $fromDomain)) {
+            throw new \InvalidArgumentException("The does not seems to be a valid domain: " . $fromDomain);
+        }
+
         $existing = $this->getRedirectionsFrom($fromDomain);
         $deleted = 0;
         foreach ($existing as $redirection) {
@@ -1398,7 +1430,7 @@ class SiteController extends ActionController
         if ($rootUid > 0) {
             return $rootUid;
         }
-        throw new NoSuchWebsiteException("The website of this organization can not be found");
+        throw new NoSuchWebsiteException("No website found for organization " . $organizationId);
     }
 
     /**

+ 4 - 1
ot_admin/Classes/Http/ApiController.php

@@ -172,8 +172,11 @@ class ApiController implements LoggerAwareInterface
 
         $organizationId = $this->getOrganizationId($request);
 
+        $params = $request->getQueryParams();
+        $redirectTo = isset($params['redirect-to']) ? $params['redirect-to'] : null;
+
         $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
-        $rootUid = $controller->deleteSiteAction($organizationId, false);
+        $rootUid = $controller->deleteSiteAction($organizationId, false, $redirectTo);
 
         $this->logger->info(sprintf(
             "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .