2022-11-19 01:15:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Core\Objects\DatabaseEntity;
|
|
|
|
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\DefaultValue;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\EnumArr;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
2022-11-19 01:15:34 +01:00
|
|
|
|
|
|
|
class UserToken extends DatabaseEntity {
|
|
|
|
|
|
|
|
const TYPE_PASSWORD_RESET = "password_reset";
|
|
|
|
const TYPE_EMAIL_CONFIRM = "email_confirm";
|
|
|
|
const TYPE_INVITE = "invite";
|
|
|
|
const TYPE_GPG_CONFIRM = "gpg_confirm";
|
|
|
|
|
|
|
|
const TOKEN_TYPES = [
|
|
|
|
self::TYPE_PASSWORD_RESET, self::TYPE_EMAIL_CONFIRM,
|
|
|
|
self::TYPE_INVITE, self::TYPE_GPG_CONFIRM
|
|
|
|
];
|
|
|
|
|
|
|
|
#[MaxLength(36)]
|
|
|
|
private string $token;
|
|
|
|
|
|
|
|
#[EnumArr(self::TOKEN_TYPES)]
|
|
|
|
private string $tokenType;
|
|
|
|
|
|
|
|
private User $user;
|
|
|
|
private \DateTime $validUntil;
|
|
|
|
|
|
|
|
#[DefaultValue(false)]
|
|
|
|
private bool $used;
|
|
|
|
|
|
|
|
public function __construct(User $user, string $token, string $type, int $validHours) {
|
|
|
|
parent::__construct();
|
|
|
|
$this->user = $user;
|
|
|
|
$this->token = $token;
|
|
|
|
$this->tokenType = $type;
|
|
|
|
$this->validUntil = (new \DateTime())->modify("+$validHours HOUR");
|
|
|
|
$this->used = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function jsonSerialize(): array {
|
|
|
|
return [
|
|
|
|
"id" => $this->getId(),
|
|
|
|
"token" => $this->token,
|
|
|
|
"tokenType" => $this->tokenType
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType(): string {
|
|
|
|
return $this->tokenType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invalidate(SQL $sql): bool {
|
|
|
|
$this->used = true;
|
2022-11-29 14:17:11 +01:00
|
|
|
return $this->save($sql, ["used"]);
|
2022-11-19 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser(): User {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateDurability(SQL $sql, int $validHours): bool {
|
|
|
|
$this->validUntil = (new \DateTime())->modify("+$validHours HOURS");
|
2022-11-29 14:17:11 +01:00
|
|
|
return $this->save($sql, ["valid_until"]);
|
2022-11-19 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getToken(): string {
|
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
}
|