web-base/Core/Objects/DatabaseEntity/User.class.php

140 lines
3.7 KiB
PHP
Raw Normal View History

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;
2023-01-07 15:34:05 +01:00
use Core\Objects\DatabaseEntity\Attribute\Visibility;
2022-11-20 17:13:53 +01:00
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
2023-01-07 15:34:05 +01:00
use Core\Objects\DatabaseEntity\Controller\DatabaseEntityHandler;
2022-06-20 19:52:31 +02:00
class User extends DatabaseEntity {
#[MaxLength(32)] #[Unique] public string $name;
2023-01-07 15:34:05 +01:00
#[MaxLength(128)]
#[Visibility(Visibility::NONE)]
public string $password;
#[MaxLength(64)]
public string $fullName;
#[MaxLength(64)]
#[Unique]
public ?string $email;
#[MaxLength(64)]
public ?string $profilePicture;
#[Visibility(Visibility::BY_GROUP, Group::ADMIN, Group::SUPPORT)]
2022-06-20 19:52:31 +02:00
private ?\DateTime $lastOnline;
2023-01-07 15:34:05 +01:00
#[Visibility(Visibility::BY_GROUP, Group::ADMIN, Group::SUPPORT)]
#[DefaultValue(CurrentTimeStamp::class)]
public \DateTime $registeredAt;
#[Visibility(Visibility::BY_GROUP, Group::ADMIN, Group::SUPPORT)]
#[DefaultValue(false)]
public bool $confirmed;
2022-11-20 17:13:53 +01:00
#[DefaultValue(Language::AMERICAN_ENGLISH)] public Language $language;
2023-01-07 15:34:05 +01:00
#[Visibility(Visibility::BY_GROUP, Group::ADMIN, Group::SUPPORT)]
2022-11-19 01:15:34 +01:00
public ?GpgKey $gpgKey;
2023-01-07 15:34:05 +01:00
#[Visibility(Visibility::BY_GROUP, Group::ADMIN, Group::SUPPORT)]
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 update(SQL $sql): bool {
$this->lastOnline = new \DateTime();
2023-01-09 14:21:11 +01:00
return $this->save($sql, ["lastOnline", "language"]);
2022-06-20 19:52:31 +02:00
}
2022-11-27 15:58:44 +01:00
public function setTwoFactorToken(TwoFactorToken $twoFactorToken) {
$this->twoFactorToken = $twoFactorToken;
}
2023-01-07 15:34:05 +01:00
public function canAccess(\ReflectionClass|DatabaseEntity|string $entityOrClass, string $propertyName): bool {
try {
$reflectionClass = ($entityOrClass instanceof \ReflectionClass
? $entityOrClass
: new \ReflectionClass($entityOrClass));
$property = $reflectionClass->getProperty($propertyName);
$visibility = DatabaseEntityHandler::getAttribute($property, Visibility::class);
if ($visibility === null) {
return true;
}
$visibilityType = $visibility->getType();
if ($visibilityType === Visibility::NONE) {
return false;
} else if ($visibilityType === Visibility::BY_GROUP) {
// allow access to own entity
if ($entityOrClass instanceof User && $entityOrClass->getId() === $this->id) {
return true;
}
// missing required group
if (empty(array_intersect(array_keys($this->groups), $visibility->getGroups()))) {
return false;
}
}
return true;
} catch (\Exception $exception) {
return false;
}
}
2022-06-20 19:52:31 +02:00
}