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 }