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
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
use Core\Driver\SQL\Column\Column;
|
|
|
|
use Core\Driver\SQL\Expression\Alias;
|
|
|
|
use Core\Driver\SQL\Expression\Coalesce;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Expression\CurrentTimeStamp;
|
2023-01-16 21:47:23 +01:00
|
|
|
use Core\Driver\SQL\Expression\NullIf;
|
2022-11-18 18:06:46 +01:00
|
|
|
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,
|
2023-01-10 22:12:05 +01:00
|
|
|
'language' => isset($this->language) ? $this->language->getName() : null,
|
2022-06-20 19:52:31 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 18:47:12 +01:00
|
|
|
|
|
|
|
public function getDisplayName(): string {
|
|
|
|
return !empty($this->fullName) ? $this->fullName : $this->name;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
2023-01-19 18:12:16 +01:00
|
|
|
public static function buildSQLDisplayName(SQL $sql, string $joinColumn, string $alias = "user"): Alias {
|
2023-01-16 21:47:23 +01:00
|
|
|
return new Alias(
|
|
|
|
$sql->select(new Coalesce(
|
|
|
|
new NullIf(new Column("User.full_name"), ""),
|
|
|
|
new NullIf(new Column("User.name"), ""))
|
|
|
|
)->from("User")->whereEq("User.id", new Column($joinColumn)),
|
2023-01-19 18:12:16 +01:00
|
|
|
$alias);
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|