更新
This commit is contained in:
@@ -16,6 +16,7 @@ namespace app\adminapi\logic\auth;
|
||||
|
||||
use app\adminapi\logic\LoginLogic;
|
||||
use app\adminapi\service\DoctorWebConsultationPolicy;
|
||||
use app\adminapi\service\AdminAccessSyncService;
|
||||
use app\common\cache\AdminAuthCache;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
@@ -24,6 +25,7 @@ use app\common\model\auth\AdminDept;
|
||||
use app\common\model\auth\AdminJobs;
|
||||
use app\common\model\auth\AdminRole;
|
||||
use app\common\model\auth\AdminSession;
|
||||
use app\common\model\auth\SystemRole;
|
||||
use app\common\cache\AdminTokenCache;
|
||||
use app\common\service\FileService;
|
||||
use app\common\service\TencentImService;
|
||||
@@ -120,6 +122,14 @@ class AdminLogic extends BaseLogic
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 先锁将要加入的角色,再锁账号,与角色变更收集关联账号使用相同顺序。
|
||||
$submittedRoleIds = self::normalizeRoleIds($params['role_id'] ?? []);
|
||||
if ($submittedRoleIds !== []) {
|
||||
$roles = SystemRole::whereIn('id', $submittedRoleIds)->order('id')->lock(true)->field('id')->select();
|
||||
if (count($roles) !== count($submittedRoleIds)) { throw new \RuntimeException('角色不存在或已删除'); }
|
||||
}
|
||||
$current = Admin::where('id', $params['id'])->lock(true)->findOrEmpty();
|
||||
if ($current->isEmpty()) { throw new \RuntimeException('管理员不存在'); }
|
||||
// 处理资质图片:如果是数组则转为JSON字符串
|
||||
$qualificationImages = '';
|
||||
if (isset($params['qualification_images'])) {
|
||||
@@ -163,17 +173,16 @@ class AdminLogic extends BaseLogic
|
||||
$data['password'] = create_password($params['password'], $passwordSalt);
|
||||
}
|
||||
|
||||
// 禁用或更换角色后.设置token过期
|
||||
// 改密、停用、角色及部门变化均影响原医院和SSO终端;先写事务内撤销记录。
|
||||
$roleId = AdminRole::where('admin_id', $params['id'])->column('role_id');
|
||||
$submittedRoleIds = self::normalizeRoleIds($params['role_id'] ?? []);
|
||||
$editRole = self::roleIdsChanged($roleId, $submittedRoleIds);
|
||||
|
||||
if ($params['disable'] == 1 || $editRole) {
|
||||
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
|
||||
foreach ($tokenArr as $token) {
|
||||
self::expireToken($token['token']);
|
||||
}
|
||||
}
|
||||
$deptChanged = AdminAccessSyncService::ids(AdminDept::where('admin_id', $params['id'])->column('dept_id'))
|
||||
!== AdminAccessSyncService::ids($params['dept_id'] ?? []);
|
||||
$securityChanged = !empty($params['password']) || (int) $params['disable'] === 1 || $editRole || $deptChanged
|
||||
|| (string) $current['account'] !== (string) $params['account']
|
||||
|| (int) $current['multipoint_login'] !== (int) $params['multipoint_login'];
|
||||
$tokens = $securityChanged ? AdminAccessSyncService::revoke([(int) $params['id']]) : [];
|
||||
|
||||
Admin::update($data);
|
||||
|
||||
@@ -192,13 +201,7 @@ class AdminLogic extends BaseLogic
|
||||
self::importDoctorAccountToIm($params['id'], $params['name']);
|
||||
|
||||
Db::commit();
|
||||
// 必须在角色关联提交后删除该账号的 URI 权限缓存,避免并发请求重新写入旧权限。
|
||||
try {
|
||||
(new AdminAuthCache($params['id']))->clearAuthCache();
|
||||
} catch (\Throwable $cacheError) {
|
||||
// 数据已经提交,缓存清理失败不能把成功的编辑伪装成失败;记录后等待缓存自然过期。
|
||||
Log::warning('管理员角色权限缓存清理失败:admin_id=' . $params['id'] . ',error=' . $cacheError->getMessage());
|
||||
}
|
||||
AdminAccessSyncService::clearCommitted([(int) $params['id']], $tokens);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
@@ -219,25 +222,20 @@ class AdminLogic extends BaseLogic
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$admin = Admin::findOrEmpty($params['id']);
|
||||
$admin = Admin::where('id', $params['id'])->lock(true)->findOrEmpty();
|
||||
if ($admin->root == YesNoEnum::YES) {
|
||||
throw new \Exception("超级管理员不允许被删除");
|
||||
}
|
||||
$tokens = AdminAccessSyncService::revoke([(int) $params['id']]);
|
||||
Admin::destroy($params['id']);
|
||||
|
||||
//设置token过期
|
||||
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
|
||||
foreach ($tokenArr as $token) {
|
||||
self::expireToken($token['token']);
|
||||
}
|
||||
(new AdminAuthCache($params['id']))->clearAuthCache();
|
||||
|
||||
// 删除旧的关联信息
|
||||
AdminRole::delByUserId($params['id']);
|
||||
AdminDept::delByUserId($params['id']);
|
||||
AdminJobs::delByUserId($params['id']);
|
||||
|
||||
Db::commit();
|
||||
AdminAccessSyncService::clearCommitted([(int) $params['id']], $tokens);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
@@ -259,20 +257,14 @@ class AdminLogic extends BaseLogic
|
||||
*/
|
||||
public static function expireToken($token): bool
|
||||
{
|
||||
$adminSession = AdminSession::where('token', '=', $token)
|
||||
->with('admin')
|
||||
->find();
|
||||
|
||||
if (empty($adminSession)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$time = time();
|
||||
$adminSession->expire_time = $time;
|
||||
$adminSession->update_time = $time;
|
||||
$adminSession->save();
|
||||
|
||||
return (new AdminTokenCache())->deleteAdminInfo($token);
|
||||
$changed = Db::transaction(static function () use ($token): bool {
|
||||
$candidate = AdminSession::where('token', $token)->findOrEmpty();
|
||||
if ($candidate->isEmpty()) { return false; }
|
||||
Admin::where('id', (int) $candidate['admin_id'])->lock(true)->field('id')->findOrEmpty();
|
||||
return AdminSession::where('token', $token)->update(['expire_time' => time() - 1, 'update_time' => time()]) > 0;
|
||||
});
|
||||
if ($changed) { (new AdminTokenCache())->deleteAdminInfo($token); }
|
||||
return $changed;
|
||||
}
|
||||
|
||||
|
||||
@@ -355,7 +347,22 @@ class AdminLogic extends BaseLogic
|
||||
$data['password'] = create_password($params['password'], $passwordSalt);
|
||||
}
|
||||
|
||||
return Admin::update($data);
|
||||
$tokens = [];
|
||||
$result = Db::transaction(static function () use ($params, $data, &$tokens) {
|
||||
$admin = Admin::where('id', $params['admin_id'])->lock(true)->findOrEmpty();
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0) { throw new \RuntimeException('管理员不存在或已停用'); }
|
||||
if (!empty($params['password'])) {
|
||||
$oldPassword = $params['password_old'] ?? null;
|
||||
if (!is_string($oldPassword) || !hash_equals((string) $admin['password'],
|
||||
create_password($oldPassword, (string) Config::get('project.unique_identification')))) {
|
||||
throw new \RuntimeException('当前密码错误');
|
||||
}
|
||||
$tokens = AdminAccessSyncService::revoke([(int) $params['admin_id']]);
|
||||
}
|
||||
return Admin::update($data);
|
||||
});
|
||||
AdminAccessSyncService::clearCommitted([(int) $params['admin_id']], $tokens);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user