refonte
This commit is contained in:
107
src/Entity/ApiToken.php
Normal file
107
src/Entity/ApiToken.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\ApiTokenRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Random\RandomException;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ApiTokenRepository::class)]
|
||||
class ApiToken
|
||||
{
|
||||
|
||||
private const string PERSONAL_ACCESS_TOKEN_PREFIX = 'ipm_';
|
||||
public const string SCOPE_USER_EDIT = 'ROLE_USER_EDIT';
|
||||
public const string SCOPE_PROJECT_CREATE = 'ROLE_PROJECT_CREATE';
|
||||
public const string SCOPE_PROJECT_EDIT = 'ROLE_PROJECT_EDIT';
|
||||
|
||||
public const array SCOPES = [
|
||||
self::SCOPE_USER_EDIT => 'Edit user',
|
||||
self::SCOPE_PROJECT_CREATE => 'Create project',
|
||||
self::SCOPE_PROJECT_EDIT => 'Edit project',
|
||||
];
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'apiTokens')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?User $ownedBy = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?\DateTimeImmutable $expiresAt = null;
|
||||
|
||||
#[ORM\Column(length: 68)]
|
||||
private ?string $token = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private array $scopes = [];
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
public function __construct(string $tokenType = self::PERSONAL_ACCESS_TOKEN_PREFIX)
|
||||
{
|
||||
$this->token = $tokenType . bin2hex(random_bytes(32));
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getOwnedBy(): ?User
|
||||
{
|
||||
return $this->ownedBy;
|
||||
}
|
||||
|
||||
public function setOwnedBy(?User $ownedBy): static
|
||||
{
|
||||
$this->ownedBy = $ownedBy;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getExpiresAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->expiresAt;
|
||||
}
|
||||
|
||||
public function setExpiresAt(?\DateTimeImmutable $expiresAt): static
|
||||
{
|
||||
$this->expiresAt = $expiresAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getToken(): ?string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
public function setToken(string $token): static
|
||||
{
|
||||
$this->token = $token;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getScopes(): array
|
||||
{
|
||||
return $this->scopes;
|
||||
}
|
||||
|
||||
public function setScopes(array $scopes): static
|
||||
{
|
||||
$this->scopes = $scopes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->expiresAt === null || $this->expiresAt > new \DateTimeImmutable();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user