2023-08-02 22:12:47 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\Users;
|
|
|
|
|
2024-08-04 21:37:12 +00:00
|
|
|
use Misuzu\Parsers\Parser;
|
|
|
|
use Carbon\CarbonImmutable;
|
2023-08-02 22:12:47 +00:00
|
|
|
use Index\Colour\Colour;
|
2024-10-05 02:40:29 +00:00
|
|
|
use Index\Db\DbResult;
|
2023-08-02 22:12:47 +00:00
|
|
|
|
|
|
|
class UserInfo {
|
2024-02-07 00:04:45 +00:00
|
|
|
public function __construct(
|
2024-11-30 04:09:29 +00:00
|
|
|
public private(set) string $id,
|
|
|
|
public private(set) string $name,
|
|
|
|
#[\SensitiveParameter] private ?string $passwordHash,
|
|
|
|
#[\SensitiveParameter] public private(set) string $emailAddress,
|
|
|
|
public private(set) string $registerRemoteAddress,
|
|
|
|
public private(set) string $lastRemoteAddress,
|
|
|
|
public private(set) bool $super,
|
|
|
|
public private(set) string $countryCode,
|
|
|
|
public private(set) ?int $colourRaw,
|
|
|
|
public private(set) int $createdTime,
|
|
|
|
public private(set) ?int $lastActiveTime,
|
|
|
|
public private(set) ?int $deletedTime,
|
|
|
|
public private(set) ?string $displayRoleId,
|
|
|
|
#[\SensitiveParameter] public private(set) ?string $totpKey,
|
|
|
|
public private(set) ?string $aboutBody,
|
|
|
|
public private(set) int $aboutBodyParser,
|
|
|
|
public private(set) ?string $signatureBody,
|
|
|
|
public private(set) int $signatureBodyParser,
|
|
|
|
public private(set) ?string $birthdateRaw,
|
|
|
|
public private(set) ?int $backgroundSettings,
|
|
|
|
public private(set) ?string $title,
|
2024-02-07 00:04:45 +00:00
|
|
|
) {}
|
2023-08-02 22:12:47 +00:00
|
|
|
|
2024-10-05 02:40:29 +00:00
|
|
|
public static function fromResult(DbResult $result): self {
|
2024-02-07 00:04:45 +00:00
|
|
|
return new UserInfo(
|
|
|
|
id: $result->getString(0),
|
|
|
|
name: $result->getString(1),
|
|
|
|
passwordHash: $result->getStringOrNull(2),
|
2024-11-30 04:09:29 +00:00
|
|
|
emailAddress: $result->getString(3),
|
|
|
|
registerRemoteAddress: $result->getString(4),
|
|
|
|
lastRemoteAddress: $result->getStringOrNull(5),
|
2024-02-07 00:04:45 +00:00
|
|
|
super: $result->getBoolean(6),
|
|
|
|
countryCode: $result->getString(7),
|
2024-11-30 04:09:29 +00:00
|
|
|
colourRaw: $result->getIntegerOrNull(8),
|
|
|
|
createdTime: $result->getInteger(9),
|
|
|
|
lastActiveTime: $result->getIntegerOrNull(10),
|
|
|
|
deletedTime: $result->getIntegerOrNull(11),
|
2024-03-30 03:19:08 +00:00
|
|
|
displayRoleId: $result->getStringOrNull(12),
|
2024-02-07 00:04:45 +00:00
|
|
|
totpKey: $result->getStringOrNull(13),
|
2024-11-30 04:09:29 +00:00
|
|
|
aboutBody: $result->getStringOrNull(14),
|
|
|
|
aboutBodyParser: $result->getInteger(15),
|
|
|
|
signatureBody: $result->getStringOrNull(16),
|
|
|
|
signatureBodyParser: $result->getInteger(17),
|
|
|
|
birthdateRaw: $result->getStringOrNull(18),
|
2024-02-07 00:04:45 +00:00
|
|
|
backgroundSettings: $result->getIntegerOrNull(19),
|
|
|
|
title: $result->getString(20),
|
|
|
|
);
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $hasPasswordHash {
|
|
|
|
get => $this->passwordHash !== null && $this->passwordHash !== '';
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $passwordNeedsRehash {
|
|
|
|
get => $this->hasPasswordHash && Users::passwordNeedsRehash($this->passwordHash);
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function verifyPassword(string $password): bool {
|
2024-11-30 04:09:29 +00:00
|
|
|
return $this->hasPasswordHash && password_verify($password, $this->passwordHash);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $hasColour {
|
|
|
|
get => $this->colourRaw !== null && ($this->colourRaw & 0x40000000) === 0;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public Colour $colour {
|
|
|
|
get => $this->colourRaw === null ? Colour::none() : Colour::fromMisuzu($this->colourRaw);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public CarbonImmutable $createdAt {
|
|
|
|
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public ?CarbonImmutable $lastActiveAt {
|
|
|
|
get => $this->lastActiveTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->lastActiveTime);
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $deleted {
|
|
|
|
get => $this->deletedTime !== null;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public ?CarbonImmutable $deletedAt {
|
|
|
|
get => $this->deletedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deletedTime);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $hasTOTP {
|
|
|
|
get => $this->totpKey !== null;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isAboutBodyPlain {
|
|
|
|
get => $this->aboutBodyParser === Parser::PLAIN;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isAboutBodyBBCode {
|
|
|
|
get => $this->aboutBodyParser === Parser::BBCODE;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isAboutBodyMarkdown {
|
|
|
|
get => $this->aboutBodyParser === Parser::MARKDOWN;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isSignatureBodyPlain {
|
|
|
|
get => $this->signatureBodyParser === Parser::PLAIN;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isSignatureBodyBBCode {
|
|
|
|
get => $this->signatureBodyParser === Parser::BBCODE;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isSignatureBodyMarkdown {
|
|
|
|
get => $this->signatureBodyParser === Parser::MARKDOWN;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $hasBirthdate {
|
|
|
|
get => $this->birthdateRaw !== null;
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public ?CarbonImmutable $birthdate {
|
|
|
|
get => $this->birthdateRaw === null ? null : CarbonImmutable::createFromFormat('Y-m-d', $this->birthdateRaw, 'UTC');
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public int $age {
|
|
|
|
get {
|
|
|
|
$birthdate = $this->birthdate;
|
|
|
|
if($birthdate === null || (int)$birthdate->format('Y') < 1900)
|
|
|
|
return -1;
|
|
|
|
return (int)$birthdate->diff(CarbonImmutable::now())->format('%y');
|
|
|
|
}
|
2023-08-02 22:12:47 +00:00
|
|
|
}
|
|
|
|
}
|