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++ }