浏览代码

make subdomains a subresource of organization and not parameters anymore

Olivier Massot 3 年之前
父节点
当前提交
b44088b98b

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

@@ -68,8 +68,8 @@ class PostUpgradeCommand extends Command
             }
 
             $this->logger->info('Populate Subdomain table from openassos.sys_domain');
-            $sql = "INSERT INTO opentalent.Subdomain (parameters_id, subdomain)
-                    SELECT o.parameters_id, REGEXP_REPLACE(d.domainName, '^(.+)\\\\.opentalent\\\\.fr$', '\\\\1')
+            $sql = "INSERT INTO opentalent.Subdomain (organization_id, subdomain)
+                    SELECT o.id, REGEXP_REPLACE(d.domainName, '^(.+)\\\\.opentalent\\\\.fr$', '\\\\1')
                     FROM openassos.sys_domain d
                     inner join opentalent.Organization o on o.cmsId = d.pid
                     where d.domainName like '%.opentalent.fr';";
@@ -87,20 +87,22 @@ class PostUpgradeCommand extends Command
             $cnn->query($sql);
 
             $this->logger->info('Complete with subdomains from Parameters table');
-            $sql = "insert into opentalent.Subdomain (parameters_id, subdomain)
-                    select distinct p.id, p.subDomain
+            $sql = "insert into opentalent.Subdomain (organization_id, subdomain)
+                    select distinct o.id, p.subDomain
                     from opentalent.Parameters p
+                    inner join opentalent.Organization o on o.parameters_id = p.id
                     left join opentalent.Subdomain s 
-                    on s.parameters_id = p.id
+                    on s.organization_id = o.id
                     where s.id is null;";
             $cnn->query($sql);
 
-            $sql = "insert into opentalent.Subdomain (parameters_id, subdomain)
-                    select p.id, p.subDomain
+            $sql = "insert into opentalent.Subdomain (organization_id, subdomain)
+                    select o.id, p.subDomain
                     from opentalent.Parameters p
+                    inner join opentalent.Organization o on o.parameters_id = p.id
                     inner join opentalent.Subdomain s 
-                    on s.parameters_id = p.id
-                    where p.subDomain not in (select subdomain from opentalent.Subdomain where parameters_id=p.id);";
+                    on s.organization_id = o.id
+                    where p.subDomain not in (select subdomain from opentalent.Subdomain where organization_id=p.id);";
             $cnn->query($sql);
 
             $this->logger->info('Set the current subdomains');
@@ -108,8 +110,8 @@ class PostUpgradeCommand extends Command
             $cnn->query($sql);
 
             $sql = "update opentalent.Subdomain s
-                    inner join opentalent.Parameters p 
-                    on s.parameters_id = p.id and s.subdomain = p.subDomain 
+                    inner join opentalent.Organization o on o.id = s.organization_id
+                    inner join opentalent.Parameters p on p.id = o.parameters_id and s.subdomain = p.subDomain 
                     set s.active = true;";
             $cnn->query($sql);
 

+ 24 - 0
src/Entity/Organization/Organization.php

@@ -224,6 +224,10 @@ class Organization
     #[ORM\OneToMany(mappedBy: 'organization', targetEntity: EducationTiming::class, orphanRemoval: true)]
     private Collection $educationTimings;
 
+    #[ORM\OneToMany( mappedBy: 'organization', targetEntity: Subdomain::class)]
+    #[ApiSubresource]
+    private Collection $subdomains;
+
     #[Pure] public function __construct()
     {
         $this->accesses = new ArrayCollection();
@@ -238,6 +242,7 @@ class Organization
         $this->cycles = new ArrayCollection();
         $this->educationTimings = new ArrayCollection();
         $this->educationNotationConfigs = new ArrayCollection();
+        $this->publicationDirectors = new ArrayCollection();
     }
 
     public function getId(): ?int
@@ -1061,4 +1066,23 @@ class Organization
     {
         return $this->educationTimings;
     }
+
+    public function getSubdomains(): Collection
+    {
+        return $this->subdomains;
+    }
+
+    public function addSubdomain($subdomain): self
+    {
+        $subdomain->setOrganization($this);
+        $this->subdomains[] = $subdomain;
+        return $this;
+    }
+
+    public function removeSubdomain($subdomain): self
+    {
+        $subdomain->setOrganization(null);
+        $this->subdomains->removeElement($subdomain);
+        return $this;
+    }
 }

+ 0 - 28
src/Entity/Organization/Parameters.php

@@ -163,15 +163,6 @@ class Parameters
     #[ORM\Column(options: ['default' => false])]
     private bool $sendAttendanceSms = false;
 
-    #[ORM\OneToMany( mappedBy: 'parameters', targetEntity: Subdomain::class)]
-    #[ApiSubresource]
-    private Collection $subdomains;
-
-    #[Pure] public function __construct()
-    {
-        $this->publicationDirectors = new ArrayCollection();
-    }
-
     public function getId(): ?int
     {
         return $this->id;
@@ -665,23 +656,4 @@ class Parameters
 
         return $this;
     }
-
-    public function getSubdomains(): Collection
-    {
-        return $this->subdomains;
-    }
-
-    public function addSubdomain($subdomain): self
-    {
-        $subdomain->setParameters($this);
-        $this->subdomains[] = $subdomain;
-        return $this;
-    }
-
-    public function removeSubdomain($subdomain): self
-    {
-        $subdomain->setParameters(null);
-        $this->subdomains->removeElement($subdomain);
-        return $this;
-    }
 }

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

@@ -11,7 +11,12 @@ use ApiPlatform\Core\Annotation\ApiResource;
  */
 #[ApiResource(
     collectionOperations: [
-        'get'
+        'get' => [
+            'security' => '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION")) and object.getParameters().getOrganization().getId() == user.getOrganization().getId()'
+        ],
+        'post' => [
+            'security' => 'is_granted("ROLE_ORGANIZATION") and object.getParameters().getOrganization().getId() == user.getOrganization().getId()'
+        ]
     ],
     itemOperations: [
         'get' => [
@@ -31,7 +36,7 @@ class Subdomain
     private ?int $id = null;
 
     #[ORM\ManyToOne(inversedBy: 'subdomains')]
-    private Parameters $parameters;
+    private Organization $organization;
 
     #[ORM\Column(length: 60, nullable: false)]
     private string $subdomain;
@@ -56,19 +61,19 @@ class Subdomain
     }
 
     /**
-     * @return Parameters
+     * @return Organization
      */
-    public function getParameters(): Parameters
+    public function getOrganization(): Organization
     {
-        return $this->parameters;
+        return $this->organization;
     }
 
     /**
-     * @param Parameters $parameters
+     * @param Organization $organization
      */
-    public function setParameters(Parameters $parameters): void
+    public function setOrganization(Organization $organization): void
     {
-        $this->parameters = $parameters;
+        $this->organization = $organization;
     }
 
     /**