Bläddra i källkod

fix the 'subdomain already active' validation

Olivier Massot 2 år sedan
förälder
incheckning
44e0b50449

+ 19 - 0
src/Repository/Organization/SubdomainRepository.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace App\Repository\Organization;
 
+use App\Entity\Organization\Organization;
 use App\Entity\Organization\Subdomain;
 use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
 use Doctrine\Persistence\ManagerRegistry;
@@ -19,4 +20,22 @@ class SubdomainRepository extends ServiceEntityRepository
     {
         parent::__construct($registry, Subdomain::class);
     }
+
+    /**
+     * Returns the current active subdomain of an organization, or null if there are'nt any
+     *
+     * @param Organization $organization
+     * @return Subdomain
+     */
+    public function getActiveSubdomainOf(Organization $organization): ?Subdomain {
+        $qb = $this->createQueryBuilder('subdomain');
+        $results = $qb
+                ->where('subdomain.organization = :organization')
+                ->andWhere($qb->expr()->eq('subdomain.active', true))
+                ->setParameter('organization', $organization)
+                ->getQuery()
+                ->getResult();
+
+        return $results[0] ?? null;
+    }
 }

+ 6 - 1
src/Service/Typo3/SubdomainService.php

@@ -11,6 +11,7 @@ use App\Repository\Access\AccessRepository;
 use App\Repository\Organization\SubdomainRepository;
 use App\Service\Mailer\Model\SubdomainChangeModel;
 use App\Service\Organization\Utils as OrganizationUtils;
+use Doctrine\DBAL\Connection;
 use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Bundle\SecurityBundle\Security;
 use Symfony\Component\Console\Exception\InvalidArgumentException;
@@ -149,7 +150,10 @@ class SubdomainService
      * @return Subdomain
      */
     public function activateSubdomain(Subdomain $subdomain): Subdomain {
-        if ($subdomain->isActive()) {
+
+        $currentActiveSubdomain = $this->subdomainRepository->getActiveSubdomainOf($subdomain->getOrganization());
+
+        if ($currentActiveSubdomain && $subdomain->getId() === $currentActiveSubdomain->getId()) {
             throw new \RuntimeException('The subdomain is already active');
         }
         if (!$subdomain->getId()) {
@@ -169,6 +173,7 @@ class SubdomainService
         return $subdomain;
     }
 
+
     /**
      * The subdomain becomes the only active subdomain of its organization.
      * New state is persisted is database.