ringfinger/backend/classes/database/User.php

173 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2020-08-17 23:46:58 +02:00
<?php
declare(strict_types=1);
final class User extends MySqlTable implements JsonSerializable
{
2020-08-23 12:37:39 +02:00
public const FIELD_ID = 'UserId';
public const FIELD_USERNAME = 'Username';
public const FIELD_PASSWORD = 'Password';
public const FIELD_EMAIL = 'Email';
public const FIELD_JABBER_ADDRESS = 'JabberAddress';
2020-08-22 23:08:05 +02:00
public const FIELD_ADMIN = 'IsAdmin';
2020-08-17 23:46:58 +02:00
2020-08-22 23:08:05 +02:00
public function __construct($id = null, DatabaseInterface &$database = null)
2020-08-23 12:37:39 +02:00
{
parent::__construct(self::class, $id, $database);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function getUserId(): ?int
{
if ($this->getPrimaryKey() === null) {
return null;
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
return (int)$this->getPrimaryKey();
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function getUsername(): string
{
return $this->getField(self::FIELD_USERNAME);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function getPassword(): string
{
return $this->getField(self::FIELD_PASSWORD);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function getEmail(): string
{
return $this->getField(self::FIELD_EMAIL);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function getJabberAddress(): string
{
return $this->getField(self::FIELD_JABBER_ADDRESS);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function isAdmin(): bool
2020-08-22 23:08:05 +02:00
{
return $this->getField(self::FIELD_ADMIN);
}
2020-08-23 12:37:39 +02:00
public function setUsername(string $username): void
{
$this->setField(self::FIELD_USERNAME, $username);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function setPassword(string $password): void
{
$this->setField(self::FIELD_PASSWORD, $password);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function setEmail(string $email): void
{
$this->setField(self::FIELD_EMAIL, $email);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function setJabberAddress(string $jabberAddress): void
{
$this->setField(self::FIELD_JABBER_ADDRESS, $jabberAddress);
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function setAdmin(bool $isAdmin): void
2020-08-22 23:08:05 +02:00
{
$this->setField(self::FIELD_ADMIN, $isAdmin);
}
2020-08-23 12:37:39 +02:00
public static function getFromUsername(string $username, DatabaseInterface &$database = null): self
{
$databaseGiven = true;
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
if ($database === null) {
$database = new MySqlDatabase();
$databaseGiven = false;
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
if ($database->Count(self::class, [self::FIELD_USERNAME => $username]) === 0) {
throw new UserException(sprintf('No user with name %s found!', $username));
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
$id = $database->Select(self::class, [self::FIELD_ID], [self::FIELD_USERNAME => $username])[0][self::FIELD_ID];
2020-08-17 23:46:58 +02:00
2020-09-09 21:35:11 +02:00
return $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
2020-08-23 12:37:39 +02:00
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public static function getFromEmail(string $email, DatabaseInterface &$database = null): self
{
$databaseGiven = true;
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
if ($database === null) {
$database = new MySqlDatabase();
$databaseGiven = false;
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
if ($database->Count(self::class) === 0) {
throw new UserException(sprintf('No user with email %s found!', $email));
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
$id = $database->Select(self::class, [self::FIELD_ID], [self::FIELD_EMAIL => $email])[0][self::FIELD_ID];
2020-08-17 23:46:58 +02:00
2020-09-09 21:35:11 +02:00
return $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
2020-08-23 12:37:39 +02:00
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
public function getFingerprintIds(): array
{
$result = $this->database->Select(
Fingerprint::class,
[Fingerprint::FIELD_ID],
[Fingerprint::FIELD_USER => $this->getUserId()]
);
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
$ids = [];
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
foreach ($result as $record) {
$ids[] = (int)$record[Fingerprint::FIELD_ID];
}
2020-08-17 23:46:58 +02:00
2020-08-23 12:37:39 +02:00
return $ids;
}
2020-08-17 23:46:58 +02:00
public function isSharingWith(int $userId): bool
{
return (bool)$this->database->Count(
Sharing::class,
[Sharing::FIELD_USER => $this->getUserId(), Sharing::FIELD_USER_SHARED => $userId]
);
}
2020-09-09 21:35:11 +02:00
public function getSharings(): SharingCollection
{
$result = $this->database->Select(
Sharing::class,
[],
[Sharing::FIELD_USER => $this->getUserId()]
);
$sharings = new SharingCollection();
foreach ($result as $record) {
$sharing = new Sharing(null, $this->database);
$sharing->setId((int)$record[Sharing::FIELD_ID]);
$sharing->setUserId((int)$record[Sharing::FIELD_USER]);
$sharing->setUserShared((int)$record[Sharing::FIELD_USER_SHARED]);
$sharings->add($sharing);
}
return $sharings;
}
2020-08-23 12:37:39 +02:00
public function jsonSerialize()
{
return [
'userId' => $this->getUserId(),
'username' => $this->getUsername(),
'jabberAddress' => $this->getJabberAddress(),
'isAdmin' => $this->isAdmin(),
2020-08-23 12:37:39 +02:00
'fingerprintIds' => $this->getFingerprintIds()
];
}
2020-08-17 23:46:58 +02:00
}