-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathrole_logic.go
437 lines (375 loc) · 13.2 KB
/
role_logic.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package logic
import (
"fmt"
"github.com/eryajf/go-ldap-admin/model"
"github.com/eryajf/go-ldap-admin/model/request"
"github.com/eryajf/go-ldap-admin/model/response"
"github.com/eryajf/go-ldap-admin/public/common"
"github.com/eryajf/go-ldap-admin/public/tools"
"github.com/eryajf/go-ldap-admin/service/isql"
"github.com/gin-gonic/gin"
"github.com/thoas/go-funk"
)
type RoleLogic struct{}
// Add 添加数据
func (l RoleLogic) Add(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleAddReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
if isql.Role.Exist(tools.H{"name": r.Name}) {
return nil, tools.NewValidatorError(fmt.Errorf("该角色名已存在"))
}
// 获取当前用户最高角色等级
minSort, ctxUser, err := isql.User.GetCurrentUserMinRoleSort(c)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户最高角色等级失败: %s", err.Error()))
}
if minSort != 1 {
return nil, tools.NewValidatorError(fmt.Errorf("当前用户没有权限更新角色"))
}
// 用户不能创建比自己等级高或相同等级的角色
if minSort >= r.Sort {
return nil, tools.NewValidatorError(fmt.Errorf("不能创建比自己等级高或相同等级的角色"))
}
role := model.Role{
Name: r.Name,
Keyword: r.Keyword,
Remark: r.Remark,
Status: r.Status,
Sort: r.Sort,
Creator: ctxUser.Username,
}
// 创建角色
err = isql.Role.Add(&role)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("创建角色失败: %s", err.Error()))
}
return nil, nil
}
// List 数据列表
func (l RoleLogic) List(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleListReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
// 获取数据列表
roles, err := isql.Role.List(r)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取菜单列表失败: %s", err.Error()))
}
count, err := isql.Role.Count()
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取接口总数失败"))
}
rets := make([]model.Role, 0)
for _, role := range roles {
rets = append(rets, *role)
}
return response.RoleListRsp{
Total: count,
Roles: rets,
}, nil
}
// Update 更新数据
func (l RoleLogic) Update(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleUpdateReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
filter := tools.H{"id": r.ID}
if !isql.Role.Exist(filter) {
return nil, tools.NewValidatorError(fmt.Errorf("该角色名不已存在"))
}
// 当前用户角色排序最小值(最高等级角色)以及当前用户
minSort, ctxUser, err := isql.User.GetCurrentUserMinRoleSort(c)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户最高角色等级失败: %s", err.Error()))
}
if minSort != 1 {
return nil, tools.NewValidatorError(fmt.Errorf("当前用户没有权限更新角色"))
}
// 不能更新比自己角色等级高或相等的角色
// 根据path中的角色ID获取该角色信息
roles, _ := isql.Role.GetRolesByIds([]uint{r.ID})
if len(roles) == 0 {
return nil, tools.NewMySqlError(fmt.Errorf("获取角色信息失败: %s", err.Error()))
}
if minSort >= roles[0].Sort {
return nil, tools.NewValidatorError(fmt.Errorf("不能更新比自己角色等级高或相等的角色"))
}
// 不能把角色等级更新得比当前用户的等级高
if minSort >= r.Sort {
return nil, tools.NewValidatorError(fmt.Errorf("不能把角色等级更新得比当前用户的等级高或相同"))
}
oldData := new(model.Role)
err = isql.Role.Find(filter, oldData)
if err != nil {
return nil, tools.NewMySqlError(err)
}
role := model.Role{
Model: oldData.Model,
Name: r.Name,
Keyword: r.Keyword,
Remark: r.Remark,
Status: r.Status,
Sort: r.Sort,
Creator: ctxUser.Username,
}
// 更新角色
err = isql.Role.Update(&role)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("更新角色失败: %s", err.Error()))
}
// 如果更新成功,且更新了角色的keyword, 则更新casbin中policy
if r.Keyword != roles[0].Keyword {
// 获取policy
rolePolicies := common.CasbinEnforcer.GetFilteredPolicy(0, roles[0].Keyword)
if len(rolePolicies) == 0 {
return
}
rolePoliciesCopy := make([][]string, 0)
// 替换keyword
for _, policy := range rolePolicies {
policyCopy := make([]string, len(policy))
copy(policyCopy, policy)
rolePoliciesCopy = append(rolePoliciesCopy, policyCopy)
policy[0] = r.Keyword
}
//gormadapter未实现UpdatePolicies方法,等gorm更新---
//isUpdated, _ := common.CasbinEnforcer.UpdatePolicies(rolePoliciesCopy, rolePolicies)
//if !isUpdated {
// response.Fail(c, nil, "更新角色成功,但角色关键字关联的权限接口更新失败!")
// return
//}
// 这里需要先新增再删除(先删除再增加会出错)
isAdded, _ := common.CasbinEnforcer.AddPolicies(rolePolicies)
if !isAdded {
return nil, tools.NewOperationError(fmt.Errorf("更新角色成功,但角色关键字关联的权限接口更新失败"))
}
isRemoved, _ := common.CasbinEnforcer.RemovePolicies(rolePoliciesCopy)
if !isRemoved {
return nil, tools.NewOperationError(fmt.Errorf("更新角色成功,但角色关键字关联的权限接口更新失败"))
}
err := common.CasbinEnforcer.LoadPolicy()
if err != nil {
return nil, tools.NewOperationError(fmt.Errorf("更新角色成功,但角色关键字关联角色的权限接口策略加载失败"))
}
}
// 更新角色成功处理用户信息缓存有两种做法:(这里使用第二种方法,因为一个角色下用户数量可能很多,第二种方法可以分散数据库压力)
// 1.可以帮助用户更新拥有该角色的用户信息缓存,使用下面方法
// err = ur.UpdateUserInfoCacheByRoleId(uint(roleId))
// 2.直接清理缓存,让活跃的用户自己重新缓存最新用户信息
isql.User.ClearUserInfoCache()
return nil, nil
}
// Delete 删除数据
func (l RoleLogic) Delete(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleDeleteReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
// 获取当前登陆用户最高等级角色
minSort, _, err := isql.User.GetCurrentUserMinRoleSort(c)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户最高角色等级失败: %s", err.Error()))
}
// 获取角色信息
roles, _ := isql.Role.GetRolesByIds(r.RoleIds)
if len(roles) == 0 {
return nil, tools.NewMySqlError(fmt.Errorf("未能获取到角色信息"))
}
// 不能删除比自己角色等级高或相等的角色
for _, role := range roles {
if minSort >= role.Sort {
return nil, tools.NewValidatorError(fmt.Errorf("不能删除比自己角色等级高或相等的角色"))
}
}
// 删除角色
err = isql.Role.Delete(r.RoleIds)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("删除角色失败: %s", err.Error()))
}
// 删除角色成功直接清理缓存,让活跃的用户自己重新缓存最新用户信息
isql.User.ClearUserInfoCache()
return nil, nil
}
// GetMenuList 获取角色菜单列表
func (l RoleLogic) GetMenuList(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleGetMenuListReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
menus, err := isql.Role.GetRoleMenusById(r.RoleID)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取角色的权限菜单失败: " + err.Error()))
}
return menus, nil
}
// GetApiList 获取角色接口列表
func (l RoleLogic) GetApiList(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleGetApiListReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
role := new(model.Role)
err := isql.Role.Find(tools.H{"id": r.RoleID}, role)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取资源失败: " + err.Error()))
}
policies := common.CasbinEnforcer.GetFilteredPolicy(0, role.Keyword)
apis, err := isql.Api.ListAll()
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取资源列表失败: " + err.Error()))
}
accessApis := make([]*model.Api, 0)
for _, policy := range policies {
path := policy[1]
method := policy[2]
for _, api := range apis {
if path == api.Path && method == api.Method {
accessApis = append(accessApis, api)
break
}
}
}
return accessApis, nil
}
// UpdateMenus 更新角色菜单
func (l RoleLogic) UpdateMenus(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleUpdateMenusReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
roles, _ := isql.Role.GetRolesByIds([]uint{r.RoleID})
if len(roles) == 0 {
return nil, tools.NewMySqlError(fmt.Errorf("未获取到角色信息"))
}
// 当前用户角色排序最小值(最高等级角色)以及当前用户
minSort, ctxUser, err := isql.User.GetCurrentUserMinRoleSort(c)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户最高角色等级失败: %s", err.Error()))
}
// (非管理员)不能更新比自己角色等级高或相等角色的权限菜单
if minSort != 1 {
if minSort >= roles[0].Sort {
return nil, tools.NewValidatorError(fmt.Errorf("不能更新比自己角色等级高或相等角色的权限菜单"))
}
}
// 获取当前用户所拥有的权限菜单
ctxUserMenus, err := isql.Menu.GetUserMenusByUserId(ctxUser.ID)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户的可访问菜单列表失败: " + err.Error()))
}
// 获取当前用户所拥有的权限菜单ID
ctxUserMenusIds := make([]uint, 0)
for _, menu := range ctxUserMenus {
ctxUserMenusIds = append(ctxUserMenusIds, menu.ID)
}
// 用户需要修改的菜单集合
reqMenus := make([]*model.Menu, 0)
// (非管理员)不能把角色的权限菜单设置的比当前用户所拥有的权限菜单多
if minSort != 1 {
for _, id := range r.MenuIds {
if !funk.Contains(ctxUserMenusIds, id) {
return nil, tools.NewValidatorError(fmt.Errorf("无权设置ID为%d的菜单", id))
}
}
for _, id := range r.MenuIds {
for _, menu := range ctxUserMenus {
if id == menu.ID {
reqMenus = append(reqMenus, menu)
break
}
}
}
} else {
// 管理员随意设置
// 根据menuIds查询查询菜单
menus, err := isql.Menu.List()
if err != nil {
return nil, tools.NewValidatorError(fmt.Errorf("获取菜单列表失败: " + err.Error()))
}
for _, menuId := range r.MenuIds {
for _, menu := range menus {
if menuId == menu.ID {
reqMenus = append(reqMenus, menu)
}
}
}
}
roles[0].Menus = reqMenus
err = isql.Role.UpdateRoleMenus(roles[0])
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("更新角色的权限菜单失败: " + err.Error()))
}
return nil, nil
}
// UpdateApis 更新角色接口
func (l RoleLogic) UpdateApis(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.RoleUpdateApisReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
// 根据path中的角色ID获取该角色信息
roles, _ := isql.Role.GetRolesByIds([]uint{r.RoleID})
if len(roles) == 0 {
return nil, tools.NewMySqlError(fmt.Errorf("未获取到角色信息"))
}
// 当前用户角色排序最小值(最高等级角色)以及当前用户
minSort, ctxUser, err := isql.User.GetCurrentUserMinRoleSort(c)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户最高角色等级失败: %s", err.Error()))
}
// (非管理员)不能更新比自己角色等级高或相等角色的权限菜单
if minSort != 1 {
if minSort >= roles[0].Sort {
return nil, tools.NewValidatorError(fmt.Errorf("不能更新比自己角色等级高或相等角色的权限菜单"))
}
}
// 获取当前用户所拥有的权限接口
ctxRoles := ctxUser.Roles
ctxRolesPolicies := make([][]string, 0)
for _, role := range ctxRoles {
policy := common.CasbinEnforcer.GetFilteredPolicy(0, role.Keyword)
ctxRolesPolicies = append(ctxRolesPolicies, policy...)
}
// 得到path中的角色ID对应角色能够设置的权限接口集合
for _, policy := range ctxRolesPolicies {
policy[0] = roles[0].Keyword
}
// 根据apiID获取接口详情
apis, err := isql.Api.GetApisById(r.ApiIds)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("根据接口ID获取接口信息失败"))
}
// 生成前端想要设置的角色policies
reqRolePolicies := make([][]string, 0)
for _, api := range apis {
reqRolePolicies = append(reqRolePolicies, []string{
roles[0].Keyword, api.Path, api.Method,
})
}
// (非管理员)不能把角色的权限接口设置的比当前用户所拥有的权限接口多
if minSort != 1 {
for _, reqPolicy := range reqRolePolicies {
if !funk.Contains(ctxRolesPolicies, reqPolicy) {
return nil, tools.NewValidatorError(fmt.Errorf("无权设置路径为%s,请求方式为%s的接口", reqPolicy[1], reqPolicy[2]))
}
}
}
// 更新角色的权限接口
err = isql.Role.UpdateRoleApis(roles[0].Keyword, reqRolePolicies)
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("更新角色的权限接口失败"))
}
return nil, nil
}