mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Merge pull request #1140 from acelaya-forks/feature/domain-redirects-endpoint
Feature/domain redirects endpoint
This commit is contained in:
commit
444a1756a2
@ -19,7 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
|
||||
|
||||
* [#943](https://github.com/shlinkio/shlink/issues/943) Added support to define different "not-found" redirects for every domain handled by Shlink.
|
||||
|
||||
Shlink will continue to allow defining the default values via env vars or config, but afterwards, you can use the `domain:redirects` command to define specific values for every single domain.
|
||||
Shlink will continue to allow defining the default values via env vars or config, but afterwards, you can use the `domain:redirects` command or the `PATCH /domains/redirects` REST endpoint to define specific values for every single domain.
|
||||
|
||||
### Changed
|
||||
* [#1118](https://github.com/shlinkio/shlink/issues/1118) Increased phpstan required level to 8.
|
||||
|
@ -51,7 +51,7 @@
|
||||
"shlinkio/shlink-config": "^1.0",
|
||||
"shlinkio/shlink-event-dispatcher": "^2.1",
|
||||
"shlinkio/shlink-importer": "^2.3.1",
|
||||
"shlinkio/shlink-installer": "dev-develop#fa6a4ca as 6.1",
|
||||
"shlinkio/shlink-installer": "dev-develop#0ddcc3d as 6.1",
|
||||
"shlinkio/shlink-ip-geolocation": "^2.0",
|
||||
"symfony/console": "^5.1",
|
||||
"symfony/filesystem": "^5.1",
|
||||
|
20
docs/swagger/definitions/NotFoundRedirects.json
Normal file
20
docs/swagger/definitions/NotFoundRedirects.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"baseUrlRedirect": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"description": "URL to redirect to when a user hits the domain's base URL"
|
||||
},
|
||||
"regular404Redirect": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"description": "URL to redirect to when a user hits a not found URL other than an invalid short URL"
|
||||
},
|
||||
"invalidShortUrlRedirect": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"description": "URL to redirect to when a user hits an invalid short URL"
|
||||
}
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "The list of tags",
|
||||
"description": "The list of domains",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
@ -33,13 +33,16 @@
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["domain", "isDefault"],
|
||||
"required": ["domain", "isDefault", "redirects"],
|
||||
"properties": {
|
||||
"domain": {
|
||||
"type": "string"
|
||||
},
|
||||
"isDefault": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"redirects": {
|
||||
"$ref": "../definitions/NotFoundRedirects.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -56,15 +59,30 @@
|
||||
"data": [
|
||||
{
|
||||
"domain": "example.com",
|
||||
"isDefault": true
|
||||
"isDefault": true,
|
||||
"redirects": {
|
||||
"baseUrlRedirect": "https://example.com/my-landing-page",
|
||||
"regular404Redirect": null,
|
||||
"invalidShortUrlRedirect": "https://example.com/invalid-url"
|
||||
}
|
||||
},
|
||||
{
|
||||
"domain": "aaa.com",
|
||||
"isDefault": false
|
||||
"isDefault": false,
|
||||
"redirects": {
|
||||
"baseUrlRedirect": null,
|
||||
"regular404Redirect": null,
|
||||
"invalidShortUrlRedirect": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"domain": "bbb.com",
|
||||
"isDefault": false
|
||||
"isDefault": false,
|
||||
"redirects": {
|
||||
"baseUrlRedirect": null,
|
||||
"regular404Redirect": null,
|
||||
"invalidShortUrlRedirect": "https://example.com/invalid-url"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
124
docs/swagger/paths/v2_domains_redirects.json
Normal file
124
docs/swagger/paths/v2_domains_redirects.json
Normal file
@ -0,0 +1,124 @@
|
||||
{
|
||||
"patch": {
|
||||
"operationId": "setDomainRedirects",
|
||||
"tags": [
|
||||
"Domains"
|
||||
],
|
||||
"summary": "Sets domain \"not found\" redirects",
|
||||
"description": "Sets the URLs that you want a visitor to get redirected to for \not found\" URLs for a specific domain",
|
||||
"security": [
|
||||
{
|
||||
"ApiKey": []
|
||||
}
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"$ref": "../parameters/version.json"
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"description": "Request body.",
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"allOf": [
|
||||
{
|
||||
"required": ["domain"],
|
||||
"properties": {
|
||||
"domain": {
|
||||
"description": "The domain's authority for which you want to set redirects",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"$ref": "../definitions/NotFoundRedirects.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "The domain's redirects after the update, when existing redirects have been merged with provided ones.",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"required": ["baseUrlRedirect", "regular404Redirect", "invalidShortUrlRedirect"]
|
||||
},
|
||||
{
|
||||
"$ref": "../definitions/NotFoundRedirects.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"examples": {
|
||||
"application/json": {
|
||||
"baseUrlRedirect": "https://example.com/my-landing-page",
|
||||
"regular404Redirect": null,
|
||||
"invalidShortUrlRedirect": "https://example.com/invalid-url"
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Provided data is invalid.",
|
||||
"content": {
|
||||
"application/problem+json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "../definitions/Error.json"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": ["invalidElements"],
|
||||
"properties": {
|
||||
"invalidElements": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"domain",
|
||||
"baseUrlRedirect",
|
||||
"regular404Redirect",
|
||||
"invalidShortUrlRedirect"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"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": {
|
||||
"application/problem+json": {
|
||||
"schema": {
|
||||
"$ref": "../definitions/Error.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"openapi": "3.0.0",
|
||||
"openapi": "3.0.3",
|
||||
"info": {
|
||||
"title": "Shlink",
|
||||
"description": "Shlink, the self-hosted URL shortener",
|
||||
@ -102,6 +102,9 @@
|
||||
"/rest/v{version}/domains": {
|
||||
"$ref": "paths/v2_domains.json"
|
||||
},
|
||||
"/rest/v{version}/domains/redirects": {
|
||||
"$ref": "paths/v2_domains_redirects.json"
|
||||
},
|
||||
|
||||
"/rest/v{version}/mercure-info": {
|
||||
"$ref": "paths/v2_mercure-info.json"
|
||||
|
@ -97,7 +97,7 @@ class GenerateKeyCommand extends BaseCommand
|
||||
$io->success(sprintf('Generated API key: "%s"', $apiKey->toString()));
|
||||
|
||||
if (! $apiKey->isAdmin()) {
|
||||
ShlinkTable::fromOutput($io)->render(
|
||||
ShlinkTable::default($io)->render(
|
||||
['Role name', 'Role metadata'],
|
||||
$apiKey->mapRoles(fn (string $name, array $meta) => [$name, arrayToString($meta, 0)]),
|
||||
null,
|
||||
|
@ -69,7 +69,7 @@ class ListKeysCommand extends BaseCommand
|
||||
return $rowData;
|
||||
});
|
||||
|
||||
ShlinkTable::fromOutput($output)->render(array_filter([
|
||||
ShlinkTable::withRowSeparators($output)->render(array_filter([
|
||||
'Key',
|
||||
'Name',
|
||||
! $enabledOnly ? 'Is enabled' : null,
|
||||
|
@ -92,7 +92,7 @@ class DomainRedirectsCommand extends Command
|
||||
};
|
||||
};
|
||||
|
||||
$this->domainService->configureNotFoundRedirects($domainAuthority, new NotFoundRedirects(
|
||||
$this->domainService->configureNotFoundRedirects($domainAuthority, NotFoundRedirects::withRedirects(
|
||||
$ask(
|
||||
'URL to redirect to when a user hits this domain\'s base URL',
|
||||
$domain?->baseUrlRedirect(),
|
||||
|
@ -43,8 +43,9 @@ class ListDomainsCommand extends Command
|
||||
$domains = $this->domainService->listDomains();
|
||||
$showRedirects = $input->getOption('show-redirects');
|
||||
$commonFields = ['Domain', 'Is default'];
|
||||
$table = $showRedirects ? ShlinkTable::withRowSeparators($output) : ShlinkTable::default($output);
|
||||
|
||||
ShlinkTable::fromOutput($output)->render(
|
||||
$table->render(
|
||||
$showRedirects ? [...$commonFields, '"Not found" redirects'] : $commonFields,
|
||||
map($domains, function (DomainItem $domain) use ($showRedirects) {
|
||||
$commonValues = [$domain->toString(), $domain->isDefault() ? 'Yes' : 'No'];
|
||||
|
@ -81,7 +81,7 @@ class GetVisitsCommand extends AbstractWithDateRangeCommand
|
||||
$rowData['country'] = ($visit->getVisitLocation() ?? new UnknownVisitLocation())->getCountryName();
|
||||
return select_keys($rowData, ['referer', 'date', 'userAgent', 'country']);
|
||||
});
|
||||
ShlinkTable::fromOutput($output)->render(['Referer', 'Date', 'User agent', 'Country'], $rows);
|
||||
ShlinkTable::default($output)->render(['Referer', 'Date', 'User agent', 'Country'], $rows);
|
||||
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand
|
||||
return map($columnsMap, fn (callable $call) => $call($rawShortUrl, $shortUrl));
|
||||
});
|
||||
|
||||
ShlinkTable::fromOutput($output)->render(
|
||||
ShlinkTable::default($output)->render(
|
||||
array_keys($columnsMap),
|
||||
$rows,
|
||||
$all ? null : $this->formatCurrentPageMessage($shortUrls, 'Page %s of %s'),
|
||||
|
@ -32,7 +32,7 @@ class ListTagsCommand extends Command
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): ?int
|
||||
{
|
||||
ShlinkTable::fromOutput($output)->render(['Name', 'URLs amount', 'Visits amount'], $this->getTagsRows());
|
||||
ShlinkTable::default($output)->render(['Name', 'URLs amount', 'Visits amount'], $this->getTagsRows());
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -5,20 +5,33 @@ declare(strict_types=1);
|
||||
namespace Shlinkio\Shlink\CLI\Util;
|
||||
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Helper\TableSeparator;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use function Functional\intersperse;
|
||||
|
||||
final class ShlinkTable
|
||||
{
|
||||
private const DEFAULT_STYLE_NAME = 'default';
|
||||
private const TABLE_TITLE_STYLE = '<options=bold> %s </>';
|
||||
|
||||
public function __construct(private Table $baseTable)
|
||||
private function __construct(private Table $baseTable, private bool $withRowSeparators)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromOutput(OutputInterface $output): self
|
||||
public static function default(OutputInterface $output): self
|
||||
{
|
||||
return new self(new Table($output));
|
||||
return new self(new Table($output), false);
|
||||
}
|
||||
|
||||
public static function withRowSeparators(OutputInterface $output): self
|
||||
{
|
||||
return new self(new Table($output), true);
|
||||
}
|
||||
|
||||
public static function fromBaseTable(Table $baseTable): self
|
||||
{
|
||||
return new self($baseTable, false);
|
||||
}
|
||||
|
||||
public function render(array $headers, array $rows, ?string $footerTitle = null, ?string $headerTitle = null): void
|
||||
@ -26,11 +39,12 @@ final class ShlinkTable
|
||||
$style = Table::getStyleDefinition(self::DEFAULT_STYLE_NAME);
|
||||
$style->setFooterTitleFormat(self::TABLE_TITLE_STYLE)
|
||||
->setHeaderTitleFormat(self::TABLE_TITLE_STYLE);
|
||||
$tableRows = $this->withRowSeparators ? intersperse($rows, new TableSeparator()) : $rows;
|
||||
|
||||
$table = clone $this->baseTable;
|
||||
$table->setStyle($style)
|
||||
->setHeaders($headers)
|
||||
->setRows($rows)
|
||||
->setRows($tableRows)
|
||||
->setFooterTitle($footerTitle)
|
||||
->setHeaderTitle($headerTitle)
|
||||
->render();
|
||||
|
@ -53,7 +53,9 @@ class ListKeysCommandTest extends TestCase
|
||||
| Key | Name | Is enabled | Expiration date | Roles |
|
||||
+--------------------------------------+------+------------+-----------------+-------+
|
||||
| {$apiKey1} | - | +++ | - | Admin |
|
||||
+--------------------------------------+------+------------+-----------------+-------+
|
||||
| {$apiKey2} | - | +++ | - | Admin |
|
||||
+--------------------------------------+------+------------+-----------------+-------+
|
||||
| {$apiKey3} | - | +++ | - | Admin |
|
||||
+--------------------------------------+------+------------+-----------------+-------+
|
||||
|
||||
@ -67,6 +69,7 @@ class ListKeysCommandTest extends TestCase
|
||||
| Key | Name | Expiration date | Roles |
|
||||
+--------------------------------------+------+-----------------+-------+
|
||||
| {$apiKey1} | - | - | Admin |
|
||||
+--------------------------------------+------+-----------------+-------+
|
||||
| {$apiKey2} | - | - | Admin |
|
||||
+--------------------------------------+------+-----------------+-------+
|
||||
|
||||
@ -92,11 +95,16 @@ class ListKeysCommandTest extends TestCase
|
||||
| Key | Name | Expiration date | Roles |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
| {$apiKey1} | - | - | Admin |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
| {$apiKey2} | - | - | Author only |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
| {$apiKey3} | - | - | Domain only: example.com |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
| {$apiKey4} | - | - | Admin |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
| {$apiKey5} | - | - | Author only |
|
||||
| | | | Domain only: example.com |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
| {$apiKey6} | - | - | Admin |
|
||||
+--------------------------------------+------+-----------------+--------------------------+
|
||||
|
||||
@ -115,8 +123,11 @@ class ListKeysCommandTest extends TestCase
|
||||
| Key | Name | Expiration date | Roles |
|
||||
+--------------------------------------+---------------+-----------------+-------+
|
||||
| {$apiKey1} | Alice | - | Admin |
|
||||
+--------------------------------------+---------------+-----------------+-------+
|
||||
| {$apiKey2} | Alice and Bob | - | Admin |
|
||||
+--------------------------------------+---------------+-----------------+-------+
|
||||
| {$apiKey3} | | - | Admin |
|
||||
+--------------------------------------+---------------+-----------------+-------+
|
||||
| {$apiKey4} | - | - | Admin |
|
||||
+--------------------------------------+---------------+-----------------+-------+
|
||||
|
||||
|
@ -40,7 +40,7 @@ class DomainRedirectsCommandTest extends TestCase
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects('foo.com', null, 'baz.com'),
|
||||
NotFoundRedirects::withRedirects('foo.com', null, 'baz.com'),
|
||||
)->willReturn(Domain::withAuthority(''));
|
||||
|
||||
$this->commandTester->setInputs(['foo.com', '', 'baz.com']);
|
||||
@ -71,12 +71,12 @@ class DomainRedirectsCommandTest extends TestCase
|
||||
{
|
||||
$domainAuthority = 'example.com';
|
||||
$domain = Domain::withAuthority($domainAuthority);
|
||||
$domain->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com', 'baz.com'));
|
||||
$domain->configureNotFoundRedirects(NotFoundRedirects::withRedirects('foo.com', 'bar.com', 'baz.com'));
|
||||
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(null, 'edited.com', 'baz.com'),
|
||||
NotFoundRedirects::withRedirects(null, 'edited.com', 'baz.com'),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs(['2', '1', 'edited.com', '0']);
|
||||
@ -105,7 +105,7 @@ class DomainRedirectsCommandTest extends TestCase
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(),
|
||||
NotFoundRedirects::withoutRedirects(),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs([$domainAuthority, '', '', '']);
|
||||
@ -132,7 +132,7 @@ class DomainRedirectsCommandTest extends TestCase
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(),
|
||||
NotFoundRedirects::withoutRedirects(),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs(['1', '', '', '']);
|
||||
@ -162,7 +162,7 @@ class DomainRedirectsCommandTest extends TestCase
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(),
|
||||
NotFoundRedirects::withoutRedirects(),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs(['2', $domainAuthority, '', '', '']);
|
||||
|
@ -36,7 +36,7 @@ class ListDomainsCommandTest extends TestCase
|
||||
public function allDomainsAreProperlyPrinted(array $input, string $expectedOutput): void
|
||||
{
|
||||
$bazDomain = Domain::withAuthority('baz.com');
|
||||
$bazDomain->configureNotFoundRedirects(new NotFoundRedirects(
|
||||
$bazDomain->configureNotFoundRedirects(NotFoundRedirects::withRedirects(
|
||||
null,
|
||||
'https://foo.com/baz-domain/regular',
|
||||
'https://foo.com/baz-domain/invalid',
|
||||
@ -77,9 +77,11 @@ class ListDomainsCommandTest extends TestCase
|
||||
| foo.com | Yes | * Base URL: https://foo.com/default/base |
|
||||
| | | * Regular 404: N/A |
|
||||
| | | * Invalid short URL: https://foo.com/default/invalid |
|
||||
+---------+------------+---------------------------------------------------------+
|
||||
| bar.com | No | * Base URL: N/A |
|
||||
| | | * Regular 404: N/A |
|
||||
| | | * Invalid short URL: N/A |
|
||||
+---------+------------+---------------------------------------------------------+
|
||||
| baz.com | No | * Base URL: N/A |
|
||||
| | | * Regular 404: https://foo.com/baz-domain/regular |
|
||||
| | | * Invalid short URL: https://foo.com/baz-domain/invalid |
|
||||
|
@ -24,7 +24,7 @@ class ShlinkTableTest extends TestCase
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->baseTable = $this->prophesize(Table::class);
|
||||
$this->shlinkTable = new ShlinkTable($this->baseTable->reveal());
|
||||
$this->shlinkTable = ShlinkTable::fromBaseTable($this->baseTable->reveal());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@ -57,7 +57,7 @@ class ShlinkTableTest extends TestCase
|
||||
/** @test */
|
||||
public function newTableIsCreatedForFactoryMethod(): void
|
||||
{
|
||||
$instance = ShlinkTable::fromOutput($this->prophesize(OutputInterface::class)->reveal());
|
||||
$instance = ShlinkTable::default($this->prophesize(OutputInterface::class)->reveal());
|
||||
|
||||
$ref = new ReflectionObject($instance);
|
||||
$baseTable = $ref->getProperty('baseTable');
|
||||
|
@ -4,15 +4,35 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
final class NotFoundRedirects
|
||||
use JsonSerializable;
|
||||
|
||||
final class NotFoundRedirects implements JsonSerializable
|
||||
{
|
||||
public function __construct(
|
||||
private ?string $baseUrlRedirect = null,
|
||||
private ?string $regular404Redirect = null,
|
||||
private ?string $invalidShortUrlRedirect = null,
|
||||
private function __construct(
|
||||
private ?string $baseUrlRedirect,
|
||||
private ?string $regular404Redirect,
|
||||
private ?string $invalidShortUrlRedirect,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function withRedirects(
|
||||
?string $baseUrlRedirect,
|
||||
?string $regular404Redirect = null,
|
||||
?string $invalidShortUrlRedirect = null,
|
||||
): self {
|
||||
return new self($baseUrlRedirect, $regular404Redirect, $invalidShortUrlRedirect);
|
||||
}
|
||||
|
||||
public static function withoutRedirects(): self
|
||||
{
|
||||
return new self(null, null, null);
|
||||
}
|
||||
|
||||
public static function fromConfig(NotFoundRedirectConfigInterface $config): self
|
||||
{
|
||||
return new self($config->baseUrlRedirect(), $config->regular404Redirect(), $config->invalidShortUrlRedirect());
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrlRedirect;
|
||||
@ -27,4 +47,13 @@ final class NotFoundRedirects
|
||||
{
|
||||
return $this->invalidShortUrlRedirect;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'baseUrlRedirect' => $this->baseUrlRedirect,
|
||||
'regular404Redirect' => $this->regular404Redirect,
|
||||
'invalidShortUrlRedirect' => $this->invalidShortUrlRedirect,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ 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;
|
||||
@ -59,29 +60,58 @@ class DomainService implements DomainServiceInterface
|
||||
return $domain;
|
||||
}
|
||||
|
||||
public function findByAuthority(string $authority): ?Domain
|
||||
public function findByAuthority(string $authority, ?ApiKey $apiKey = null): ?Domain
|
||||
{
|
||||
$repo = $this->em->getRepository(Domain::class);
|
||||
return $repo->findOneBy(['authority' => $authority]);
|
||||
return $repo->findOneByAuthority($authority, $apiKey);
|
||||
}
|
||||
|
||||
public function getOrCreate(string $authority): Domain
|
||||
/**
|
||||
* @throws DomainNotFoundException
|
||||
*/
|
||||
public function getOrCreate(string $authority, ?ApiKey $apiKey = null): Domain
|
||||
{
|
||||
$domain = $this->findByAuthority($authority) ?? Domain::withAuthority($authority);
|
||||
|
||||
$this->em->persist($domain);
|
||||
$domain = $this->getPersistedDomain($authority, $apiKey);
|
||||
$this->em->flush();
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
public function configureNotFoundRedirects(string $authority, NotFoundRedirects $notFoundRedirects): Domain
|
||||
{
|
||||
$domain = $this->getOrCreate($authority);
|
||||
/**
|
||||
* @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);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DomainNotFoundException
|
||||
*/
|
||||
private function getPersistedDomain(string $authority, ?ApiKey $apiKey): Domain
|
||||
{
|
||||
$domain = $this->findByAuthority($authority, $apiKey);
|
||||
if ($domain === null && $apiKey?->hasRole(Role::DOMAIN_SPECIFIC)) {
|
||||
// This API key is restricted to one domain and a different one was tried to be fetched
|
||||
throw DomainNotFoundException::fromAuthority($authority);
|
||||
}
|
||||
|
||||
$domain = $domain ?? Domain::withAuthority($authority);
|
||||
$this->em->persist($domain);
|
||||
|
||||
return $domain;
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ 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
|
||||
@ -22,9 +23,20 @@ interface DomainServiceInterface
|
||||
*/
|
||||
public function getDomain(string $domainId): Domain;
|
||||
|
||||
public function getOrCreate(string $authority): Domain;
|
||||
/**
|
||||
* @throws DomainNotFoundException If the API key is restricted to one domain and a different one is provided
|
||||
*/
|
||||
public function getOrCreate(string $authority, ?ApiKey $apiKey = null): Domain;
|
||||
|
||||
public function findByAuthority(string $authority): ?Domain;
|
||||
public function findByAuthority(string $authority, ?ApiKey $apiKey = null): ?Domain;
|
||||
|
||||
public function configureNotFoundRedirects(string $authority, NotFoundRedirects $notFoundRedirects): Domain;
|
||||
/**
|
||||
* @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,
|
||||
NotFoundRedirects $notFoundRedirects,
|
||||
?ApiKey $apiKey = null,
|
||||
): Domain;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Domain\Model;
|
||||
|
||||
use JsonSerializable;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
|
||||
final class DomainItem implements JsonSerializable
|
||||
@ -32,6 +33,7 @@ final class DomainItem implements JsonSerializable
|
||||
return [
|
||||
'domain' => $this->authority,
|
||||
'isDefault' => $this->isDefault,
|
||||
'redirects' => NotFoundRedirects::fromConfig($this->notFoundRedirectConfig),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,13 @@ namespace Shlinkio\Shlink\Core\Domain\Repository;
|
||||
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
use Shlinkio\Shlink\Core\Domain\Spec\IsDomain;
|
||||
use Shlinkio\Shlink\Core\Domain\Spec\IsNotAuthority;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToApiKey;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class DomainRepository extends EntitySpecificationRepository implements DomainRepositoryInterface
|
||||
@ -19,22 +24,50 @@ class DomainRepository extends EntitySpecificationRepository implements DomainRe
|
||||
{
|
||||
$qb = $this->createQueryBuilder('d');
|
||||
$qb->leftJoin(ShortUrl::class, 's', Join::WITH, 's.domain = d')
|
||||
->orderBy('d.authority', 'ASC')
|
||||
->groupBy('d')
|
||||
->orderBy('d.authority', 'ASC')
|
||||
->having($qb->expr()->gt('COUNT(s.id)', '0'))
|
||||
->orHaving($qb->expr()->isNotNull('d.baseUrlRedirect'))
|
||||
->orHaving($qb->expr()->isNotNull('d.regular404Redirect'))
|
||||
->orHaving($qb->expr()->isNotNull('d.invalidShortUrlRedirect'));
|
||||
|
||||
if ($excludedAuthority !== null) {
|
||||
$qb->where($qb->expr()->neq('d.authority', ':excludedAuthority'))
|
||||
->setParameter('excludedAuthority', $excludedAuthority);
|
||||
}
|
||||
|
||||
if ($apiKey !== null) {
|
||||
$this->applySpecification($qb, $apiKey->spec(), 's');
|
||||
$specs = $this->determineExtraSpecs($excludedAuthority, $apiKey);
|
||||
foreach ($specs as [$alias, $spec]) {
|
||||
$this->applySpecification($qb, $spec, $alias);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function findOneByAuthority(string $authority, ?ApiKey $apiKey = null): ?Domain
|
||||
{
|
||||
$qb = $this->createQueryBuilder('d');
|
||||
$qb->leftJoin(ShortUrl::class, 's', Join::WITH, 's.domain = d')
|
||||
->where($qb->expr()->eq('d.authority', ':authority'))
|
||||
->setParameter('authority', $authority)
|
||||
->setMaxResults(1);
|
||||
|
||||
$specs = $this->determineExtraSpecs(null, $apiKey);
|
||||
foreach ($specs as [$alias, $spec]) {
|
||||
$this->applySpecification($qb, $spec, $alias);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getOneOrNullResult();
|
||||
}
|
||||
|
||||
private function determineExtraSpecs(?string $excludedAuthority, ?ApiKey $apiKey): iterable
|
||||
{
|
||||
if ($excludedAuthority !== null) {
|
||||
yield ['d', new IsNotAuthority($excludedAuthority)];
|
||||
}
|
||||
|
||||
// FIXME The $apiKey->spec() method cannot be used here, as it returns a single spec which assumes the
|
||||
// ShortUrl is the root entity. Here, the Domain is the root entity.
|
||||
// Think on a way to centralize the conditional behavior and make $apiKey->spec() more flexible.
|
||||
yield from $apiKey?->mapRoles(fn (string $roleName, array $meta) => match ($roleName) {
|
||||
Role::DOMAIN_SPECIFIC => ['d', new IsDomain(Role::domainIdFromMeta($meta))],
|
||||
Role::AUTHORED_SHORT_URLS => ['s', new BelongsToApiKey($apiKey)],
|
||||
default => [null, Spec::andX()],
|
||||
}) ?? [];
|
||||
}
|
||||
}
|
||||
|
@ -15,4 +15,6 @@ interface DomainRepositoryInterface extends ObjectRepository, EntitySpecificatio
|
||||
* @return Domain[]
|
||||
*/
|
||||
public function findDomainsWithout(?string $excludedAuthority, ?ApiKey $apiKey = null): array;
|
||||
|
||||
public function findOneByAuthority(string $authority, ?ApiKey $apiKey = null): ?Domain;
|
||||
}
|
||||
|
22
module/Core/src/Domain/Spec/IsDomain.php
Normal file
22
module/Core/src/Domain/Spec/IsDomain.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Domain\Spec;
|
||||
|
||||
use Happyr\DoctrineSpecification\Filter\Filter;
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
use Happyr\DoctrineSpecification\Specification\BaseSpecification;
|
||||
|
||||
class IsDomain extends BaseSpecification
|
||||
{
|
||||
public function __construct(private string $domainId, ?string $context = null)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
{
|
||||
return Spec::eq('id', $this->domainId);
|
||||
}
|
||||
}
|
22
module/Core/src/Domain/Spec/IsNotAuthority.php
Normal file
22
module/Core/src/Domain/Spec/IsNotAuthority.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Domain\Spec;
|
||||
|
||||
use Happyr\DoctrineSpecification\Filter\Filter;
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
use Happyr\DoctrineSpecification\Specification\BaseSpecification;
|
||||
|
||||
class IsNotAuthority extends BaseSpecification
|
||||
{
|
||||
public function __construct(private string $authority, ?string $context = null)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
{
|
||||
return Spec::not(Spec::eq('authority', $this->authority));
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Domain\Validation;
|
||||
|
||||
use Laminas\InputFilter\InputFilter;
|
||||
use Shlinkio\Shlink\Common\Validation;
|
||||
|
||||
class DomainRedirectsInputFilter extends InputFilter
|
||||
{
|
||||
use Validation\InputFactoryTrait;
|
||||
|
||||
public const DOMAIN = 'domain';
|
||||
public const BASE_URL_REDIRECT = 'baseUrlRedirect';
|
||||
public const REGULAR_404_REDIRECT = 'regular404Redirect';
|
||||
public const INVALID_SHORT_URL_REDIRECT = 'invalidShortUrlRedirect';
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function withData(array $data): self
|
||||
{
|
||||
$instance = new self();
|
||||
|
||||
$instance->initializeInputs();
|
||||
$instance->setData($data);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
private function initializeInputs(): void
|
||||
{
|
||||
$domain = $this->createInput(self::DOMAIN);
|
||||
$domain->getValidatorChain()->attach(new Validation\HostAndPortValidator());
|
||||
$this->add($domain);
|
||||
|
||||
$this->add($this->createInput(self::BASE_URL_REDIRECT, false));
|
||||
$this->add($this->createInput(self::REGULAR_404_REDIRECT, false));
|
||||
$this->add($this->createInput(self::INVALID_SHORT_URL_REDIRECT, false));
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ class DeleteShortUrlException extends DomainException implements ProblemDetailsE
|
||||
use CommonProblemDetailsExceptionTrait;
|
||||
|
||||
private const TITLE = 'Cannot delete short URL';
|
||||
private const TYPE = 'INVALID_SHORTCODE_DELETION'; // FIXME Should be INVALID_SHORT_URL_DELETION
|
||||
private const TYPE = 'INVALID_SHORTCODE_DELETION'; // FIXME Deprecated: Should be INVALID_SHORT_URL_DELETION
|
||||
|
||||
public static function fromVisitsThreshold(int $threshold, string $shortCode): self
|
||||
{
|
||||
|
@ -17,16 +17,27 @@ class DomainNotFoundException extends DomainException implements ProblemDetailsE
|
||||
private const TITLE = 'Domain not found';
|
||||
private const TYPE = 'DOMAIN_NOT_FOUND';
|
||||
|
||||
private function __construct(string $message, array $additional)
|
||||
{
|
||||
parent::__construct($message);
|
||||
|
||||
$this->detail = $message;
|
||||
$this->title = self::TITLE;
|
||||
$this->type = self::TYPE;
|
||||
$this->status = StatusCodeInterface::STATUS_NOT_FOUND;
|
||||
$this->additional = $additional;
|
||||
}
|
||||
|
||||
public static function fromId(string $id): self
|
||||
{
|
||||
$e = new self(sprintf('Domain with id "%s" could not be found', $id));
|
||||
return new self(sprintf('Domain with id "%s" could not be found', $id), ['id' => $id]);
|
||||
}
|
||||
|
||||
$e->detail = $e->getMessage();
|
||||
$e->title = self::TITLE;
|
||||
$e->type = self::TYPE;
|
||||
$e->status = StatusCodeInterface::STATUS_NOT_FOUND;
|
||||
$e->additional = ['id' => $id];
|
||||
|
||||
return $e;
|
||||
public static function fromAuthority(string $authority): self
|
||||
{
|
||||
return new self(
|
||||
sprintf('Domain with authority "%s" could not be found', $authority),
|
||||
['authority' => $authority],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
33
module/Core/src/Exception/InvalidDomainException.php
Normal file
33
module/Core/src/Exception/InvalidDomainException.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Exception;
|
||||
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
||||
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
|
||||
class InvalidDomainException extends DomainException implements ProblemDetailsExceptionInterface
|
||||
{
|
||||
use CommonProblemDetailsExceptionTrait;
|
||||
|
||||
private const TITLE = 'Invalid domain';
|
||||
private const TYPE = 'INVALID_DOMAIN';
|
||||
|
||||
private function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
public static function forDefaultDomainRedirects(): self
|
||||
{
|
||||
$e = new self('You cannot configure default domain\'s redirects this way. Use the configuration or env vars.');
|
||||
$e->detail = $e->getMessage();
|
||||
$e->title = self::TITLE;
|
||||
$e->type = self::TYPE;
|
||||
$e->status = StatusCodeInterface::STATUS_FORBIDDEN;
|
||||
|
||||
return $e;
|
||||
}
|
||||
}
|
@ -11,13 +11,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class BelongsToApiKey extends BaseSpecification
|
||||
{
|
||||
public function __construct(private ApiKey $apiKey, private ?string $dqlAlias = null)
|
||||
public function __construct(private ApiKey $apiKey, ?string $context = null)
|
||||
{
|
||||
parent::__construct();
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
{
|
||||
return Spec::eq('authorApiKey', $this->apiKey, $this->dqlAlias);
|
||||
return Spec::eq('authorApiKey', $this->apiKey);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ class BelongsToDomainInlined implements Filter
|
||||
{
|
||||
}
|
||||
|
||||
public function getFilter(QueryBuilder $qb, string $dqlAlias): string
|
||||
public function getFilter(QueryBuilder $qb, string $context): string
|
||||
{
|
||||
// Parameters in this query need to be inlined, not bound, as we need to use it as sub-query later
|
||||
return (string) $qb->expr()->eq('s.domain', '\'' . $this->domainId . '\'');
|
||||
|
@ -75,7 +75,7 @@ class ShortUrlInputFilter extends InputFilter
|
||||
$customSlug = $this->createInput(self::CUSTOM_SLUG, false)->setContinueIfEmpty(true);
|
||||
$customSlug->getFilterChain()->attach(new Validation\SluggerFilter(new CocurSymfonySluggerBridge(new Slugify([
|
||||
'regexp' => CUSTOM_SLUGS_REGEXP,
|
||||
'lowercase' => false, // We want to keep it case sensitive
|
||||
'lowercase' => false, // We want to keep it case-sensitive
|
||||
'rulesets' => ['default'],
|
||||
]))));
|
||||
$customSlug->getValidatorChain()->attach(new Validator\NotEmpty([
|
||||
|
@ -27,7 +27,7 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function findDomainsReturnsExpectedResult(): void
|
||||
public function expectedDomainsAreFoundWhenNoApiKeyIsInvolved(): void
|
||||
{
|
||||
$fooDomain = Domain::withAuthority('foo.com');
|
||||
$this->getEntityManager()->persist($fooDomain);
|
||||
@ -45,7 +45,7 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
$this->getEntityManager()->persist($detachedDomain);
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
$detachedWithRedirects->configureNotFoundRedirects(NotFoundRedirects::withRedirects('foo.com', 'bar.com'));
|
||||
$this->getEntityManager()->persist($detachedWithRedirects);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
@ -70,10 +70,15 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
[$barDomain, $bazDomain, $fooDomain],
|
||||
$this->repo->findDomainsWithout('detached-with-redirects.com'),
|
||||
);
|
||||
|
||||
self::assertEquals($barDomain, $this->repo->findOneByAuthority('bar.com'));
|
||||
self::assertEquals($detachedWithRedirects, $this->repo->findOneByAuthority('detached-with-redirects.com'));
|
||||
self::assertNull($this->repo->findOneByAuthority('does-not-exist.com'));
|
||||
self::assertEquals($detachedDomain, $this->repo->findOneByAuthority('detached.com'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function findDomainsReturnsJustThoseMatchingProvidedApiKey(): void
|
||||
public function expectedDomainsAreFoundWhenApiKeyIsProvided(): void
|
||||
{
|
||||
$authorApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()));
|
||||
$this->getEntityManager()->persist($authorApiKey);
|
||||
@ -92,12 +97,12 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
$this->getEntityManager()->persist($bazDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($bazDomain, $authorApiKey));
|
||||
|
||||
// $detachedDomain = Domain::withAuthority('detached.com');
|
||||
// $this->getEntityManager()->persist($detachedDomain);
|
||||
//
|
||||
// $detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
// $detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
// $this->getEntityManager()->persist($detachedWithRedirects);
|
||||
$detachedDomain = Domain::withAuthority('detached.com');
|
||||
$this->getEntityManager()->persist($detachedDomain);
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(NotFoundRedirects::withRedirects('foo.com', 'bar.com'));
|
||||
$this->getEntityManager()->persist($detachedWithRedirects);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
@ -109,21 +114,30 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
$barDomainApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($barDomain)));
|
||||
$this->getEntityManager()->persist($barDomainApiKey);
|
||||
|
||||
// $detachedWithRedirectsApiKey = ApiKey::fromMeta(
|
||||
// ApiKeyMeta::withRoles(RoleDefinition::forDomain($detachedWithRedirects)),
|
||||
// );
|
||||
// $this->getEntityManager()->persist($detachedWithRedirectsApiKey);
|
||||
$detachedWithRedirectsApiKey = ApiKey::fromMeta(
|
||||
ApiKeyMeta::withRoles(RoleDefinition::forDomain($detachedWithRedirects)),
|
||||
);
|
||||
$this->getEntityManager()->persist($detachedWithRedirectsApiKey);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
self::assertEquals([$fooDomain], $this->repo->findDomainsWithout(null, $fooDomainApiKey));
|
||||
self::assertEquals([$barDomain], $this->repo->findDomainsWithout(null, $barDomainApiKey));
|
||||
// self::assertEquals(
|
||||
// [$detachedWithRedirects],
|
||||
// $this->repo->findDomainsWithout(null, $detachedWithRedirectsApiKey),
|
||||
// );
|
||||
self::assertEquals(
|
||||
[$detachedWithRedirects],
|
||||
$this->repo->findDomainsWithout(null, $detachedWithRedirectsApiKey),
|
||||
);
|
||||
self::assertEquals([$bazDomain, $fooDomain], $this->repo->findDomainsWithout(null, $authorApiKey));
|
||||
self::assertEquals([], $this->repo->findDomainsWithout(null, $authorAndDomainApiKey));
|
||||
|
||||
self::assertEquals($fooDomain, $this->repo->findOneByAuthority('foo.com', $authorApiKey));
|
||||
self::assertNull($this->repo->findOneByAuthority('bar.com', $authorApiKey));
|
||||
self::assertEquals($barDomain, $this->repo->findOneByAuthority('bar.com', $barDomainApiKey));
|
||||
self::assertEquals(
|
||||
$detachedWithRedirects,
|
||||
$this->repo->findOneByAuthority('detached-with-redirects.com', $detachedWithRedirectsApiKey),
|
||||
);
|
||||
self::assertNull($this->repo->findOneByAuthority('foo.com', $detachedWithRedirectsApiKey));
|
||||
}
|
||||
|
||||
private function createShortUrl(Domain $domain, ?ApiKey $apiKey = null): ShortUrl
|
||||
|
@ -15,6 +15,7 @@ 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;
|
||||
@ -133,16 +134,16 @@ class DomainServiceTest extends TestCase
|
||||
* @test
|
||||
* @dataProvider provideFoundDomains
|
||||
*/
|
||||
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain): void
|
||||
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
||||
{
|
||||
$authority = 'example.com';
|
||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
||||
$repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
|
||||
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain);
|
||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
||||
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
|
||||
$flush = $this->em->flush();
|
||||
|
||||
$result = $this->domainService->getOrCreate($authority);
|
||||
$result = $this->domainService->getOrCreate($authority, $apiKey);
|
||||
|
||||
if ($foundDomain !== null) {
|
||||
self::assertSame($result, $foundDomain);
|
||||
@ -152,24 +153,42 @@ class DomainServiceTest extends TestCase
|
||||
$flush->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function getOrCreateThrowsExceptionForApiKeysWithDomainRole(): void
|
||||
{
|
||||
$authority = 'example.com';
|
||||
$domain = Domain::withAuthority($authority)->setId('1');
|
||||
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
||||
$repo->findOneByAuthority($authority, $apiKey)->willReturn(null);
|
||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->expectException(DomainNotFoundException::class);
|
||||
$getRepo->shouldBeCalledOnce();
|
||||
$this->em->persist(Argument::cetera())->shouldNotBeCalled();
|
||||
$this->em->flush()->shouldNotBeCalled();
|
||||
|
||||
$this->domainService->getOrCreate($authority, $apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideFoundDomains
|
||||
*/
|
||||
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain): void
|
||||
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
||||
{
|
||||
$authority = 'example.com';
|
||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
||||
$repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
|
||||
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain);
|
||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
||||
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
|
||||
$flush = $this->em->flush();
|
||||
|
||||
$result = $this->domainService->configureNotFoundRedirects($authority, new NotFoundRedirects(
|
||||
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
|
||||
'foo.com',
|
||||
'bar.com',
|
||||
'baz.com',
|
||||
));
|
||||
), $apiKey);
|
||||
|
||||
if ($foundDomain !== null) {
|
||||
self::assertSame($result, $foundDomain);
|
||||
@ -179,12 +198,31 @@ class DomainServiceTest extends TestCase
|
||||
self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
|
||||
$getRepo->shouldHaveBeenCalledOnce();
|
||||
$persist->shouldHaveBeenCalledOnce();
|
||||
$flush->shouldHaveBeenCalledTimes(2);
|
||||
$flush->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideFoundDomains(): iterable
|
||||
{
|
||||
yield 'domain not found' => [null];
|
||||
yield 'domain found' => [Domain::withAuthority('')];
|
||||
$domain = Domain::withAuthority('');
|
||||
$adminApiKey = ApiKey::create();
|
||||
$authorApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()));
|
||||
|
||||
yield 'domain not found and no API key' => [null, null];
|
||||
yield 'domain found and no API key' => [$domain, null];
|
||||
yield 'domain not found and admin API key' => [null, $adminApiKey];
|
||||
yield 'domain found and admin API key' => [$domain, $adminApiKey];
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ use function sprintf;
|
||||
class DomainNotFoundExceptionTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function properlyCreatesExceptionFromNotFoundTag(): void
|
||||
public function properlyCreatesExceptionFromId(): void
|
||||
{
|
||||
$id = '123';
|
||||
$expectedMessage = sprintf('Domain with id "%s" could not be found', $id);
|
||||
@ -25,4 +25,19 @@ class DomainNotFoundExceptionTest extends TestCase
|
||||
self::assertEquals(['id' => $id], $e->getAdditionalData());
|
||||
self::assertEquals(404, $e->getStatus());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function properlyCreatesExceptionFromAuthority(): void
|
||||
{
|
||||
$authority = 'example.com';
|
||||
$expectedMessage = sprintf('Domain with authority "%s" could not be found', $authority);
|
||||
$e = DomainNotFoundException::fromAuthority($authority);
|
||||
|
||||
self::assertEquals($expectedMessage, $e->getMessage());
|
||||
self::assertEquals($expectedMessage, $e->getDetail());
|
||||
self::assertEquals('Domain not found', $e->getTitle());
|
||||
self::assertEquals('DOMAIN_NOT_FOUND', $e->getType());
|
||||
self::assertEquals(['authority' => $authority], $e->getAdditionalData());
|
||||
self::assertEquals(404, $e->getStatus());
|
||||
}
|
||||
}
|
||||
|
24
module/Core/test/Exception/InvalidDomainExceptionTest.php
Normal file
24
module/Core/test/Exception/InvalidDomainExceptionTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Exception;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidDomainException;
|
||||
|
||||
class InvalidDomainExceptionTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function configuresTheExceptionAsExpected(): void
|
||||
{
|
||||
$e = InvalidDomainException::forDefaultDomainRedirects();
|
||||
$expected = 'You cannot configure default domain\'s redirects this way. Use the configuration or env vars.';
|
||||
|
||||
self::assertEquals($expected, $e->getMessage());
|
||||
self::assertEquals($expected, $e->getDetail());
|
||||
self::assertEquals('Invalid domain', $e->getTitle());
|
||||
self::assertEquals('INVALID_DOMAIN', $e->getType());
|
||||
self::assertEquals(403, $e->getStatus());
|
||||
}
|
||||
}
|
@ -40,6 +40,7 @@ return [
|
||||
Action\Tag\CreateTagsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Tag\UpdateTagAction::class => ConfigAbstractFactory::class,
|
||||
Action\Domain\ListDomainsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Domain\DomainRedirectsAction::class => ConfigAbstractFactory::class,
|
||||
|
||||
ImplicitOptionsMiddleware::class => Middleware\EmptyResponseImplicitOptionsMiddlewareFactory::class,
|
||||
Middleware\BodyParserMiddleware::class => InvokableFactory::class,
|
||||
@ -81,6 +82,7 @@ return [
|
||||
Action\Tag\CreateTagsAction::class => [TagService::class],
|
||||
Action\Tag\UpdateTagAction::class => [TagService::class],
|
||||
Action\Domain\ListDomainsAction::class => [DomainService::class],
|
||||
Action\Domain\DomainRedirectsAction::class => [DomainService::class],
|
||||
|
||||
Middleware\CrossDomainMiddleware::class => ['config.cors'],
|
||||
Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ['config.url_shortener.domain.hostname'],
|
||||
|
@ -44,6 +44,7 @@ return [
|
||||
|
||||
// Domains
|
||||
Action\Domain\ListDomainsAction::getRouteDef(),
|
||||
Action\Domain\DomainRedirectsAction::getRouteDef(),
|
||||
|
||||
Action\MercureInfoAction::getRouteDef(),
|
||||
],
|
||||
|
39
module/Rest/src/Action/Domain/DomainRedirectsAction.php
Normal file
39
module/Rest/src/Action/Domain/DomainRedirectsAction.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Action\Domain;
|
||||
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||
use Shlinkio\Shlink\Rest\Action\Domain\Request\DomainRedirectsRequest;
|
||||
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
||||
|
||||
class DomainRedirectsAction extends AbstractRestAction
|
||||
{
|
||||
protected const ROUTE_PATH = '/domains/redirects';
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH];
|
||||
|
||||
public function __construct(private DomainServiceInterface $domainService)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
/** @var array $body */
|
||||
$body = $request->getParsedBody();
|
||||
$requestData = DomainRedirectsRequest::fromRawData($body);
|
||||
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
|
||||
|
||||
$authority = $requestData->authority();
|
||||
$domain = $this->domainService->getOrCreate($authority);
|
||||
$notFoundRedirects = $requestData->toNotFoundRedirects($domain);
|
||||
|
||||
$this->domainService->configureNotFoundRedirects($authority, $notFoundRedirects, $apiKey);
|
||||
|
||||
return new JsonResponse($notFoundRedirects);
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Action\Domain\Request;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\Validation\DomainRedirectsInputFilter;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
class DomainRedirectsRequest
|
||||
{
|
||||
private string $authority;
|
||||
private ?string $baseUrlRedirect = null;
|
||||
private bool $baseUrlRedirectWasProvided = false;
|
||||
private ?string $regular404Redirect = null;
|
||||
private bool $regular404RedirectWasProvided = false;
|
||||
private ?string $invalidShortUrlRedirect = null;
|
||||
private bool $invalidShortUrlRedirectWasProvided = false;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromRawData(array $payload): self
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->validateAndInit($payload);
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateAndInit(array $payload): void
|
||||
{
|
||||
$inputFilter = DomainRedirectsInputFilter::withData($payload);
|
||||
if (! $inputFilter->isValid()) {
|
||||
throw ValidationException::fromInputFilter($inputFilter);
|
||||
}
|
||||
|
||||
$this->baseUrlRedirectWasProvided = array_key_exists(
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT,
|
||||
$payload,
|
||||
);
|
||||
$this->regular404RedirectWasProvided = array_key_exists(
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT,
|
||||
$payload,
|
||||
);
|
||||
$this->invalidShortUrlRedirectWasProvided = array_key_exists(
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT,
|
||||
$payload,
|
||||
);
|
||||
|
||||
$this->authority = $inputFilter->getValue(DomainRedirectsInputFilter::DOMAIN);
|
||||
$this->baseUrlRedirect = $inputFilter->getValue(DomainRedirectsInputFilter::BASE_URL_REDIRECT);
|
||||
$this->regular404Redirect = $inputFilter->getValue(DomainRedirectsInputFilter::REGULAR_404_REDIRECT);
|
||||
$this->invalidShortUrlRedirect = $inputFilter->getValue(DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT);
|
||||
}
|
||||
|
||||
public function authority(): string
|
||||
{
|
||||
return $this->authority;
|
||||
}
|
||||
|
||||
public function toNotFoundRedirects(?NotFoundRedirectConfigInterface $defaults = null): NotFoundRedirects
|
||||
{
|
||||
return NotFoundRedirects::withRedirects(
|
||||
$this->baseUrlRedirectWasProvided ? $this->baseUrlRedirect : $defaults?->baseUrlRedirect(),
|
||||
$this->regular404RedirectWasProvided ? $this->regular404Redirect : $defaults?->regular404Redirect(),
|
||||
$this->invalidShortUrlRedirectWasProvided
|
||||
? $this->invalidShortUrlRedirect
|
||||
: $defaults?->invalidShortUrlRedirect(),
|
||||
);
|
||||
}
|
||||
}
|
@ -119,6 +119,11 @@ class ApiKey extends AbstractEntity
|
||||
return $role?->meta() ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param callable(string $roleName, array $meta): T $fun
|
||||
* @return T[]
|
||||
*/
|
||||
public function mapRoles(callable $fun): array
|
||||
{
|
||||
return $this->roles->map(fn (ApiKeyRole $role) => $fun($role->name(), $role->meta()))->getValues();
|
||||
|
100
module/Rest/test-api/Action/DomainRedirectsTest.php
Normal file
100
module/Rest/test-api/Action/DomainRedirectsTest.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||
|
||||
class DomainRedirectsTest extends ApiTestCase
|
||||
{
|
||||
/** @test */
|
||||
public function anErrorIsReturnedWhenTryingToEditDefaultDomain(): void
|
||||
{
|
||||
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/domains/redirects', [
|
||||
RequestOptions::JSON => ['domain' => 'doma.in'],
|
||||
]);
|
||||
$payload = $this->getJsonResponsePayload($resp);
|
||||
|
||||
self::assertEquals(self::STATUS_FORBIDDEN, $resp->getStatusCode());
|
||||
self::assertEquals(self::STATUS_FORBIDDEN, $payload['status']);
|
||||
self::assertEquals('INVALID_DOMAIN', $payload['type']);
|
||||
self::assertEquals(
|
||||
'You cannot configure default domain\'s redirects this way. Use the configuration or env vars.',
|
||||
$payload['detail'],
|
||||
);
|
||||
self::assertEquals('Invalid domain', $payload['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideInvalidDomains
|
||||
*/
|
||||
public function anErrorIsReturnedWhenTryingToEditAnInvalidDomain(array $request): void
|
||||
{
|
||||
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/domains/redirects', [
|
||||
RequestOptions::JSON => $request,
|
||||
]);
|
||||
$payload = $this->getJsonResponsePayload($resp);
|
||||
|
||||
self::assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode());
|
||||
self::assertEquals(self::STATUS_BAD_REQUEST, $payload['status']);
|
||||
self::assertEquals('INVALID_ARGUMENT', $payload['type']);
|
||||
self::assertEquals('Provided data is not valid', $payload['detail']);
|
||||
self::assertEquals('Invalid data', $payload['title']);
|
||||
}
|
||||
|
||||
public function provideInvalidDomains(): iterable
|
||||
{
|
||||
yield 'no domain' => [[]];
|
||||
yield 'empty domain' => [['domain' => '']];
|
||||
yield 'null domain' => [['domain' => null]];
|
||||
yield 'invalid domain' => [['domain' => '192.168.1.1']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideRequests
|
||||
*/
|
||||
public function allowsToEditDomainRedirects(array $request, array $expectedResponse): void
|
||||
{
|
||||
$resp = $this->callApiWithKey(self::METHOD_PATCH, '/domains/redirects', [
|
||||
RequestOptions::JSON => $request,
|
||||
]);
|
||||
$payload = $this->getJsonResponsePayload($resp);
|
||||
|
||||
self::assertEquals(self::STATUS_OK, $resp->getStatusCode());
|
||||
self::assertEquals($expectedResponse, $payload);
|
||||
}
|
||||
|
||||
public function provideRequests(): iterable
|
||||
{
|
||||
yield 'new domain' => [[
|
||||
'domain' => 'my-new-domain.com',
|
||||
'regular404Redirect' => 'foo.com',
|
||||
], [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => 'foo.com',
|
||||
'invalidShortUrlRedirect' => null,
|
||||
]];
|
||||
yield 'existing domain with redirects' => [[
|
||||
'domain' => 'detached-with-redirects.com',
|
||||
'baseUrlRedirect' => null,
|
||||
'invalidShortUrlRedirect' => 'foo.com',
|
||||
], [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => 'bar.com',
|
||||
'invalidShortUrlRedirect' => 'foo.com',
|
||||
]];
|
||||
yield 'existing domain with no redirects' => [[
|
||||
'domain' => 'example.com',
|
||||
'baseUrlRedirect' => null,
|
||||
'invalidShortUrlRedirect' => 'foo.com',
|
||||
], [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => null,
|
||||
'invalidShortUrlRedirect' => 'foo.com',
|
||||
]];
|
||||
}
|
||||
}
|
@ -31,30 +31,60 @@ class ListDomainsTest extends ApiTestCase
|
||||
[
|
||||
'domain' => 'doma.in',
|
||||
'isDefault' => true,
|
||||
'redirects' => [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => null,
|
||||
'invalidShortUrlRedirect' => null,
|
||||
],
|
||||
],
|
||||
[
|
||||
'domain' => 'detached-with-redirects.com',
|
||||
'isDefault' => false,
|
||||
'redirects' => [
|
||||
'baseUrlRedirect' => 'foo.com',
|
||||
'regular404Redirect' => 'bar.com',
|
||||
'invalidShortUrlRedirect' => null,
|
||||
],
|
||||
],
|
||||
[
|
||||
'domain' => 'example.com',
|
||||
'isDefault' => false,
|
||||
'redirects' => [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => null,
|
||||
'invalidShortUrlRedirect' => null,
|
||||
],
|
||||
],
|
||||
[
|
||||
'domain' => 'some-domain.com',
|
||||
'isDefault' => false,
|
||||
'redirects' => [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => null,
|
||||
'invalidShortUrlRedirect' => null,
|
||||
],
|
||||
],
|
||||
]];
|
||||
yield 'author API key' => ['author_api_key', [
|
||||
[
|
||||
'domain' => 'doma.in',
|
||||
'isDefault' => true,
|
||||
'redirects' => [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => null,
|
||||
'invalidShortUrlRedirect' => null,
|
||||
],
|
||||
],
|
||||
]];
|
||||
yield 'domain API key' => ['domain_api_key', [
|
||||
[
|
||||
'domain' => 'example.com',
|
||||
'isDefault' => false,
|
||||
'redirects' => [
|
||||
'baseUrlRedirect' => null,
|
||||
'regular404Redirect' => null,
|
||||
'invalidShortUrlRedirect' => null,
|
||||
],
|
||||
],
|
||||
]];
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ class DomainFixture extends AbstractFixture
|
||||
$manager->persist(Domain::withAuthority('this_domain_is_detached.com'));
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
$detachedWithRedirects->configureNotFoundRedirects(NotFoundRedirects::withRedirects('foo.com', 'bar.com'));
|
||||
$manager->persist($detachedWithRedirects);
|
||||
|
||||
$manager->flush();
|
||||
|
160
module/Rest/test/Action/Domain/DomainRedirectsActionTest.php
Normal file
160
module/Rest/test/Action/Domain/DomainRedirectsActionTest.php
Normal file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Action\Domain;
|
||||
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Validation\DomainRedirectsInputFilter;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Rest\Action\Domain\DomainRedirectsAction;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
class DomainRedirectsActionTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private DomainRedirectsAction $action;
|
||||
private ObjectProphecy $domainService;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->domainService = $this->prophesize(DomainServiceInterface::class);
|
||||
$this->action = new DomainRedirectsAction($this->domainService->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideInvalidBodies
|
||||
*/
|
||||
public function invalidDataThrowsException(array $body): void
|
||||
{
|
||||
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
$this->domainService->getOrCreate(Argument::cetera())->shouldNotBeCalled();
|
||||
$this->domainService->configureNotFoundRedirects(Argument::cetera())->shouldNotBeCalled();
|
||||
|
||||
$this->action->handle($request);
|
||||
}
|
||||
|
||||
public function provideInvalidBodies(): iterable
|
||||
{
|
||||
yield 'no domain' => [[]];
|
||||
yield 'empty domain' => [['domain' => '']];
|
||||
yield 'invalid domain' => [['domain' => '192.168.1.20']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideDomainsAndRedirects
|
||||
*/
|
||||
public function domainIsFetchedAndUsedToGetItConfigured(
|
||||
Domain $domain,
|
||||
array $redirects,
|
||||
array $expectedResult,
|
||||
): void {
|
||||
$authority = 'doma.in';
|
||||
$redirects['domain'] = $authority;
|
||||
$apiKey = ApiKey::create();
|
||||
$request = ServerRequestFactory::fromGlobals()->withParsedBody($redirects)
|
||||
->withAttribute(ApiKey::class, $apiKey);
|
||||
|
||||
$getOrCreate = $this->domainService->getOrCreate($authority)->willReturn($domain);
|
||||
$configureNotFoundRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$authority,
|
||||
NotFoundRedirects::withRedirects(
|
||||
array_key_exists(DomainRedirectsInputFilter::BASE_URL_REDIRECT, $redirects)
|
||||
? $redirects[DomainRedirectsInputFilter::BASE_URL_REDIRECT]
|
||||
: $domain?->baseUrlRedirect(),
|
||||
array_key_exists(DomainRedirectsInputFilter::REGULAR_404_REDIRECT, $redirects)
|
||||
? $redirects[DomainRedirectsInputFilter::REGULAR_404_REDIRECT]
|
||||
: $domain?->regular404Redirect(),
|
||||
array_key_exists(DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT, $redirects)
|
||||
? $redirects[DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT]
|
||||
: $domain?->invalidShortUrlRedirect(),
|
||||
),
|
||||
$apiKey,
|
||||
);
|
||||
|
||||
/** @var JsonResponse $response */
|
||||
$response = $this->action->handle($request);
|
||||
/** @var NotFoundRedirects $payload */
|
||||
$payload = $response->getPayload();
|
||||
|
||||
self::assertEquals($expectedResult, $payload->jsonSerialize());
|
||||
$getOrCreate->shouldHaveBeenCalledOnce();
|
||||
$configureNotFoundRedirects->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideDomainsAndRedirects(): iterable
|
||||
{
|
||||
yield 'full overwrite' => [Domain::withAuthority(''), [
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => 'foo',
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT => 'bar',
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => 'baz',
|
||||
], [
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => 'foo',
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT => 'bar',
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => 'baz',
|
||||
]];
|
||||
yield 'partial overwrite' => [Domain::withAuthority(''), [
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => 'foo',
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => 'baz',
|
||||
], [
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => 'foo',
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT => null,
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => 'baz',
|
||||
]];
|
||||
yield 'no override' => [
|
||||
(static function (): Domain {
|
||||
$domain = Domain::withAuthority('');
|
||||
$domain->configureNotFoundRedirects(NotFoundRedirects::withRedirects(
|
||||
'baz',
|
||||
'bar',
|
||||
'foo',
|
||||
));
|
||||
|
||||
return $domain;
|
||||
})(),
|
||||
[],
|
||||
[
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => 'baz',
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT => 'bar',
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => 'foo',
|
||||
],
|
||||
];
|
||||
yield 'reset' => [
|
||||
(static function (): Domain {
|
||||
$domain = Domain::withAuthority('');
|
||||
$domain->configureNotFoundRedirects(NotFoundRedirects::withRedirects(
|
||||
'foo',
|
||||
'bar',
|
||||
'baz',
|
||||
));
|
||||
|
||||
return $domain;
|
||||
})(),
|
||||
[
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => null,
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT => null,
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => null,
|
||||
],
|
||||
[
|
||||
DomainRedirectsInputFilter::BASE_URL_REDIRECT => null,
|
||||
DomainRedirectsInputFilter::REGULAR_404_REDIRECT => null,
|
||||
DomainRedirectsInputFilter::INVALID_SHORT_URL_REDIRECT => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user