From 50cb069677f3d1df8d751e67322a268179b70522 Mon Sep 17 00:00:00 2001 From: augushong Date: Fri, 17 Jul 2026 23:44:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E8=A7=92=E8=89=B2=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=96=B0=E5=A2=9E=E5=88=86=E9=85=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E6=96=B9=E6=B3=95(assignUsers+toggleUser)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .../base/admin/controller/system/AuthBase.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/extend/base/admin/controller/system/AuthBase.php b/extend/base/admin/controller/system/AuthBase.php index e73abd5..4f40452 100644 --- a/extend/base/admin/controller/system/AuthBase.php +++ b/extend/base/admin/controller/system/AuthBase.php @@ -2,14 +2,17 @@ namespace base\admin\controller\system; +use app\admin\model\SystemAdmin; use app\admin\model\SystemAuth; use app\admin\model\SystemAuthNode; use app\admin\service\annotation\ControllerAnnotation; use app\admin\service\annotation\NodeAnotation; use app\admin\service\NodeService; use app\admin\service\TriggerService; +use app\common\constants\AdminConstant; use app\common\controller\AdminController; use think\App; +use think\facade\Db; /** * @ControllerAnnotation(title="角色权限管理") @@ -95,4 +98,88 @@ class AuthBase extends AdminController } $this->success('保存成功'); } + + /** + * @NodeAnotation(title="分配用户") + */ + public function assignUsers($id) + { + $row = $this->model->find($id); + empty($row) && $this->error('数据不存在'); + + if ($this->request->isAjax()) { + list($page, $limit, $where) = $this->buildTableParames(); + $count = SystemAdmin::where($where) + ->where('delete_time', 0) + ->count(); + $list = SystemAdmin::where($where) + ->withoutField('password') + ->fieldRaw('FIND_IN_SET(' . intval($id) . ', auth_ids) AS has_role') + ->where('delete_time', 0) + ->page($page, $limit) + ->order('id desc') + ->select(); + $data = [ + 'code' => 0, + 'msg' => '', + 'count' => $count, + 'data' => $list, + ]; + + return json($data); + } + + $this->assign('row', $row); + + return $this->fetch(); + } + + /** + * @NodeAnotation(title="分配用户切换") + */ + public function toggleUser() + { + $this->checkPostRequest(); + $userId = intval($this->request->post('user_id')); + $roleId = intval($this->request->post('role_id')); + + if ($userId == AdminConstant::SUPER_ADMIN_ID) { + $this->error('超级管理员不支持此操作'); + } + + $user = SystemAdmin::find($userId); + if (empty($user)) { + $this->error('用户不存在'); + } + $role = SystemAuth::where('id', $roleId)->where('delete_time', 0)->find(); + if (empty($role)) { + $this->error('角色不存在'); + } + + Db::startTrans(); + try { + // FOR UPDATE 行锁,防止并发下读-改-写丢失逗号串的其它角色 + $userRow = Db::name('system_admin')->where('id', $userId)->lock(true)->find(); + $existing = empty($userRow['auth_ids']) ? [] : array_filter(explode(',', $userRow['auth_ids'])); + $existing = array_values(array_map('strval', $existing)); + $roleIdStr = (string) $roleId; + if (in_array($roleIdStr, $existing)) { + // 撤回:仅移除该角色,保留其它角色 + $newArr = array_values(array_diff($existing, [$roleIdStr])); + } else { + // 分配:新增该角色,保留其它角色 + $newArr = array_values(array_unique(array_merge($existing, [$roleIdStr]))); + } + Db::name('system_admin')->where('id', $userId)->update([ + 'auth_ids' => empty($newArr) ? '' : implode(',', $newArr), + ]); + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + $this->error('操作失败:' . $e->getMessage()); + } + + TriggerService::updateMenu($userId); + $this->success('操作成功'); + } }