- Fix EasyAdmin 5 routing: #[AdminDashboard] attribute + easyadmin.routes loader - Fix login: _username/_password field names, CSRF stateless token config, sessions directory, Opcache reload after cache:clear - Add MistralClient behind OllamaClientInterface — switchable via services.yaml alias - Add Attribute CRUD with EnumType form + ChoiceField display (enum-safe rendering) - Add Article Type CRUD with AssociationField for attribute assignments - Add app:api-keys:create console command (bcrypt-hashed, never stored as plaintext) - Add redis ext to Docker image + symfony/redis-messenger, start workers - Translate all UI strings to English - Add tests: MistralClient, ApiKey, CreateApiKeyCommand, StringArrayType, ArticleTypeCrudController, AttributeDefinitionCrudController (82 tests total) - Update design doc: tech stack, AI backend switching guide, ops section Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
128 lines
2.8 KiB
PHP
128 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Domain\Auth;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Uid\Uuid;
|
|
|
|
#[ORM\Entity]
|
|
#[ORM\Table(name: 'api_keys', schema: 'app')]
|
|
class ApiKey
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\Column(type: 'uuid')]
|
|
private Uuid $id;
|
|
|
|
#[ORM\ManyToOne(targetEntity: User::class)]
|
|
#[ORM\JoinColumn(nullable: false)]
|
|
private User $user;
|
|
|
|
#[ORM\Column(type: 'string', length: 255)]
|
|
private string $label;
|
|
|
|
#[ORM\Column(type: 'string', length: 8)]
|
|
private string $keyPrefix;
|
|
|
|
#[ORM\Column(type: 'string', length: 255, unique: true)]
|
|
private string $keyHash;
|
|
|
|
/** @var array<string, bool> */
|
|
#[ORM\Column(type: 'json')]
|
|
private array $permissions = [];
|
|
|
|
#[ORM\Column(type: 'boolean')]
|
|
private bool $isActive = true;
|
|
|
|
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
|
private ?\DateTimeImmutable $lastUsedAt = null;
|
|
|
|
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
|
private ?\DateTimeImmutable $expiresAt = null;
|
|
|
|
public function __construct(User $user, string $label, string $keyPrefix, string $keyHash)
|
|
{
|
|
$this->id = Uuid::v7();
|
|
$this->user = $user;
|
|
$this->label = $label;
|
|
$this->keyPrefix = $keyPrefix;
|
|
$this->keyHash = $keyHash;
|
|
}
|
|
|
|
public function getId(): Uuid
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getUser(): User
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
public function getLabel(): string
|
|
{
|
|
return $this->label;
|
|
}
|
|
|
|
public function getKeyPrefix(): string
|
|
{
|
|
return $this->keyPrefix;
|
|
}
|
|
|
|
public function getKeyHash(): string
|
|
{
|
|
return $this->keyHash;
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->isActive;
|
|
}
|
|
|
|
public function setIsActive(bool $active): void
|
|
{
|
|
$this->isActive = $active;
|
|
}
|
|
|
|
public function getLastUsedAt(): ?\DateTimeImmutable
|
|
{
|
|
return $this->lastUsedAt;
|
|
}
|
|
|
|
public function getExpiresAt(): ?\DateTimeImmutable
|
|
{
|
|
return $this->expiresAt;
|
|
}
|
|
|
|
public function setExpiresAt(?\DateTimeImmutable $expiresAt): void
|
|
{
|
|
$this->expiresAt = $expiresAt;
|
|
}
|
|
|
|
/** @return array<string, bool> */
|
|
public function getPermissions(): array
|
|
{
|
|
return $this->permissions;
|
|
}
|
|
|
|
public function hasPermission(string $permission): bool
|
|
{
|
|
return $this->permissions[$permission] ?? false;
|
|
}
|
|
|
|
public function grantPermission(string $permission): void
|
|
{
|
|
$this->permissions[$permission] = true;
|
|
}
|
|
|
|
public function markUsed(): void
|
|
{
|
|
$this->lastUsedAt = new \DateTimeImmutable();
|
|
}
|
|
|
|
public function isExpired(): bool
|
|
{
|
|
return null !== $this->expiresAt && $this->expiresAt < new \DateTimeImmutable();
|
|
}
|
|
}
|