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-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Expression\CurrentTimeStamp;
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\DefaultValue;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Multiple;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Unique;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
class User extends DatabaseEntity {
|
|
|
|
|
|
|
|
#[MaxLength(32)] #[Unique] public string $name;
|
|
|
|
#[MaxLength(128)] public string $password;
|
|
|
|
#[MaxLength(64)] public string $fullName;
|
|
|
|
#[MaxLength(64)] #[Unique] public ?string $email;
|
2022-11-19 01:15:34 +01:00
|
|
|
#[MaxLength(64)] public ?string $profilePicture;
|
2022-06-20 19:52:31 +02:00
|
|
|
private ?\DateTime $lastOnline;
|
|
|
|
#[DefaultValue(CurrentTimeStamp::class)] public \DateTime $registeredAt;
|
2022-11-20 17:13:53 +01:00
|
|
|
#[DefaultValue(false)] public bool $confirmed;
|
|
|
|
#[DefaultValue(Language::AMERICAN_ENGLISH)] public Language $language;
|
2022-11-19 01:15:34 +01:00
|
|
|
public ?GpgKey $gpgKey;
|
2022-06-20 19:52:31 +02:00
|
|
|
private ?TwoFactorToken $twoFactorToken;
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
#[Multiple(Group::class)]
|
|
|
|
public array $groups;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
public function __construct(?int $id = null) {
|
|
|
|
parent::__construct($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUsername(): string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFullName(): string {
|
|
|
|
return $this->fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEmail(): ?string {
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGroups(): array {
|
|
|
|
return $this->groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasGroup(int $group): bool {
|
|
|
|
return isset($this->groups[$group]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGPG(): ?GpgKey {
|
|
|
|
return $this->gpgKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTwoFactorToken(): ?TwoFactorToken {
|
|
|
|
return $this->twoFactorToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProfilePicture(): ?string {
|
|
|
|
return $this->profilePicture;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __debugInfo(): array {
|
|
|
|
return [
|
|
|
|
'id' => $this->getId(),
|
|
|
|
'username' => $this->name,
|
|
|
|
'language' => $this->language->getName(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function jsonSerialize(): array {
|
|
|
|
return [
|
|
|
|
'id' => $this->getId(),
|
|
|
|
'name' => $this->name,
|
|
|
|
'fullName' => $this->fullName,
|
|
|
|
'profilePicture' => $this->profilePicture,
|
|
|
|
'email' => $this->email,
|
|
|
|
'groups' => $this->groups ?? null,
|
|
|
|
'language' => (isset($this->language) ? $this->language->jsonSerialize() : null),
|
|
|
|
'session' => (isset($this->session) ? $this->session->jsonSerialize() : null),
|
|
|
|
"gpg" => (isset($this->gpgKey) ? $this->gpgKey->jsonSerialize() : null),
|
|
|
|
"2fa" => (isset($this->twoFactorToken) ? $this->twoFactorToken->jsonSerialize() : null),
|
2022-11-19 01:15:34 +01:00
|
|
|
"reqisteredAt" => $this->registeredAt->getTimestamp(),
|
|
|
|
"lastOnline" => $this->lastOnline->getTimestamp(),
|
|
|
|
"confirmed" => $this->confirmed
|
2022-06-20 19:52:31 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(SQL $sql): bool {
|
|
|
|
$this->lastOnline = new \DateTime();
|
|
|
|
return $this->save($sql, ["last_online", "language_id"]);
|
|
|
|
}
|
2022-11-27 15:58:44 +01:00
|
|
|
|
|
|
|
public function setTwoFactorToken(TwoFactorToken $twoFactorToken) {
|
|
|
|
$this->twoFactorToken = $twoFactorToken;
|
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|