Commit d90fe210 authored by Thomas Citharel's avatar Thomas Citharel 🌲

make domain unique again ! :trump:

parent 18f432a9
Pipeline #25896 passed with stage
in 11 minutes and 6 seconds
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Add unique constraint on domain name
*/
class Version20171017124240 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('CREATE UNIQUE INDEX UNIQ_A7A91E0BF3FF5361 ON domain (domain_name)');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('DROP INDEX UNIQ_A7A91E0BF3FF5361');
}
}
......@@ -361,7 +361,7 @@ class DomainController extends Controller
$em = $this->getDoctrine()->getManager();
if ($em->getRepository('AppBundle:Domain')->getActiveDomainByName($domain->getDomainName())) {
if ($em->getRepository('AppBundle:Domain')->findOneBy(['domainName' => $domain->getDomainName()])) {
$this->createAccessDeniedException('This domain is already attached');
}
......@@ -424,7 +424,7 @@ class DomainController extends Controller
$createTaskId = $this->certTaskFactory->createCertTaskForAttachment($this->getUser(), $domain, CertData::ACTION_CREATE);
if ($domain->getStatus() === Domain::DOMAIN_VHOST_ADDED && $previousSite !== $newSite) {
if ($domain->getStatus() === Domain::DOMAIN_OK && $previousSite !== $newSite) {
$this->certTaskFactory->createCertTaskForAttachment($this->getUser(), $oldDomain, CertData::ACTION_REDUCE, CertTask::STATUS_WAITING, $createTaskId);
}
$domain->setStatus(Domain::DOMAIN_WAITING_CONFIGURATION);
......@@ -555,8 +555,7 @@ class DomainController extends Controller
* Remove it from our database
*/
$domain->removeSite();
$domain->setStatus(Domain::DOMAIN_REMOVED);
$em->persist($domain);
$em->remove($domain);
$em->flush();
return $this->redirectToRoute('domain');
......
......@@ -334,7 +334,7 @@ class SiteController extends Controller
foreach ($site->getDomains() as $domain) {
/** @var $domain Domain */
$domain->removeSite();
$domain->setStatus(Domain::DOMAIN_REMOVED);
$domain->setStatus(Domain::DOMAIN_DETACHED);
$em->persist($domain);
/**
......
......@@ -2,7 +2,6 @@
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Validator\Constraints as FramasitesAssert;
......@@ -14,7 +13,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
* @ORM\HasLifecycleCallbacks()
* @FramasitesAssert\ConstraintValidDomainConfiguration
* @UniqueEntity(
* fields={"domainName", "site"},
* fields={"domain_name"},
* message="domain.attach.already_attached"
* )
*/
......@@ -33,7 +32,7 @@ class Domain
* @Assert\NotBlank
* @Assert\NotEqualTo("frama.site")
* @Assert\NotEqualTo("frama.wiki")
* @ORM\Column(name="domain_name", type="string")
* @ORM\Column(name="domain_name", type="string", unique=true)
*/
private $domainName;
......@@ -110,11 +109,15 @@ class Domain
const DOMAIN_VALIDATED = 30;
const DOMAIN_DNS_CHANGED = 40;
const DOMAIN_WAITING_CONFIGURATION = 50;
const DOMAIN_VHOST_ADDED = 60;
const DOMAIN_VHOST_ADDED = 60; // Depreciated
const DOMAIN_OK = 100;
const DOMAIN_EXPIRED = 70;
const DOMAIN_REMOVED = 200;
const DOMAIN_DETACHED = 150;
const DOMAIN_REMOVED = 200; // Depreciated
/**
* Domain constructor.
*/
public function __construct()
{
$this->contacts = [];
......
......@@ -88,8 +88,7 @@ class UserFactory
foreach ($user->getDomains() as $domain) {
/** @var $domain Domain */
$this->detachDomain($domain);
$domain->setStatus(Domain::DOMAIN_REMOVED);
$this->entityManager->persist($domain);
$this->entityManager->remove($domain);
}
/**
......@@ -120,8 +119,7 @@ class UserFactory
/** @var $domain Domain */
$this->detachDomain($domain);
$domain->removeSite();
$domain->setStatus(Domain::DOMAIN_REMOVED);
$this->entityManager->persist($domain);
$this->entityManager->remove($domain);
}
/**
......
......@@ -34,17 +34,4 @@ class DomainRepository extends EntityRepository
->getQuery()
->getSingleScalarResult();
}
/**
* @param string $domainName
* @return mixed
*/
public function getActiveDomainByName(string $domainName)
{
return $this->createQueryBuilder('d')
->andWhere('d.domainName = :domainName')->setParameter(':domainName', $domainName)
->andWhere('d.status <= 100')
->getQuery()
->getOneOrNullResult();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment