rbac.go
4.5 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package rbac
import (
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/rbac"
)
type RbacService struct {
}
func (svr *RbacService) Access(header *protocol.RequestHeader, request *protocolx.AccessRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.AccessResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
var access []*domain.Access
if _, access, err = AccessRepository.Find(map[string]interface{}{"sortByParentId": "ASC", "sortBySort": "ASC"}); err != nil {
return
}
var rspList []interface{}
for _, item := range access {
rspList = append(rspList, map[string]interface{}{
"id": item.Id,
"name": item.AccessName,
"icon": "",
"parentId": item.ParentId,
"sort": item.Sort,
"code": item.AccessCode,
})
}
rsp = map[string]interface{}{
"lists": rspList,
}
err = transactionContext.CommitTransaction()
return
}
func (svr *RbacService) RoleAccess(header *protocol.RequestHeader, request *protocolx.RoleAccessRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
RoleAccessDao, _ = dao.NewRoleAccessDao(transactionContext)
RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
role *domain.Role
)
rsp = &protocolx.RoleAccessResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
if role, err = RoleRepository.FindOne(map[string]interface{}{"id": request.RoleId}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, "角色不存在")
return
}
accessIds, _ := RoleAccessDao.GetRoleAccess(request.RoleId)
rsp = map[string]interface{}{
"roleId": role.Id,
"roleName": role.RoleName,
"accessIds": accessIds,
}
err = transactionContext.CommitTransaction()
return
}
func (svr *RbacService) SetRoleAccess(header *protocol.RequestHeader, request *protocolx.SetRoleAccessRequest) (rsp *protocolx.SetRoleAccessResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
RoleAccessDao, _ = dao.NewRoleAccessDao(transactionContext)
)
rsp = &protocolx.SetRoleAccessResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
if _, err = RoleRepository.FindOne(map[string]interface{}{"id": request.RoleId}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, "角色不存在")
return
}
var accessMap = make(map[int64]*domain.Access)
_, access, e := AccessRepository.Find(map[string]interface{}{"inAccessIds": request.AccessIds})
if e != nil {
err = e
return
}
for _, v := range access {
accessMap[v.Id] = v
}
if err = RoleAccessDao.DeleteRoleAccess(request.RoleId); err != nil {
return
}
if len(request.AccessIds) > 0 {
var roleAccess []*domain.RoleAccess
for _, v := range request.AccessIds {
item := &domain.RoleAccess{
RoleId: request.RoleId,
AccessId: v,
}
if accessItem, ok := accessMap[v]; ok {
item.Object = accessItem.Object
item.Action = accessItem.Action
item.Option = accessItem.AccessCode
}
roleAccess = append(roleAccess, item)
}
if err = RoleAccessDao.SaveRoleAccess(roleAccess); err != nil {
return
}
}
err = transactionContext.CommitTransaction()
return
}
func NewRbacService(options map[string]interface{}) *RbacService {
svr := &RbacService{}
return svr
}