2022-06-20 19:52:31 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\Objects\DatabaseEntity;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
2022-11-29 14:17:11 +01:00
|
|
|
use Core\Driver\SQL\SQL;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
class ApiKey extends DatabaseEntity {
|
|
|
|
|
|
|
|
private bool $active;
|
|
|
|
#[MaxLength(64)] public String $apiKey;
|
|
|
|
public \DateTime $validUntil;
|
|
|
|
public User $user;
|
|
|
|
|
|
|
|
public function __construct(?int $id = null) {
|
|
|
|
parent::__construct($id);
|
|
|
|
$this->active = true;
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:17:11 +01:00
|
|
|
public function getValidUntil(): \DateTime {
|
|
|
|
return $this->validUntil;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refresh(SQL $sql, int $days): bool {
|
|
|
|
$this->validUntil = (new \DateTime())->modify("+$days days");
|
2023-01-09 14:21:11 +01:00
|
|
|
return $this->save($sql, ["validUntil"]);
|
2022-11-29 14:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function revoke(SQL $sql): bool {
|
|
|
|
$this->active = false;
|
|
|
|
return $this->save($sql, ["active"]);
|
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|