pg_role_access_menus_service.go
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package domainService
import (
"fmt"
"github.com/emirpasic/gods/sets/hashset"
"github.com/linmadan/egglib-go/core/application"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
"strconv"
)
// 角色有权限的菜单
type PgRoleAccessMenusService struct {
transactionContext *pgTransaction.TransactionContext
}
// DataAuth 数据权限验证
//
// options 数据参数
// data 需要验证权限的数据
func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, roleId []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
var err error
var menus []*domain.Menu
menuIdSet := hashset.New()
if len(roleId) == 0 {
return menus, nil
}
// 1.角色有权限的菜单列表
roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
for i := range roleId {
var role *domain.Role
if role, _ = roleRepository.FindOne(map[string]interface{}{"roleId": roleId[i]}); role == nil {
continue
}
// 只要当前登录组织的有权限菜单
if option.OrgId > 0 && option.OrgId != role.OrgId {
continue
}
for i := 0; i < len(role.AccessMenus); i++ {
menuIdSet.Add(role.AccessMenus[i])
}
}
// 2.获取所有公开的菜单
menuRepository, _ := repository.NewMenuRepository(ptr.transactionContext)
if err != nil {
return nil, err
}
queryOptions := make(map[string]interface{})
queryOptions["isPublish"] = domain.MenuPublic
if len(option.MenuCategory) > 0 {
queryOptions["code"] = ""
if m, e := menuRepository.FindOne(map[string]interface{}{"code": option.MenuCategory}); e == nil && m != nil {
queryOptions["category"] = strconv.Itoa(int(m.MenuId))
}
}
_, menus, err = menuRepository.Find(queryOptions)
if err != nil {
return nil, err
}
// 3.获取自定义菜单
customizeMenuRepository, _ := repository.NewCustomizeMenuRepository(ptr.transactionContext)
if err != nil {
return nil, err
}
_, alias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 4. 设置别名、返回有效的菜单列表
var mapMenus = make(map[int64]*domain.Menu)
for i := range menus {
menus[i].EnableStatus = domain.MenuStatusDisable
mapMenus[menus[i].MenuId] = menus[i]
}
for i := range alias {
if v, ok := mapMenus[alias[i].MenuId]; ok {
v.MenuName = alias[i].MenuAlias // 设置别名
v.Sort = alias[i].Sort
}
}
values := menuIdSet.Values()
for i := range values {
menuId := (values[i]).(int64) // 设置菜单权限状态
if v, ok := mapMenus[menuId]; ok {
v.EnableStatus = domain.MenuStatusEnable
}
}
// 5.返回数据
// 返回所有菜单
if option.ALLDisableMenu == 1 {
return menus, nil
}
// 返回有权限的菜单
var enableMenus []*domain.Menu
for i := range menus {
if menus[i].EnableStatus == domain.MenuStatusEnable {
enableMenus = append(enableMenus, menus[i])
}
}
return enableMenus, nil
}
func NewPgRoleAccessMenusService(transactionContext *pgTransaction.TransactionContext) (*PgRoleAccessMenusService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgRoleAccessMenusService{
transactionContext: transactionContext,
}, nil
}
}