Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the DNS mapping migration #6529

Merged
merged 18 commits into from Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions core-bundle/config/migrations.yaml
Expand Up @@ -14,6 +14,12 @@ services:
- '@database_connection'
- '@contao.doctrine.schema_provider'

contao.migration.environment.dns:
class: Contao\CoreBundle\Migration\Environment\DnsMigration
arguments:
- '@database_connection'
- '%contao.dns_mapping%'

contao.migration.version_500.accesskey:
class: Contao\CoreBundle\Migration\Version500\AccesskeyMigration
arguments:
Expand Down
8 changes: 8 additions & 0 deletions core-bundle/src/DependencyInjection/ContaoCoreExtension.php
Expand Up @@ -138,6 +138,14 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('contao.insert_tags.allowed_tags', $config['insert_tags']['allowed_tags']);
$container->setParameter('contao.sanitizer.allowed_url_protocols', $config['sanitizer']['allowed_url_protocols']);

/**
* TODO: provide a bundle configuration for this parameter once it is possible to reference
* environment variables for array config nodes (https://github.com/symfony/symfony/issues/40906)
*/
if (!$container->hasParameter('contao.dns_mapping')) {
$container->setParameter('contao.dns_mapping', []);
}

$this->handleMessengerConfig($config, $container);
$this->handleSearchConfig($config, $container);
$this->handleCrawlConfig($config, $container);
Expand Down
142 changes: 142 additions & 0 deletions core-bundle/src/Migration/Environment/DnsMigration.php
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Migration\Environment;

use Contao\CoreBundle\Migration\AbstractMigration;
use Contao\CoreBundle\Migration\MigrationResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;

class DnsMigration extends AbstractMigration
{
public function __construct(
private readonly Connection $db,
private readonly array $mapping,
) {
}

public function shouldRun(): bool
{
if (!$this->mapping) {
return false;
}

$schemaManager = $this->db->createSchemaManager();

if (!$schemaManager->tablesExist(['tl_page'])) {
return false;
}

$columns = $schemaManager->listTableColumns('tl_page');

if (!isset($columns['dns']) || !isset($columns['type']) || !isset($columns['usessl'])) {
return false;
}

foreach ($this->mapping as $from => $to) {
$from = $this->parseHost($from);
$to = $this->parseHost($to);

$qb = $this->db
->createQueryBuilder()
->select('TRUE')
->from('tl_page')
->where("type = 'root'")
;

if ($from['scheme']) {
$qb->andWhere('https:' === $from['scheme'] ? 'useSSL = 1' : 'useSSL = 0');
}

if (null !== $from['host']) {
$qb
->andWhere('dns = :fromHost')
->setParameter('fromHost', $from['host'])
;
}

$or = [];

if ($to['scheme']) {
$or[] = 'https:' === $to['scheme'] ? 'useSSL = 0' : 'useSSL = 1';
}

if (null !== $to['host']) {
$or[] = 'dns != :toHost';
$qb->setParameter('toHost', $to['host']);
}

if ($or) {
$qb->andWhere($qb->expr()->or(...$or));
}

if ($qb->executeQuery()->fetchOne()) {
return true;
}
}

return false;
}

public function run(): MigrationResult
{
foreach ($this->mapping as $from => $to) {
$from = $this->parseHost($from);
$to = $this->parseHost($to);

$qb = $this->db->createQueryBuilder()
->update('tl_page')
->where("type = 'root'")
;

if ($from['scheme']) {
$qb->andWhere('https:' === $from['scheme'] ? 'useSSL = 1' : 'useSSL = 0');
}

if (null !== $from['host']) {
$qb
->andWhere('dns = :fromHost')
->setParameter('fromHost', $from['host'])
;
}

if ($to['scheme']) {
$qb
->set('useSSL', ':useSSL')
->setParameter('useSSL', 'https:' === $to['scheme'], ParameterType::BOOLEAN)
;
}

if (null !== $to['host']) {
$qb
->set('dns', ':toHost')
->setParameter('toHost', $to['host'])
;
}

$qb->executeQuery();
}

return $this->createResult(true);
}

private function parseHost(string $host): array
{
[$host, $scheme] = array_reverse(explode('//', $host)) + [null, null];

return [
'host' => $host,
'scheme' => $scheme,
];
}
}
19 changes: 19 additions & 0 deletions core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php
Expand Up @@ -856,6 +856,25 @@ public function testRegistersAsHookAttribute(): void
);
}

public function testSetsDefaultDnsMappingParameter(): void
{
$container = $this->getContainerBuilder();

(new ContaoCoreExtension())->load([], $container);

$this->assertSame([], $container->getParameter('contao.dns_mapping'));
}

public function testDoesNotOverwriteDnsMappingParameter(): void
{
$container = $this->getContainerBuilder();
$container->setParameter('contao.dns_mapping', ['example.com' => 'example.local']);

(new ContaoCoreExtension())->load([], $container);

$this->assertSame(['example.com' => 'example.local'], $container->getParameter('contao.dns_mapping'));
}

public function testRegistersAsCallbackAttribute(): void
{
$container = $this->getContainerBuilder();
Expand Down