mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Created specs for API key roles
This commit is contained in:
parent
7e6882960e
commit
df53e6c6f2
5 changed files with 93 additions and 1 deletions
28
module/Core/src/ShortUrl/Spec/BelongsToApiKey.php
Normal file
28
module/Core/src/ShortUrl/Spec/BelongsToApiKey.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Spec;
|
||||
|
||||
use Happyr\DoctrineSpecification\BaseSpecification;
|
||||
use Happyr\DoctrineSpecification\Filter\Filter;
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class BelongsToApiKey extends BaseSpecification
|
||||
{
|
||||
private ApiKey $apiKey;
|
||||
private string $dqlAlias;
|
||||
|
||||
public function __construct(ApiKey $apiKey, ?string $dqlAlias = null)
|
||||
{
|
||||
$this->dqlAlias = $dqlAlias ?? 's';
|
||||
$this->apiKey = $apiKey;
|
||||
parent::__construct($this->dqlAlias);
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
{
|
||||
return Spec::eq('authorApiKey', $this->apiKey, $this->dqlAlias);
|
||||
}
|
||||
}
|
27
module/Core/src/ShortUrl/Spec/BelongsToDomain.php
Normal file
27
module/Core/src/ShortUrl/Spec/BelongsToDomain.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Spec;
|
||||
|
||||
use Happyr\DoctrineSpecification\BaseSpecification;
|
||||
use Happyr\DoctrineSpecification\Filter\Filter;
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
|
||||
class BelongsToDomain extends BaseSpecification
|
||||
{
|
||||
private int $domainId;
|
||||
private string $dqlAlias;
|
||||
|
||||
public function __construct(int $domainId, ?string $dqlAlias = null)
|
||||
{
|
||||
$this->domainId = $domainId;
|
||||
$this->dqlAlias = $dqlAlias ?? 's';
|
||||
parent::__construct($this->dqlAlias);
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
{
|
||||
return Spec::eq('domain', $this->domainId, $this->dqlAlias);
|
||||
}
|
||||
}
|
30
module/Rest/src/ApiKey/Role.php
Normal file
30
module/Rest/src/ApiKey/Role.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\ApiKey;
|
||||
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
use Happyr\DoctrineSpecification\Specification\Specification;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToApiKey;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Spec\BelongsToDomain;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKeyRole;
|
||||
|
||||
class Role
|
||||
{
|
||||
public const AUTHORED_SHORT_URLS = 'AUTHORED_SHORT_URLS';
|
||||
public const DOMAIN_SPECIFIC = 'DOMAIN_SPECIFIC';
|
||||
|
||||
public static function toSpec(ApiKeyRole $role): Specification
|
||||
{
|
||||
if ($role->name() === self::AUTHORED_SHORT_URLS) {
|
||||
return new BelongsToApiKey($role->apiKey());
|
||||
}
|
||||
|
||||
if ($role->name() === self::DOMAIN_SPECIFIC) {
|
||||
return new BelongsToDomain($role->meta()['domain_id'] ?? -1);
|
||||
}
|
||||
|
||||
return Spec::andX();
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ use Happyr\DoctrineSpecification\Spec;
|
|||
use Happyr\DoctrineSpecification\Specification\Specification;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
||||
|
||||
class ApiKey extends AbstractEntity
|
||||
{
|
||||
|
@ -69,6 +70,7 @@ class ApiKey extends AbstractEntity
|
|||
|
||||
public function spec(): Specification
|
||||
{
|
||||
return Spec::andX();
|
||||
$specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role));
|
||||
return Spec::andX(...$specs);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,4 +28,9 @@ class ApiKeyRole extends AbstractEntity
|
|||
{
|
||||
return $this->meta;
|
||||
}
|
||||
|
||||
public function apiKey(): ApiKey
|
||||
{
|
||||
return $this->apiKey;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue