Files
zyt/identity-admin/server/app/service/AccountService.php
T
2026-11-11 13:53:14 +08:00

85 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace app\service;
use app\exception\ApiError;
use app\repository\IdentityRepository;
final class AccountService
{
public function __construct(private readonly IdentityRepository $repository)
{
}
public function lists(array $query): array
{
[$page, $size, $keyword, $disable] = Input::page($query);
$where = ' WHERE (account LIKE ? OR name LIKE ?)';
$params = ['%' . $keyword . '%', '%' . $keyword . '%'];
if ($disable !== null) { $where .= ' AND disable=?'; $params[] = $disable; }
$count = (int) $this->repository->db->one('SELECT COUNT(*) AS n FROM admin' . $where, $params)['n'];
$offset = ($page - 1) * $size;
$rows = $this->repository->db->all('SELECT * FROM admin' . $where . " ORDER BY id DESC LIMIT {$size} OFFSET {$offset}", $params);
return ['lists' => array_map($this->repository->publicAdmin(...), $rows), 'count' => $count, 'page_no' => $page, 'page_size' => $size];
}
public function save(string $action, array $data, array $actor): array
{
$allowed = ['account', 'name', 'disable', 'role_id', 'dept_id', 'password'];
Input::only($data, $action === 'edit' ? [...$allowed, 'id'] : $allowed);
return $this->repository->db->transaction(function () use ($action, $data, $actor): array {
$id = $action === 'edit' ? Input::integer($data['id'] ?? null) : 0;
$old = $id ? $this->repository->require('admin', $id) : [];
if ($old && (int) $old['is_root'] === 1 && (int) $actor['is_root'] !== 1) {
throw new ApiError(403, '只有根管理员可以编辑根账号');
}
$source = array_replace($old, $data);
$values = ['account' => Input::account($source['account'] ?? null), 'name' => Input::text($source['name'] ?? null, '名称'),
'disable' => Input::integer($source['disable'] ?? 0, '状态', 0, 1)];
if ($values['disable'] === 1 && ($id === (int) $actor['id'] || (int) ($old['is_root'] ?? 0) === 1)) {
throw new ApiError(409, '不能停用自己或根账号');
}
$oldRoles = $id ? $this->repository->ids('admin_role', 'admin_id', $id, 'role_id') : [];
$oldDepts = $id ? $this->repository->ids('admin_dept', 'admin_id', $id, 'dept_id') : [];
$roles = Input::ids(array_key_exists('role_id', $data) ? $data['role_id'] : $oldRoles, '角色');
$depts = Input::ids(array_key_exists('dept_id', $data) ? $data['dept_id'] : $oldDepts, '部门');
$this->repository->associations('system_role', $roles, $oldRoles);
$this->repository->associations('dept', $depts, $oldDepts);
if ($this->repository->db->one('SELECT id FROM admin WHERE account=? AND id<>?', [$values['account'], $id])) {
throw new ApiError(409, '账号已存在');
}
$passwordChanged = false;
if (!$id || (array_key_exists('password', $data) && $data['password'] !== '')) {
$values['password_hash'] = password_hash(Input::password($data['password'] ?? null), PASSWORD_DEFAULT);
$passwordChanged = true;
}
if ($id) {
$this->repository->update('admin', $id, $values);
} else {
$id = $this->repository->insert('admin', $values + ['is_root' => 0, 'create_time' => time()]);
}
$this->repository->replace('admin_role', 'admin_id', $id, 'role_id', $roles);
$this->repository->replace('admin_dept', 'admin_id', $id, 'dept_id', $depts);
// Privilege changes are re-evaluated on every request; password/status changes also revoke sessions.
if ($passwordChanged || $values['disable'] === 1) {
$this->repository->db->execute('DELETE FROM admin_session WHERE admin_id=?', [$id]);
}
return $this->repository->publicAdmin($this->repository->require('admin', $id));
});
}
public function delete(array $data, array $actor): array
{
Input::only($data, ['id']);
$id = Input::integer($data['id'] ?? null);
return $this->repository->db->transaction(function () use ($id, $actor): array {
$admin = $this->repository->require('admin', $id);
if ($id === (int) $actor['id'] || (int) $admin['is_root'] === 1) {
throw new ApiError(409, '不能删除自己或根账号');
}
$this->repository->db->execute('DELETE FROM admin WHERE id=?', [$id]);
return ['id' => $id];
});
}
}