소스 검색

add multi-subdomain management, various fixes

Olivier Massot 3 년 전
부모
커밋
f63b3140c1

+ 3 - 2
config/opentalent/products.yaml

@@ -38,6 +38,7 @@ opentalent:
           - TypeOfPractice
           - Activity
           - ActivityType
+          - Subdomain
         roles:
           - ROLE_GENERAL_CONFIG
 
@@ -306,9 +307,9 @@ opentalent:
 
       manager:
         extend: artist-premium
-        modules: ~            
+        modules: ~
 
       manager_premium:
         extend: manager
         modules:
-          - CorePremium
+          - CorePremium

+ 23 - 0
src/Commands/BindfileRefreshCommand.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Commands;
+
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+#[AsCommand(
+    name: 'opentalent:bindfile-refresh',
+    description: 'Push the latest data from the Opentalent DB to dolibarr'
+)]
+class BindfileRefreshCommand
+{
+    protected function configure()
+    {
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+
+    }
+}

+ 7 - 1
src/Commands/PostUpgrade/V0_2/PostUpgradeCommand.php

@@ -75,6 +75,12 @@ class PostUpgradeCommand extends Command
                     where d.domainName like '%.opentalent.fr';";
             $cnn->query($sql);
 
+            $sql = "delete 
+                    from opentalent.Subdomain
+                    where subdomain REGEXP '^(.*)\\\\.(.*)$' 
+                    and REGEXP_REPLACE(subdomain, '\\\\.', '-') in (select subdomain from opentalent.Subdomain);";
+            $cnn->query($sql);
+
             $sql = "update opentalent.Subdomain
                     set subdomain = REGEXP_REPLACE(subdomain, '\\\\.', '-')
                     where subdomain REGEXP '^(.*)\\\\.(.*)$';";
@@ -82,7 +88,7 @@ class PostUpgradeCommand extends Command
 
             $this->logger->info('Complete with subdomains from Parameters table');
             $sql = "insert into opentalent.Subdomain (parameters_id, subdomain)
-                    select p.id, p.subDomain
+                    select distinct p.id, p.subDomain
                     from opentalent.Parameters p
                     left join opentalent.Subdomain s 
                     on s.parameters_id = p.id

+ 18 - 6
src/Entity/Organization/Parameters.php

@@ -297,23 +297,38 @@ class Parameters
         return $this;
     }
 
+    /**
+     * @deprecated
+     * @return string|null
+     */
     public function getSubDomain(): ?string
     {
         return $this->subDomain;
     }
 
+    /**
+     * @deprecated
+     * @param string|null $subDomain
+     */
     public function setSubDomain(?string $subDomain): self
     {
-        $this->subDomain = $subDomain;
-
-        return $this;
+        throw new \RuntimeException('setSubDomain is deprecated, use addSubdomain with setActive');
     }
 
+    /**
+     * @deprecated
+     * @return string|null
+     */
     public function getWebsite(): ?string
     {
         return $this->website;
     }
 
+    /**
+     * @deprecated
+     * @param string|null $website
+     * @return $this
+     */
     public function setWebsite(?string $website): self
     {
         $this->website = $website;
@@ -347,9 +362,6 @@ class Parameters
     public function setCustomDomain(?string $customDomain): void
     {
         $this->customDomain = $customDomain;
-
-        // En attendant la fin de la migration vers le nouveau système de gestion des sous-domaines
-        $this->otherWebsite = $customDomain;
     }
 
     public function getDesactivateOpentalentSiteWeb(): bool

+ 13 - 4
src/Entity/Organization/Subdomain.php

@@ -10,12 +10,15 @@ use ApiPlatform\Core\Annotation\ApiResource;
  * Sous-domaine enregistré par une organisation
  */
 #[ApiResource(
+    collectionOperations: [
+        'get'
+    ],
     itemOperations: [
         'get' => [
-            'security' => '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION")) and object.getOrganization().getId() == user.getOrganization().getId()'
+            'security' => '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION")) and object.getParameters().getOrganization().getId() == user.getOrganization().getId()'
         ],
         'put' => [
-            'security' => 'is_granted("ROLE_ORGANIZATION") and object.getOrganization().getId() == user.getOrganization().getId()'
+            'security' => 'is_granted("ROLE_ORGANIZATION") and object.getParameters().getOrganization().getId() == user.getOrganization().getId()'
         ]
     ]
 )]
@@ -99,7 +102,13 @@ class Subdomain
     {
         $this->active = $active;
 
-        // En attendant la fin de la migration vers le nouveau système de gestion des sous-domaines
-        $this->getParameters()->setSubDomain($this->subdomain);
+        // Ensure it is the only active subdomain
+        if ($active) {
+            foreach ($this->getParameters()->getSubdomains() as $subdomain) {
+                if ($subdomain !== $this) {
+                    $subdomain->setActive(false);
+                }
+            }
+        }
     }
 }

+ 2 - 2
src/Security/Voter/ModuleVoter.php

@@ -21,7 +21,7 @@ class ModuleVoter extends Voter
 
     public function __construct(private Module $module, private ResourceMetadataFactoryInterface $resourceMetadataFactory)
     { }
-    
+
     protected function supports(string $attribute, $subject): bool
     {
         if (!in_array($attribute, [self::HAVING_MODULE])) {
@@ -47,7 +47,7 @@ class ModuleVoter extends Voter
 
         //Check if there is a module for this entity : eq configuration problem
         if (null === $module) {
-            throw new AccessDeniedHttpException(sprintf('There no module for the entity (%s) !', $resourceMetadata->getShortName()));
+            throw new AccessDeniedHttpException(sprintf('There are no module for the entity (%s) !', $resourceMetadata->getShortName()));
         }
 
         /** @var Access $currentAccess */

+ 3 - 3
src/Service/Organization/OrganizationProfileCreator.php

@@ -63,8 +63,8 @@ class OrganizationProfileCreator
         $organizationProfile
             ->setId($organization->getId())
             ->setName($organization->getName())
-            ->setSubDomain($organization->getParameters()->getSubDomain())
-            ->setWebsite($organization->getParameters()->getWebsite());
+            ->setWebsite(Utils::getOrganizationWebsite($organization));
+
         return $organizationProfile;
     }
-}
+}

+ 47 - 1
src/Service/Organization/Utils.php

@@ -6,6 +6,7 @@ namespace App\Service\Organization;
 use App\Entity\Organization\Organization;
 use App\Enum\Organization\OrganizationIdsEnum;
 use App\Enum\Organization\SettingsProductEnum;
+use App\Service\Utils\UrlBuilder;
 use App\Test\Service\Organization\UtilsTest;
 
 /**
@@ -143,4 +144,49 @@ class Utils
             return  (int) ($year - 1);
         else return (int) $year;
     }
-}
+
+    /**
+     * Return the active subdomain of an organization as a string, or null
+     *
+     * @param Organization $organization
+     * @return string | null
+     */
+    public static function getOrganizationActiveSubdomain(Organization $organization): ?string {
+        foreach ($organization->getParameters()->getSubdomains() as $subdomain) {
+            if ($subdomain->isActive()) {
+                return $subdomain->getSubdomain();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Get the URL of the current website of the organization
+     *
+     * @see https://ressources.opentalent.fr/display/SPEC/Preferences#Preferences-Siteinternet
+     *
+     * @param Organization $organization
+     * @return string | null
+     */
+    public static function getOrganizationWebsite(Organization $organization): ?string {
+        $parameters = $organization->getParameters();
+
+        if ($parameters->getDesactivateOpentalentSiteWeb()) {
+            if ($parameters->getOtherWebsite()) {
+                return UrlBuilder::prependHttps($parameters->getOtherWebsite());
+            }
+            return null;
+        }
+
+        if (!empty($parameters->getCustomDomain())) {
+            return UrlBuilder::prependHttps($parameters->getCustomDomain());
+        }
+
+        $subdomain = self::getOrganizationActiveSubdomain($organization);
+        if (!$subdomain) {
+            return null;
+        }
+
+        return 'https://' . $subdomain . '.opentalent.fr';
+    }
+}

+ 3 - 3
src/Service/Utils/UrlBuilder.php

@@ -47,12 +47,12 @@ class UrlBuilder
     }
 
     /**
-     * Prepend the 'https://' part if neither 'http://' of 'https://' is present, does nothing else
+     * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
      *
      * @param $url
      * @return string
      */
-    public static function preprendHttps($url): string
+    public static function prependHttps($url): string
     {
         if (!preg_match('/^https?:\/\/.*/', $url)) {
             $url = 'https://' . $url;
@@ -72,7 +72,7 @@ class UrlBuilder
     public static function concat(string $url, string $path, array $parameters, bool $preprendHttps = false): string {
         $url = self::concatParameters(self::concatPath($url, $path), $parameters);
         if ($preprendHttps) {
-            $url = self::preprendHttps($url);
+            $url = self::prependHttps($url);
         }
         return $url;
     }

+ 3 - 3
tests/Service/Utils/UrlBuilderTest.php

@@ -40,15 +40,15 @@ class UrlBuilderTest extends TestCase
     public function testPrependHttps() {
         $this->assertEquals(
             'https://domain.org/abc',
-            UrlBuilder::preprendHttps('https://domain.org/abc')
+            UrlBuilder::prependHttps('https://domain.org/abc')
         );
         $this->assertEquals(
             'http://domain.org/abc',
-            UrlBuilder::preprendHttps('http://domain.org/abc')
+            UrlBuilder::prependHttps('http://domain.org/abc')
         );
         $this->assertEquals(
             'https://domain.org/abc',
-            UrlBuilder::preprendHttps('domain.org/abc')
+            UrlBuilder::prependHttps('domain.org/abc')
         );
     }