浏览代码

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

Olivier Massot 4 年之前
父节点
当前提交
909ac02ccd

+ 5 - 5
ot_admin/Classes/Command/DeleteSiteCommand.php

@@ -47,10 +47,10 @@ class DeleteSiteCommand extends Command
                 "Permanently delete the records and files. Use with caution."
             )
             ->addOption(
-                'redirect-to',
-                'r',
+                'no-redirect',
+                'd',
                 InputOption::VALUE_REQUIRED,
-                "Organization id of the website to which add a redirection."
+                "If set, the website's url won't be redirected to its parent's website"
             )
             ->addOption(
                 'force',
@@ -77,12 +77,12 @@ class DeleteSiteCommand extends Command
         $org_id = $input->getArgument('organization-id');
         $hard = $input->getOption('hard');
         $force = $input->getOption('force');
-        $redirectTo = $input->getOption('redirect-to');
+        $noRedirect = $input->getOption('no-redirect');
 
         $io = new SymfonyStyle($input, $output);
 
         $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
-        $rootUid = $siteController->deleteSiteAction($org_id, $hard, $redirectTo, $force);
+        $rootUid = $siteController->deleteSiteAction($org_id, $hard, !$noRedirect, $force);
 
         if ($hard) {
             $io->success(sprintf("The website with root uid " . $rootUid . " has been permanently deleted"));

+ 12 - 11
ot_admin/Classes/Controller/SiteController.php

@@ -737,14 +737,14 @@ 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
+     * If $redirectToParent is set to true, the website's url will be redirected to its parent's website
      *
      * The $force parameter, if true, will both bypass the 'DEL###' file confirmation and recursively delete the
      * user_upload and form_definitions of the website. <!> USE WITH CAUTION
      *
      * @param int $organizationId
      * @param bool $hard
-     * @param int|null $redirectTo
+     * @param bool $redirectToParent  If set to true, the website's url will be redirected to its parent's website
      * @param bool $force
      * @return int
      * @throws NoSuchRecordException
@@ -752,7 +752,7 @@ class SiteController extends ActionController
      * @throws \Doctrine\DBAL\DBALException
      * @throws \Throwable
      */
-    public function deleteSiteAction(int $organizationId, bool $hard=false, ?int $redirectTo=null, bool $force = false): int
+    public function deleteSiteAction(int $organizationId, bool $hard=false, bool $redirectToParent=true, bool $force = false): int
     {
         $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
         $websiteUid = $website['uid'];
@@ -776,13 +776,13 @@ class SiteController extends ActionController
         }
 
         // Prepare the redirection
-        if ($redirectTo) {
-            if ($redirectTo == $organizationId) {
-                throw new \InvalidArgumentException('redirectTo value has to be different from the organizationId');
-            }
+        if ($redirectToParent) {
             $originDomain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
 
-            $targetOrganizationWebsite = $this->otWebsiteRepository->getWebsiteByOrganizationId($redirectTo);
+            $organization = $this->fetchOrganization($organizationId);
+
+            $targetOrganizationId = $organization->getParentId();
+            $targetOrganizationWebsite = $this->otWebsiteRepository->getWebsiteByOrganizationId($targetOrganizationId);
             $targetDomain = $this->otWebsiteRepository->resolveWebsiteDomain($targetOrganizationWebsite);
         }
 
@@ -930,7 +930,7 @@ class SiteController extends ActionController
             }
 
             // Add the redirection
-            if ($redirectTo) {
+            if ($redirectToParent) {
                 $this->addRedirection($originDomain, $targetDomain);
             }
 
@@ -1124,7 +1124,7 @@ class SiteController extends ActionController
             }
 
             // remove eventual redirection from this site to another
-            $originUrl = $this->getSiteInfosAction($organizationId)->getBaseUrl();
+            $originUrl = $this->otWebsiteRepository->resolveWebsiteDomain($website);
             $this->removeRedirectionsFrom($originUrl);
 
             // Try to commit the result
@@ -1993,7 +1993,8 @@ class SiteController extends ActionController
 
     private function fetchOrganizationExtraData(int $organizationId) {
 
-        $db_host = $_SERVER['HTTP_HOST'] != 'local.sub.opentalent.fr' ? 'prod-back' : 'db';
+        $db_host = ($_SERVER['HTTP_HOST'] == 'typo3' |
+                    $_SERVER['HTTP_HOST'] == 'local.sub.opentalent.fr') ? 'db' : 'prod-back';
 
         $cnn = new PDO(
             "mysql:host=" . $db_host . ";dbname=opentalent",

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

@@ -171,6 +171,42 @@ class ApiController implements LoggerAwareInterface
         );
     }
 
+    /**
+     * -- Target of the route 'redirect_add' --
+     * >> Requires query params named 'from-domain' (string) and 'to-domain' (string)
+     *
+     * Add or update a redirection from 'from-domain' to 'to-domain'
+     *
+     * @param ServerRequest $request
+     * @return JsonResponse
+     * @throws \Exception
+     */
+    public function addRedirectionAction(ServerRequest $request): JsonResponse
+    {
+        $this->assertIpAllowed();
+
+        $fromDomain = (isset($queryParams['from-domain']) && $queryParams['from-domain']);
+        $toDomain = (isset($queryParams['to-domain']) && $queryParams['to-domain']);
+
+        $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
+        $res = $controller->addRedirection($fromDomain, $toDomain);
+
+        if ($res == SiteController::REDIRECTION_UPDATED) {
+            $msg = "An existing redirection has been updated ";
+        } elseif ($res == SiteController::REDIRECTION_CREATED) {
+            $msg = "A redirection has been added ";
+        }
+
+        $this->logger->info(sprintf(
+            "OtAdmin API: " . $msg . " from " . $fromDomain . " to " . $toDomain
+        ));
+        return new JsonResponse(
+            [
+                'msg' => $msg . " from " . $fromDomain . " to " . $toDomain,
+            ]
+        );
+    }
+
     /**
      * -- Target of the route 'site_delete' --
      * >> Requires a query param named 'organization-id' (int)
@@ -188,10 +224,9 @@ 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, $redirectTo);
+        $rootUid = $controller->deleteSiteAction($organizationId);
 
         $this->logger->info(sprintf(
             "OtAdmin API: The website with root uid " . $rootUid . " has been soft-deleted " .

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

@@ -37,6 +37,11 @@ return [
         'target' => ApiController::class . '::setSiteCustomDomainAction',
         'access' => 'public'
     ],
+    'redirect_add' => [
+        'path' => '/otadmin/redirect/add',
+        'target' => ApiController::class . '::addRedirectionAction',
+        'access' => 'public'
+    ],
     'site_resetperms' => [
         'path' => '/otadmin/site/reset-perms',
         'target' => ApiController::class . '::resetBeUserPermsAction',

+ 2 - 2
ot_core/Classes/Domain/Model/Organization.php

@@ -477,7 +477,7 @@ class Organization extends AbstractEntity
     /**
      * Returns the parentId
      *
-     * @return string $parentId
+     * @return int|null $parentId
      */
     public function getParentId(): ?int
     {
@@ -485,7 +485,7 @@ class Organization extends AbstractEntity
     }
 
     /**
-     * Sets the parentName
+     * Sets the parentId
      *
      * @param int|null $parentId
      * @return void

+ 2 - 1
ot_core/Classes/Domain/Repository/BaseApiRepository.php

@@ -76,7 +76,8 @@ abstract class BaseApiRepository implements LoggerAwareInterface
      */
     protected function getApiUri(string $trailing_part = null): string
     {
-        $host = $_SERVER['HTTP_HOST'];
+        $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['VIRTUAL_HOST'];
+
         if (isset($this->variants_uris[$host])) {
             $uri = $this->variants_uris[$host];
         } else {

+ 1 - 0
ot_core/Classes/Domain/Repository/OrganizationRepository.php

@@ -93,6 +93,7 @@ class OrganizationRepository extends BaseApiRepository
         if ($record['@type'] != $this::HYDRA_TYPE) {
             return null;
         }
+
         $organization = new Organization();
         $a = explode('/', $record['@id']);
         $organization->setId(end($a));

+ 1 - 0
ot_core/Configuration/ot_config.yaml

@@ -3,3 +3,4 @@
 api_variant_uri:
   preprod.opentalent.fr: https://api.preprod.opentalent.fr/api/
   local.sub.opentalent.fr: http://nginx/api/
+  typo3: http://nginx/api/