mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Added a system to set roles to API keys
This commit is contained in:
@@ -8,6 +8,7 @@ use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKeyRole;
|
||||
|
||||
use function Shlinkio\Shlink\Core\determineTableName;
|
||||
|
||||
@@ -34,4 +35,8 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
|
||||
|
||||
$builder->createField('enabled', Types::BOOLEAN)
|
||||
->build();
|
||||
|
||||
$builder->createOneToMany('roles', ApiKeyRole::class)
|
||||
->mappedBy('apiKey')
|
||||
->build();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
use function Shlinkio\Shlink\Core\determineTableName;
|
||||
|
||||
return static function (ClassMetadata $metadata, array $emConfig): void {
|
||||
$builder = new ClassMetadataBuilder($metadata);
|
||||
|
||||
$builder->setTable(determineTableName('api_key_roles', $emConfig));
|
||||
|
||||
$builder->createField('id', Types::BIGINT)
|
||||
->makePrimaryKey()
|
||||
->generatedValue('IDENTITY')
|
||||
->option('unsigned', true)
|
||||
->build();
|
||||
|
||||
$builder->createField('roleName', Types::STRING)
|
||||
->columnName('role_name')
|
||||
->length(256)
|
||||
->nullable(false)
|
||||
->build();
|
||||
|
||||
$builder->createField('meta', Types::JSON)
|
||||
->columnName('meta')
|
||||
->nullable(false)
|
||||
->build();
|
||||
|
||||
$builder->createManyToOne('apiKey', ApiKey::class)
|
||||
->addJoinColumn('api_key_id', 'id', false, false, 'CASCADE')
|
||||
->cascadePersist()
|
||||
->build();
|
||||
|
||||
$builder->addUniqueConstraint(['role_name', 'api_key_id'], 'UQ_role_plus_api_key');
|
||||
};
|
||||
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace Shlinkio\Shlink\Rest\Entity;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Happyr\DoctrineSpecification\Spec;
|
||||
use Happyr\DoctrineSpecification\Specification\Specification;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
@@ -15,12 +17,15 @@ class ApiKey extends AbstractEntity
|
||||
private string $key;
|
||||
private ?Chronos $expirationDate = null;
|
||||
private bool $enabled;
|
||||
/** @var Collection|ApiKeyRole[] */
|
||||
private Collection $roles;
|
||||
|
||||
public function __construct(?Chronos $expirationDate = null)
|
||||
{
|
||||
$this->key = Uuid::uuid4()->toString();
|
||||
$this->expirationDate = $expirationDate;
|
||||
$this->enabled = true;
|
||||
$this->roles = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getExpirationDate(): ?Chronos
|
||||
@@ -62,8 +67,6 @@ class ApiKey extends AbstractEntity
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function spec(): Specification
|
||||
{
|
||||
return Spec::andX();
|
||||
|
||||
31
module/Rest/src/Entity/ApiKeyRole.php
Normal file
31
module/Rest/src/Entity/ApiKeyRole.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Entity;
|
||||
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
|
||||
class ApiKeyRole extends AbstractEntity
|
||||
{
|
||||
private string $roleName;
|
||||
private array $meta;
|
||||
private ApiKey $apiKey;
|
||||
|
||||
public function __construct(string $roleName, array $meta, ApiKey $apiKey)
|
||||
{
|
||||
$this->roleName = $roleName;
|
||||
$this->meta = $meta;
|
||||
$this->apiKey = $apiKey;
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->roleName;
|
||||
}
|
||||
|
||||
public function meta(): array
|
||||
{
|
||||
return $this->meta;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user