mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Reduce duplicated logic when checking if an API key is admin
This commit is contained in:
@@ -11,14 +11,14 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class WithApiKeySpecsEnsuringJoin extends BaseSpecification
|
||||
{
|
||||
public function __construct(private ?ApiKey $apiKey, private string $fieldToJoin = 'shortUrls')
|
||||
public function __construct(private readonly ?ApiKey $apiKey, private readonly string $fieldToJoin = 'shortUrls')
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function getSpec(): Specification
|
||||
{
|
||||
return $this->apiKey === null || $this->apiKey->isAdmin() ? Spec::andX() : Spec::andX(
|
||||
return $this->apiKey === null || ApiKey::isAdmin($this->apiKey) ? Spec::andX() : Spec::andX(
|
||||
Spec::join($this->fieldToJoin, 's'),
|
||||
$this->apiKey->spec($this->fieldToJoin),
|
||||
);
|
||||
|
||||
@@ -114,9 +114,12 @@ class ApiKey extends AbstractEntity
|
||||
return Spec::andX(...$specs);
|
||||
}
|
||||
|
||||
public function isAdmin(): bool
|
||||
/**
|
||||
* @return ($apiKey is null ? true : boolean)
|
||||
*/
|
||||
public static function isAdmin(?ApiKey $apiKey): bool
|
||||
{
|
||||
return $this->roles->isEmpty();
|
||||
return $apiKey === null || $apiKey->roles->isEmpty();
|
||||
}
|
||||
|
||||
public function hasRole(Role $role): bool
|
||||
|
||||
Reference in New Issue
Block a user