Add ORPHAN_VISITS_EXCLUDED API key role

This commit is contained in:
Alejandro Celaya
2023-05-30 09:12:46 +02:00
parent 112b54ec7d
commit 8b03532ddb
11 changed files with 54 additions and 23 deletions

View File

@@ -44,7 +44,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
$builder->createOneToMany('roles', ApiKeyRole::class)
->mappedBy('apiKey')
->setIndexBy('roleName')
->setIndexBy('role')
->cascadePersist()
->orphanRemoval()
->build();

View File

@@ -25,7 +25,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
->build();
(new FieldBuilder($builder, [
'fieldName' => 'roleName',
'fieldName' => 'role',
'type' => Types::STRING,
'enumType' => Role::class,
]))->columnName('role_name')

View File

@@ -25,4 +25,9 @@ final class RoleDefinition
['domain_id' => $domain->getId(), 'authority' => $domain->authority],
);
}
public static function forOrphanVisitsExcluded(): self
{
return new self(Role::NO_ORPHAN_VISITS, []);
}
}

View File

@@ -12,16 +12,20 @@ use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToDomain;
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToDomainInlined;
use Shlinkio\Shlink\Rest\Entity\ApiKeyRole;
use function sprintf;
enum Role: string
{
case AUTHORED_SHORT_URLS = 'AUTHORED_SHORT_URLS';
case DOMAIN_SPECIFIC = 'DOMAIN_SPECIFIC';
case NO_ORPHAN_VISITS = 'NO_ORPHAN_VISITS';
public function toFriendlyName(): string
public function toFriendlyName(array $meta): string
{
return match ($this) {
self::AUTHORED_SHORT_URLS => 'Author only',
self::DOMAIN_SPECIFIC => 'Domain only',
self::DOMAIN_SPECIFIC => sprintf('Domain only: %s', Role::domainAuthorityFromMeta($meta)),
self::NO_ORPHAN_VISITS => 'No orphan visits',
};
}
@@ -30,6 +34,7 @@ enum Role: string
return match ($this) {
self::AUTHORED_SHORT_URLS => 'author-only',
self::DOMAIN_SPECIFIC => 'domain-only',
self::NO_ORPHAN_VISITS => 'no-orphan-visits',
};
}
@@ -38,6 +43,7 @@ enum Role: string
return match ($role->role()) {
self::AUTHORED_SHORT_URLS => new BelongsToApiKey($role->apiKey(), $context),
self::DOMAIN_SPECIFIC => new BelongsToDomain(self::domainIdFromMeta($role->meta()), $context),
default => Spec::andX(),
};
}
@@ -46,6 +52,7 @@ enum Role: string
return match ($role->role()) {
self::AUTHORED_SHORT_URLS => Spec::andX(new BelongsToApiKeyInlined($role->apiKey())),
self::DOMAIN_SPECIFIC => Spec::andX(new BelongsToDomainInlined(self::domainIdFromMeta($role->meta()))),
default => Spec::andX(),
};
}

View File

@@ -9,13 +9,24 @@ use Shlinkio\Shlink\Rest\ApiKey\Role;
class ApiKeyRole extends AbstractEntity
{
public function __construct(private Role $roleName, private array $meta, private ApiKey $apiKey)
public function __construct(public readonly Role $role, private array $meta, public readonly ApiKey $apiKey)
{
}
/**
* @deprecated Use property access directly
*/
public function role(): Role
{
return $this->roleName;
return $this->role;
}
/**
* @deprecated Use property access directly
*/
public function apiKey(): ApiKey
{
return $this->apiKey;
}
public function meta(): array
@@ -27,9 +38,4 @@ class ApiKeyRole extends AbstractEntity
{
$this->meta = $newMeta;
}
public function apiKey(): ApiKey
{
return $this->apiKey;
}
}

View File

@@ -86,14 +86,15 @@ class RoleTest extends TestCase
}
#[Test, DataProvider('provideRoleNames')]
public function getsExpectedRoleFriendlyName(Role $role, string $expectedFriendlyName): void
public function getsExpectedRoleFriendlyName(Role $role, array $meta, string $expectedFriendlyName): void
{
self::assertEquals($expectedFriendlyName, $role->toFriendlyName());
self::assertEquals($expectedFriendlyName, $role->toFriendlyName($meta));
}
public static function provideRoleNames(): iterable
{
yield Role::AUTHORED_SHORT_URLS->value => [Role::AUTHORED_SHORT_URLS, 'Author only'];
yield Role::DOMAIN_SPECIFIC->value => [Role::DOMAIN_SPECIFIC, 'Domain only'];
yield Role::AUTHORED_SHORT_URLS->value => [Role::AUTHORED_SHORT_URLS, [], 'Author only'];
yield Role::DOMAIN_SPECIFIC->value => [Role::DOMAIN_SPECIFIC, ['authority' => 's.test'], 'Domain only: s.test'];
yield Role::NO_ORPHAN_VISITS->value => [Role::NO_ORPHAN_VISITS, [], 'No orphan visits'];
}
}