This repository has been archived on 2025-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
misuzu-interim/src/Users/UserInfo.php

141 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace Misuzu\Users;
use Misuzu\Parsers\Parser;
use Carbon\CarbonImmutable;
use Index\Colour\Colour;
2024-10-05 02:40:29 +00:00
use Index\Db\DbResult;
class UserInfo {
2024-02-07 00:04:45 +00:00
public function __construct(
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
) {}
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),
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),
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),
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),
);
}
public bool $hasPasswordHash {
get => $this->passwordHash !== null && $this->passwordHash !== '';
}
public bool $passwordNeedsRehash {
get => $this->hasPasswordHash && Users::passwordNeedsRehash($this->passwordHash);
}
public function verifyPassword(string $password): bool {
return $this->hasPasswordHash && password_verify($password, $this->passwordHash);
2023-08-28 01:17:34 +00:00
}
public bool $hasColour {
get => $this->colourRaw !== null && ($this->colourRaw & 0x40000000) === 0;
2023-08-28 01:17:34 +00:00
}
public Colour $colour {
get => $this->colourRaw === null ? Colour::none() : Colour::fromMisuzu($this->colourRaw);
2023-08-28 01:17:34 +00:00
}
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
}
public ?CarbonImmutable $lastActiveAt {
get => $this->lastActiveTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->lastActiveTime);
}
public bool $deleted {
get => $this->deletedTime !== null;
}
public ?CarbonImmutable $deletedAt {
get => $this->deletedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deletedTime);
2023-08-28 01:17:34 +00:00
}
public bool $hasTOTP {
get => $this->totpKey !== null;
2023-08-28 01:17:34 +00:00
}
public bool $isAboutBodyPlain {
get => $this->aboutBodyParser === Parser::PLAIN;
2023-08-28 01:17:34 +00:00
}
public bool $isAboutBodyBBCode {
get => $this->aboutBodyParser === Parser::BBCODE;
}
public bool $isAboutBodyMarkdown {
get => $this->aboutBodyParser === Parser::MARKDOWN;
}
public bool $isSignatureBodyPlain {
get => $this->signatureBodyParser === Parser::PLAIN;
}
public bool $isSignatureBodyBBCode {
get => $this->signatureBodyParser === Parser::BBCODE;
}
public bool $isSignatureBodyMarkdown {
get => $this->signatureBodyParser === Parser::MARKDOWN;
}
public bool $hasBirthdate {
get => $this->birthdateRaw !== null;
}
public ?CarbonImmutable $birthdate {
get => $this->birthdateRaw === null ? null : CarbonImmutable::createFromFormat('Y-m-d', $this->birthdateRaw, 'UTC');
}
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');
}
}
}