From 0aae8e36eb654f7646441a19aa2160b702fa0ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 1 Feb 2024 20:39:54 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E9=80=BB=E8=BE=91=E5=AE=8C?= =?UTF-8?q?=E5=96=84=EF=BC=8C=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=94=A8=E6=88=B7=E8=A7=92=E8=89=B2=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/auth/handle/oauth2.go | 4 +++- service/role/select.go | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/auth/handle/oauth2.go b/pkg/auth/handle/oauth2.go index b930f2c2..9f538f6e 100644 --- a/pkg/auth/handle/oauth2.go +++ b/pkg/auth/handle/oauth2.go @@ -12,6 +12,7 @@ import ( "wechat-robot/internal/database" "wechat-robot/model/entity" userService "wechat-robot/service/adminuser" + "wechat-robot/service/role" "wechat-robot/utils" ) @@ -64,7 +65,8 @@ func ExtensionFields(ti oauth2.TokenInfo) (fieldsValue map[string]any) { _ = database.Client.Take(&ui, "id = ?", ti.GetUserID()).Error fieldsValue["username"] = ui.Username fieldsValue["id"] = ui.Id - fieldsValue["roles"] = []string{"admin"} + // 获取用户的角色代码 + fieldsValue["roles"] = role.GetCodesByUserId(ti.GetUserID()) return } diff --git a/service/role/select.go b/service/role/select.go index 6eade7f3..b86d1899 100644 --- a/service/role/select.go +++ b/service/role/select.go @@ -10,7 +10,16 @@ import ( // @param userId string 用户Id // @return flag bool 是否是超级管理员用户 func CheckIsSuperAdminUser(userId string) (flag bool) { - var codes []string + // 获取用户角色代码 + codes := GetCodesByUserId(userId) + return slices.Contains(codes, "admin") +} + +// GetCodesByUserId +// @description: 根据用户Id获取用户的角色代码 +// @param userId string 用户Id +// @return codes []string 角色代码 +func GetCodesByUserId(userId string) (codes []string) { // 获取用户角色代码 database.Client.Table("t_role AS tr"). Joins("LEFT JOIN t_admin_user_role AS tur ON tur.role_id = tr.id"). @@ -18,5 +27,5 @@ func CheckIsSuperAdminUser(userId string) (flag bool) { Where("tur.user_id = ?", userId). Where("tr.is_del IS FALSE"). Take(&codes) - return slices.Contains(codes, "admin") + return }