From 5e722c830f231176de9484ae04ea65de0f06dfe5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 7 Dec 2021 21:13:47 +0100 Subject: [PATCH] Allowed to set redirects for default domain via command line or API --- docs/swagger/paths/v2_domains_redirects.json | 10 ------ module/Core/src/Domain/DomainService.php | 29 ++++++++++------ .../src/Domain/DomainServiceInterface.php | 2 -- .../src/Exception/InvalidDomainException.php | 33 ------------------- module/Core/test/Domain/DomainServiceTest.php | 14 +------- .../Exception/InvalidDomainExceptionTest.php | 24 -------------- 6 files changed, 20 insertions(+), 92 deletions(-) delete mode 100644 module/Core/src/Exception/InvalidDomainException.php delete mode 100644 module/Core/test/Exception/InvalidDomainExceptionTest.php diff --git a/docs/swagger/paths/v2_domains_redirects.json b/docs/swagger/paths/v2_domains_redirects.json index d9863dcd..031e1d43 100644 --- a/docs/swagger/paths/v2_domains_redirects.json +++ b/docs/swagger/paths/v2_domains_redirects.json @@ -99,16 +99,6 @@ } } }, - "403": { - "description": "Default domain was provided, and it cannot be edited this way.", - "content": { - "application/problem+json": { - "schema": { - "$ref": "../definitions/Error.json" - } - } - } - }, "500": { "description": "Unexpected error.", "content": { diff --git a/module/Core/src/Domain/DomainService.php b/module/Core/src/Domain/DomainService.php index b5141324..b1ba194f 100644 --- a/module/Core/src/Domain/DomainService.php +++ b/module/Core/src/Domain/DomainService.php @@ -10,11 +10,12 @@ use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface; use Shlinkio\Shlink\Core\Entity\Domain; use Shlinkio\Shlink\Core\Exception\DomainNotFoundException; -use Shlinkio\Shlink\Core\Exception\InvalidDomainException; use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions; use Shlinkio\Shlink\Rest\ApiKey\Role; use Shlinkio\Shlink\Rest\Entity\ApiKey; +use function Functional\first; +use function Functional\group; use function Functional\map; class DomainService implements DomainServiceInterface @@ -31,9 +32,7 @@ class DomainService implements DomainServiceInterface */ public function listDomains(?ApiKey $apiKey = null): array { - /** @var DomainRepositoryInterface $repo */ - $repo = $this->em->getRepository(Domain::class); - $domains = $repo->findDomainsWithout($this->defaultDomain, $apiKey); + [$default, $domains] = $this->defaultDomainAndRest($apiKey); $mappedDomains = map($domains, fn (Domain $domain) => DomainItem::forExistingDomain($domain)); if ($apiKey?->hasRole(Role::DOMAIN_SPECIFIC)) { @@ -41,11 +40,26 @@ class DomainService implements DomainServiceInterface } return [ - DomainItem::forDefaultDomain($this->defaultDomain, $this->redirectOptions), + DomainItem::forDefaultDomain($this->defaultDomain, $default ?? $this->redirectOptions), ...$mappedDomains, ]; } + /** + * @return array{Domain|null, Domain[]} + */ + private function defaultDomainAndRest(?ApiKey $apiKey): array + { + /** @var DomainRepositoryInterface $repo */ + $repo = $this->em->getRepository(Domain::class); + $groups = group( + $repo->findDomainsWithout(null, $apiKey), // FIXME Always called with null as first arg + fn (Domain $domain) => $domain->getAuthority() === $this->defaultDomain ? 'default' : 'domains', + ); + + return [first($groups['default'] ?? []), $groups['domains'] ?? []]; + } + /** * @throws DomainNotFoundException */ @@ -79,17 +93,12 @@ class DomainService implements DomainServiceInterface /** * @throws DomainNotFoundException - * @throws InvalidDomainException */ public function configureNotFoundRedirects( string $authority, NotFoundRedirects $notFoundRedirects, ?ApiKey $apiKey = null, ): Domain { - if ($authority === $this->defaultDomain) { - throw InvalidDomainException::forDefaultDomainRedirects(); - } - $domain = $this->getPersistedDomain($authority, $apiKey); $domain->configureNotFoundRedirects($notFoundRedirects); diff --git a/module/Core/src/Domain/DomainServiceInterface.php b/module/Core/src/Domain/DomainServiceInterface.php index 7748284d..9ac48e69 100644 --- a/module/Core/src/Domain/DomainServiceInterface.php +++ b/module/Core/src/Domain/DomainServiceInterface.php @@ -8,7 +8,6 @@ use Shlinkio\Shlink\Core\Config\NotFoundRedirects; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Entity\Domain; use Shlinkio\Shlink\Core\Exception\DomainNotFoundException; -use Shlinkio\Shlink\Core\Exception\InvalidDomainException; use Shlinkio\Shlink\Rest\Entity\ApiKey; interface DomainServiceInterface @@ -32,7 +31,6 @@ interface DomainServiceInterface /** * @throws DomainNotFoundException If the API key is restricted to one domain and a different one is provided - * @throws InvalidDomainException If default domain is provided */ public function configureNotFoundRedirects( string $authority, diff --git a/module/Core/src/Exception/InvalidDomainException.php b/module/Core/src/Exception/InvalidDomainException.php deleted file mode 100644 index d41e71ac..00000000 --- a/module/Core/src/Exception/InvalidDomainException.php +++ /dev/null @@ -1,33 +0,0 @@ -detail = $e->getMessage(); - $e->title = self::TITLE; - $e->type = self::TYPE; - $e->status = StatusCodeInterface::STATUS_FORBIDDEN; - - return $e; - } -} diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index 159fb6ca..337438b5 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -15,7 +15,6 @@ use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface; use Shlinkio\Shlink\Core\Entity\Domain; use Shlinkio\Shlink\Core\Exception\DomainNotFoundException; -use Shlinkio\Shlink\Core\Exception\InvalidDomainException; use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; @@ -42,7 +41,7 @@ class DomainServiceTest extends TestCase { $repo = $this->prophesize(DomainRepositoryInterface::class); $getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); - $findDomains = $repo->findDomainsWithout('default.com', $apiKey)->willReturn($domains); + $findDomains = $repo->findDomainsWithout(null, $apiKey)->willReturn($domains); $result = $this->domainService->listDomains($apiKey); @@ -214,15 +213,4 @@ class DomainServiceTest extends TestCase yield 'domain not found and author API key' => [null, $authorApiKey]; yield 'domain found and author API key' => [$domain, $authorApiKey]; } - - /** @test */ - public function anExceptionIsThrowsWhenTryingToEditRedirectsForDefaultDomain(): void - { - $this->expectException(InvalidDomainException::class); - $this->expectExceptionMessage( - 'You cannot configure default domain\'s redirects this way. Use the configuration or env vars.', - ); - - $this->domainService->configureNotFoundRedirects('default.com', NotFoundRedirects::withoutRedirects()); - } } diff --git a/module/Core/test/Exception/InvalidDomainExceptionTest.php b/module/Core/test/Exception/InvalidDomainExceptionTest.php deleted file mode 100644 index 06b78ff2..00000000 --- a/module/Core/test/Exception/InvalidDomainExceptionTest.php +++ /dev/null @@ -1,24 +0,0 @@ -getMessage()); - self::assertEquals($expected, $e->getDetail()); - self::assertEquals('Invalid domain', $e->getTitle()); - self::assertEquals('INVALID_DOMAIN', $e->getType()); - self::assertEquals(403, $e->getStatus()); - } -}