登录权限返回修改
This commit is contained in:
@@ -2,6 +2,7 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
@@ -92,10 +93,12 @@ func (s *Service) Login(ctx context.Context, req *dto.LoginRequest, clientIP str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
permissions, err := s.getUserPermissions(ctx, account.ID)
|
||||
permissions, menus, buttons, err := s.getUserPermissionsAndMenus(ctx, account.ID, account.UserType, device)
|
||||
if err != nil {
|
||||
s.logger.Error("查询用户权限失败", zap.Uint("user_id", account.ID), zap.Error(err))
|
||||
permissions = []string{}
|
||||
menus = []dto.MenuNode{}
|
||||
buttons = []string{}
|
||||
}
|
||||
|
||||
userInfo := s.buildUserInfo(account)
|
||||
@@ -113,6 +116,8 @@ func (s *Service) Login(ctx context.Context, req *dto.LoginRequest, clientIP str
|
||||
ExpiresIn: int64(constants.DefaultAccessTokenTTL.Seconds()),
|
||||
User: userInfo,
|
||||
Permissions: permissions,
|
||||
Menus: menus,
|
||||
Buttons: buttons,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -258,3 +263,135 @@ func (s *Service) getUserTypeName(userType int) string {
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) getUserPermissionsAndMenus(ctx context.Context, userID uint, userType int, device string) ([]string, []dto.MenuNode, []string, error) {
|
||||
if userType == constants.UserTypeSuperAdmin {
|
||||
return s.getAllPermissionsForSuperAdmin(ctx, device)
|
||||
}
|
||||
|
||||
accountRoles, err := s.accountRoleStore.GetByAccountID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(errors.CodeInternalError, err, "查询用户角色失败")
|
||||
}
|
||||
|
||||
if len(accountRoles) == 0 {
|
||||
return []string{}, []dto.MenuNode{}, []string{}, nil
|
||||
}
|
||||
|
||||
roleIDs := make([]uint, 0, len(accountRoles))
|
||||
for _, ar := range accountRoles {
|
||||
roleIDs = append(roleIDs, ar.RoleID)
|
||||
}
|
||||
|
||||
permIDs, err := s.rolePermStore.GetPermIDsByRoleIDs(ctx, roleIDs)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(errors.CodeInternalError, err, "查询角色权限失败")
|
||||
}
|
||||
|
||||
if len(permIDs) == 0 {
|
||||
return []string{}, []dto.MenuNode{}, []string{}, nil
|
||||
}
|
||||
|
||||
permissions, err := s.permissionStore.GetByIDs(ctx, permIDs)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(errors.CodeInternalError, err, "查询权限详情失败")
|
||||
}
|
||||
|
||||
return s.classifyPermissions(permissions, device)
|
||||
}
|
||||
|
||||
func (s *Service) getAllPermissionsForSuperAdmin(ctx context.Context, device string) ([]string, []dto.MenuNode, []string, error) {
|
||||
permissions, err := s.permissionStore.GetAll(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(errors.CodeInternalError, err, "查询所有权限失败")
|
||||
}
|
||||
|
||||
return s.classifyPermissions(permissions, device)
|
||||
}
|
||||
|
||||
func (s *Service) classifyPermissions(permissions []*model.Permission, device string) ([]string, []dto.MenuNode, []string, error) {
|
||||
var menuPerms []*model.Permission
|
||||
var buttonCodes []string
|
||||
var allCodes []string
|
||||
|
||||
for _, perm := range permissions {
|
||||
if perm.Status != constants.StatusEnabled {
|
||||
continue
|
||||
}
|
||||
|
||||
if perm.Platform != constants.PlatformAll && perm.Platform != device {
|
||||
continue
|
||||
}
|
||||
|
||||
allCodes = append(allCodes, perm.PermCode)
|
||||
|
||||
if perm.PermType == constants.PermissionTypeMenu {
|
||||
menuPerms = append(menuPerms, perm)
|
||||
} else if perm.PermType == constants.PermissionTypeButton {
|
||||
buttonCodes = append(buttonCodes, perm.PermCode)
|
||||
}
|
||||
}
|
||||
|
||||
menuTree := s.buildMenuTree(menuPerms)
|
||||
|
||||
return allCodes, menuTree, buttonCodes, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildMenuTree(permissions []*model.Permission) []dto.MenuNode {
|
||||
if len(permissions) == 0 {
|
||||
return []dto.MenuNode{}
|
||||
}
|
||||
|
||||
permMap := make(map[uint]*model.Permission)
|
||||
for _, p := range permissions {
|
||||
permMap[p.ID] = p
|
||||
}
|
||||
|
||||
var roots []dto.MenuNode
|
||||
for _, p := range permissions {
|
||||
if p.ParentID == nil || *p.ParentID == 0 {
|
||||
roots = append(roots, s.buildNode(p, permMap))
|
||||
} else if _, ok := permMap[*p.ParentID]; !ok {
|
||||
s.logger.Warn("检测到孤儿节点",
|
||||
zap.Uint("child_id", p.ID),
|
||||
zap.String("perm_code", p.PermCode),
|
||||
zap.Uint("parent_id", *p.ParentID),
|
||||
)
|
||||
roots = append(roots, s.buildNode(p, permMap))
|
||||
}
|
||||
}
|
||||
|
||||
s.sortMenuNodes(roots)
|
||||
return roots
|
||||
}
|
||||
|
||||
func (s *Service) buildNode(perm *model.Permission, permMap map[uint]*model.Permission) dto.MenuNode {
|
||||
node := dto.MenuNode{
|
||||
ID: perm.ID,
|
||||
PermCode: perm.PermCode,
|
||||
Name: perm.PermName,
|
||||
URL: perm.URL,
|
||||
Sort: perm.Sort,
|
||||
Children: []dto.MenuNode{},
|
||||
}
|
||||
|
||||
for _, p := range permMap {
|
||||
if p.ParentID != nil && *p.ParentID == perm.ID {
|
||||
node.Children = append(node.Children, s.buildNode(p, permMap))
|
||||
}
|
||||
}
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
func (s *Service) sortMenuNodes(nodes []dto.MenuNode) {
|
||||
sort.Slice(nodes, func(i, j int) bool {
|
||||
return nodes[i].Sort < nodes[j].Sort
|
||||
})
|
||||
|
||||
for i := range nodes {
|
||||
if len(nodes[i].Children) > 0 {
|
||||
s.sortMenuNodes(nodes[i].Children)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user