2026-09-30 00:49:53 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 登录账号。一个人最多一个账号,账号依附于「人」而存在。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
|
|
final class Account
|
|
|
|
|
{
|
2026-09-30 08:13:07 +08:00
|
|
|
public const ROLES = ['admin', 'member', 'guest'];
|
|
|
|
|
|
2026-09-30 00:49:53 +08:00
|
|
|
public static function findByPerson(int $personId): ?array
|
|
|
|
|
{
|
|
|
|
|
$st = Database::get()->prepare('SELECT * FROM accounts WHERE person_id = ?');
|
|
|
|
|
$st->execute([$personId]);
|
|
|
|
|
$row = $st->fetch();
|
|
|
|
|
return $row ?: null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function create(int $personId, string $password, string $role = 'member'): int
|
|
|
|
|
{
|
2026-09-30 08:13:07 +08:00
|
|
|
$role = in_array($role, self::ROLES, true) ? $role : 'member';
|
2026-09-30 00:49:53 +08:00
|
|
|
$st = Database::get()->prepare(
|
|
|
|
|
'INSERT INTO accounts (person_id, password_hash, role) VALUES (?, ?, ?)'
|
|
|
|
|
);
|
|
|
|
|
$st->execute([$personId, password_hash($password, PASSWORD_DEFAULT), $role]);
|
2026-09-30 13:24:26 +08:00
|
|
|
$id = (int)Database::get()->lastInsertId();
|
|
|
|
|
self::seedSelfRoster($personId);
|
|
|
|
|
return $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刚开通账号的人,自己的名册里先放上他自己。
|
|
|
|
|
*
|
|
|
|
|
* 名册是「每人一份」(v5 起),所以新账号的名册默认是空的 ——
|
|
|
|
|
* 那是对的(他再加/再复用别人),但连「我」都看不见就太奇怪了:
|
|
|
|
|
* 首页/家谱里都有他自己,名册里却没有,看着像自己不在家里。
|
|
|
|
|
* 所以这里只补他自己,别人一概不补。
|
|
|
|
|
*
|
|
|
|
|
* ⚠️ 故意**不** try/catch 吞异常:这是开账号链路的一部分,
|
|
|
|
|
* 出了问题要立刻看得见(静默失败正是这个项目踩过的坑)。
|
|
|
|
|
*/
|
|
|
|
|
private static function seedSelfRoster(int $personId): void
|
|
|
|
|
{
|
|
|
|
|
$st = Database::get()->prepare('SELECT family_id FROM persons WHERE id = ?');
|
|
|
|
|
$st->execute([$personId]);
|
|
|
|
|
$fid = (int)$st->fetchColumn();
|
|
|
|
|
if ($fid > 0) {
|
|
|
|
|
PersonRoster::ensure($fid, $personId, $personId, null, 'self');
|
|
|
|
|
}
|
2026-09-30 00:49:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function setPassword(int $accountId, string $password): void
|
|
|
|
|
{
|
|
|
|
|
Database::get()->prepare('UPDATE accounts SET password_hash = ? WHERE id = ?')
|
|
|
|
|
->execute([password_hash($password, PASSWORD_DEFAULT), $accountId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function setRole(int $accountId, string $role): void
|
|
|
|
|
{
|
2026-09-30 08:13:07 +08:00
|
|
|
$role = in_array($role, self::ROLES, true) ? $role : 'member';
|
2026-09-30 00:49:53 +08:00
|
|
|
Database::get()->prepare('UPDATE accounts SET role = ? WHERE id = ?')
|
|
|
|
|
->execute([$role, $accountId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function setStatus(int $accountId, string $status): void
|
|
|
|
|
{
|
|
|
|
|
Database::get()->prepare('UPDATE accounts SET status = ? WHERE id = ?')
|
|
|
|
|
->execute([$status, $accountId]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 08:13:07 +08:00
|
|
|
/** 各角色各有多少人(设置页显示) */
|
|
|
|
|
public static function roleCounts(): array
|
|
|
|
|
{
|
|
|
|
|
$st = Database::get()->query(
|
|
|
|
|
"SELECT role, COUNT(*) AS n FROM accounts WHERE status = 'active' GROUP BY role"
|
|
|
|
|
);
|
|
|
|
|
$out = ['admin' => 0, 'member' => 0, 'guest' => 0];
|
|
|
|
|
foreach ($st->fetchAll() as $r) {
|
|
|
|
|
$out[(string)$r['role']] = (int)$r['n'];
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function roleWord(string $role): string
|
|
|
|
|
{
|
|
|
|
|
return ['admin' => '管理员', 'member' => '家庭成员', 'guest' => '访客'][$role] ?? '成员';
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 00:49:53 +08:00
|
|
|
/** 管理员不能被停用/降权 —— 否则全家人都会被锁在系统外面 */
|
|
|
|
|
public static function adminCount(): int
|
|
|
|
|
{
|
|
|
|
|
$st = Database::get()->prepare(
|
|
|
|
|
"SELECT COUNT(*) FROM accounts a
|
|
|
|
|
JOIN persons p ON p.id = a.person_id
|
|
|
|
|
WHERE a.role = 'admin' AND a.status = 'active'"
|
|
|
|
|
);
|
|
|
|
|
$st->execute();
|
|
|
|
|
return (int)$st->fetchColumn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function validatePassword(string $password): array
|
|
|
|
|
{
|
|
|
|
|
$err = [];
|
|
|
|
|
if (strlen($password) < 8) {
|
|
|
|
|
$err[] = '密码至少 8 位';
|
|
|
|
|
}
|
|
|
|
|
if (!preg_match('/[0-9]/', $password) || !preg_match('/[A-Za-z]/', $password)) {
|
|
|
|
|
$err[] = '密码要同时包含字母和数字';
|
|
|
|
|
}
|
|
|
|
|
return $err;
|
|
|
|
|
}
|
|
|
|
|
}
|