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

add post-persist methods and constraints ; fix upgrade script

Olivier Massot 3 лет назад
Родитель
Сommit
d0e36c5702

+ 11 - 0
src/Commands/PostUpgrade/V0_2/PostUpgradeCommand.php

@@ -73,6 +73,17 @@ class PostUpgradeCommand extends Command
                 throw new \RuntimeException('Subdomain table is not empty');
                 throw new \RuntimeException('Subdomain table is not empty');
             }
             }
 
 
+            $this->logger->info('Populate with reserved subdomains');
+            $reservedSubdomains = [
+                'app', 'my', 'api', 'ap2i', 'assistance', 'local', 'ressources', 'logs', 'stats', 'support', 'preprod',
+                'test', 'admin', 'statistiques', 'drive', 'cloud', 'git', 'frames', 'v6', 'v59', 'www', 'myadmin'
+            ];
+            foreach ($reservedSubdomains as $reserved) {
+                $sql = "insert into opentalent.Subdomain (organization_id, subdomain, active)
+                    values (13, '" . $reserved . "', 0);";
+                $opentalentCnn->query($sql);
+            }
+
             $this->logger->info('Populate Subdomain table from openassos.sys_domain');
             $this->logger->info('Populate Subdomain table from openassos.sys_domain');
 
 
             $sql = "SELECT d.pid, REGEXP_REPLACE(d.domainName, '^(.+)\\\\.opentalent\\\\.fr$', '\\\\1')
             $sql = "SELECT d.pid, REGEXP_REPLACE(d.domainName, '^(.+)\\\\.opentalent\\\\.fr$', '\\\\1')

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

@@ -7,6 +7,7 @@ use App\Annotation\OrganizationDefaultValue;
 use App\Repository\Organization\SubdomainRepository;
 use App\Repository\Organization\SubdomainRepository;
 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\ORM\Mapping as ORM;
 use ApiPlatform\Core\Annotation\ApiResource;
 use ApiPlatform\Core\Annotation\ApiResource;
+use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 use Symfony\Component\Serializer\Annotation\Groups;
 use Symfony\Component\Serializer\Annotation\Groups;
 use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
 use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
 
 
@@ -18,9 +19,7 @@ use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
         'get' => [
         'get' => [
             'security' => '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION"))' // TODO: Comment je teste l'organisation sur une collection?
             'security' => '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION"))' // TODO: Comment je teste l'organisation sur une collection?
         ],
         ],
-        'post' => [
-            'security' => 'is_granted("ROLE_ORGANIZATION")'
-        ]
+        'post'
     ],
     ],
     itemOperations: [
     itemOperations: [
         'get' => [
         'get' => [
@@ -34,6 +33,7 @@ use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
 #[ORM\Entity(repositoryClass: SubdomainRepository::class)]
 #[ORM\Entity(repositoryClass: SubdomainRepository::class)]
 #[OrganizationDefaultValue(fieldName: "organization")]
 #[OrganizationDefaultValue(fieldName: "organization")]
 #[ApiFilter(SearchFilter::class, properties: ['subdomain' => 'exact'])]
 #[ApiFilter(SearchFilter::class, properties: ['subdomain' => 'exact'])]
+#[UniqueEntity('email')]
 class Subdomain
 class Subdomain
 {
 {
     #[ORM\Id]
     #[ORM\Id]
@@ -44,7 +44,7 @@ class Subdomain
     #[ORM\ManyToOne(inversedBy: 'subdomains')]
     #[ORM\ManyToOne(inversedBy: 'subdomains')]
     private Organization $organization;
     private Organization $organization;
 
 
-    #[ORM\Column(length: 60, nullable: false)]
+    #[ORM\Column(type: 'string', length: 60, unique: true, nullable: false)]
     #[Groups("subdomain")]
     #[Groups("subdomain")]
     private string $subdomain;
     private string $subdomain;
 
 
@@ -92,6 +92,14 @@ class Subdomain
         return $this->subdomain;
         return $this->subdomain;
     }
     }
 
 
+    /**
+     * @param string $subdomain
+     */
+    public function setSubdomain(string $subdomain): void
+    {
+        $this->subdomain = $subdomain;
+    }
+
     /**
     /**
      * @return bool
      * @return bool
      */
      */
@@ -106,14 +114,5 @@ class Subdomain
     public function setActive(bool $active): void
     public function setActive(bool $active): void
     {
     {
         $this->active = $active;
         $this->active = $active;
-
-        // Ensure it is the only active subdomain
-        if ($active) {
-            foreach ($this->getOrganization()->getSubdomains() as $subdomain) {
-                if ($subdomain !== $this) {
-                    $subdomain->setActive(false);
-                }
-            }
-        }
     }
     }
 }
 }

+ 22 - 0
src/Service/Organization/Utils.php

@@ -8,6 +8,7 @@ use App\Enum\Organization\OrganizationIdsEnum;
 use App\Enum\Organization\SettingsProductEnum;
 use App\Enum\Organization\SettingsProductEnum;
 use App\Service\Utils\UrlBuilder;
 use App\Service\Utils\UrlBuilder;
 use App\Test\Service\Organization\UtilsTest;
 use App\Test\Service\Organization\UtilsTest;
+use Doctrine\Common\Collections\Criteria;
 
 
 /**
 /**
  * Class OrganizationUtils : service rassemblant des fonctions d'aides pour les questions se rapportant à l'organisation
  * Class OrganizationUtils : service rassemblant des fonctions d'aides pour les questions se rapportant à l'organisation
@@ -189,4 +190,25 @@ class Utils
 
 
         return 'https://' . $subdomain . '.opentalent.fr';
         return 'https://' . $subdomain . '.opentalent.fr';
     }
     }
+
+    /**
+     * Update the admin username to match the pattern 'admin{subdomain}'
+     *
+     * @param Organization $organization
+     */
+    public static function updateAdminUsername(Organization $organization): void {
+        $subdomain = self::getOrganizationActiveSubdomain($organization);
+        if (!$subdomain) {
+            throw new \RuntimeException('Organization has no active subdomain : ' . $organization->getId());
+        }
+
+        $criteria = Criteria::create()
+            ->andWhere(Criteria::expr()->eq('adminAccess', 1));
+        $admin = $organization->getAccesses()->matching($criteria)->first();
+        if (!$admin) {
+            throw new \RuntimeException('no admin found for organization ' . $organization->getId());
+        }
+
+        $admin->getPerson()->setUsername('admin' . $subdomain);
+    }
 }
 }

+ 4 - 2
src/Service/Typo3/BindFileService.php

@@ -2,7 +2,9 @@
 
 
 namespace App\Service\Typo3;
 namespace App\Service\Typo3;
 
 
-class BindFileServic
+class BindFileService
 {
 {
-
+    public function registerSubdomain(string $subdomain) {
+        throw new \RuntimeException('not implemented');
+    }
 }
 }