From f5a21066342c807bc75da3a763b5b5154a48ffc0 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Fri, 1 Sep 2023 17:44:49 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=88=97=E8=A1=A8=E4=B8=AD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=BB=98=E8=AE=A4=E7=94=A8=E6=88=B7=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dto/auth.go | 4 ++++ service/user.go | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/dto/auth.go b/dto/auth.go index 7944629..efc1b93 100644 --- a/dto/auth.go +++ b/dto/auth.go @@ -59,6 +59,10 @@ func IsSystemRole(role int32) bool { return role == int32(ADMIN) || role == int32(USER) } +func GetDefaultAuthRole() *AuthRoleRspDto { + return &AuthRoleRspDto{Id: int32(USER), Name: "普通用户"} +} + type AuthUserStorageDto struct { UID int32 `json:"uid" form:"uid"` IsAdmin bool `json:"isAdmin" form:"isAdmin"` diff --git a/service/user.go b/service/user.go index 28bc3db..e43385e 100644 --- a/service/user.go +++ b/service/user.go @@ -54,13 +54,15 @@ func FindUserInfo(userId int32) *dto.UserRspDto { } rspUser := dto.ConvertFromUserDto(user) roles := QueryAuthRoleByUid(user.ID) - if len(roles) == 0 { // 如果没有指派用户时,指定普通用户信息 - roles = []*model.AuthRole{{ID: int32(dto.USER), Name: "普通用户"}} - } rids := make([]int32, len(roles)) - for i, r := range roles { - rids[i] = r.ID - rspUser.Roles = append(rspUser.Roles, &dto.AuthRoleRspDto{Id: r.ID, Name: r.Name}) + if len(roles) == 0 { // 如果没有指派用户时,指定普通用户信息 + rspUser.Roles = append(rspUser.Roles, dto.GetDefaultAuthRole()) + rids = append(rids, int32(dto.USER)) + } else { + for i, r := range roles { + rids[i] = r.ID + rspUser.Roles = append(rspUser.Roles, &dto.AuthRoleRspDto{Id: r.ID, Name: r.Name}) + } } // 查询权限路径 rspUser.Paths = QueryAuthApiPathByRids(rids) @@ -110,6 +112,9 @@ func linkUserRole(users []*model.User) []*dto.UserRspDto { userArr := make([]*dto.UserRspDto, un) i := 0 for _, u := range userMap { + if len(u.Roles) == 0 { + u.Roles = append(u.Roles, dto.GetDefaultAuthRole()) + } userArr[i] = u i++ }