Skip to content

Commit a7b5b1e

Browse files
committed
feature: 优化严格模式角色鉴权操作。
1 parent e3b8caf commit a7b5b1e

File tree

5 files changed

+40
-61
lines changed

5 files changed

+40
-61
lines changed

server/service/system/sys_authority.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ func (authorityService *AuthorityService) GetStructAuthorityList(authorityID uin
233233
return list, err
234234
}
235235

236+
func (authorityService *AuthorityService) CheckAuthorityIDAuth(authorityID, targetID uint) (err error) {
237+
if !global.GVA_CONFIG.System.UseStrictAuth {
238+
return nil
239+
}
240+
authIDS, err := authorityService.GetStructAuthorityList(authorityID)
241+
if err != nil {
242+
return err
243+
}
244+
hasAuth := false
245+
for _, v := range authIDS {
246+
if v == targetID {
247+
hasAuth = true
248+
break
249+
}
250+
}
251+
if !hasAuth {
252+
return errors.New("您提交的角色ID不合法")
253+
}
254+
return nil
255+
}
256+
236257
//@author: [piexlmax](https://github.com/piexlmax)
237258
//@function: GetAuthorityInfo
238259
//@description: 获取所有角色信息
@@ -251,22 +272,19 @@ func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthor
251272
//@return: error
252273

253274
func (authorityService *AuthorityService) SetDataAuthority(adminAuthorityID uint, auth system.SysAuthority) error {
254-
if global.GVA_CONFIG.System.UseStrictAuth {
255-
authids, err := AuthorityServiceApp.GetStructAuthorityList(adminAuthorityID)
275+
var checkIDs []uint
276+
checkIDs = append(checkIDs, auth.AuthorityId)
277+
for i := range auth.DataAuthorityId {
278+
checkIDs = append(checkIDs, auth.DataAuthorityId[i].AuthorityId)
279+
}
280+
281+
for i := range checkIDs {
282+
err := authorityService.CheckAuthorityIDAuth(adminAuthorityID, checkIDs[i])
256283
if err != nil {
257284
return err
258285
}
259-
hasAuth := false
260-
for _, v := range authids {
261-
if v == auth.AuthorityId {
262-
hasAuth = true
263-
break
264-
}
265-
}
266-
if !hasAuth {
267-
return errors.New("您提交的角色ID不合法")
268-
}
269286
}
287+
270288
var s system.SysAuthority
271289
global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
272290
err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)

server/service/system/sys_casbin.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,9 @@ var CasbinServiceApp = new(CasbinService)
2828

2929
func (casbinService *CasbinService) UpdateCasbin(adminAuthorityID, AuthorityID uint, casbinInfos []request.CasbinInfo) error {
3030

31-
if global.GVA_CONFIG.System.UseStrictAuth {
32-
authids, err := AuthorityServiceApp.GetStructAuthorityList(adminAuthorityID)
33-
if err != nil {
34-
return err
35-
}
36-
hasAuth := false
37-
for _, v := range authids {
38-
if v == AuthorityID {
39-
hasAuth = true
40-
break
41-
}
42-
}
43-
if !hasAuth {
44-
return errors.New("您提交的角色ID不合法")
45-
}
31+
err := AuthorityServiceApp.CheckAuthorityIDAuth(adminAuthorityID, AuthorityID)
32+
if err != nil {
33+
return err
4634
}
4735

4836
authorityId := strconv.Itoa(int(AuthorityID))

server/service/system/sys_menu.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,9 @@ func (menuService *MenuService) AddMenuAuthority(menus []system.SysBaseMenu, adm
201201
auth.AuthorityId = authorityId
202202
auth.SysBaseMenus = menus
203203

204-
if global.GVA_CONFIG.System.UseStrictAuth {
205-
authids, err := AuthorityServiceApp.GetStructAuthorityList(adminAuthorityID)
206-
if err != nil {
207-
return err
208-
}
209-
hasAuth := false
210-
for _, v := range authids {
211-
if v == authorityId {
212-
hasAuth = true
213-
break
214-
}
215-
}
216-
if !hasAuth {
217-
return errors.New("您提交的角色ID不合法")
218-
}
204+
err = AuthorityServiceApp.CheckAuthorityIDAuth(adminAuthorityID, authorityId)
205+
if err != nil {
206+
return err
219207
}
220208

221209
err = AuthorityServiceApp.SetMenuAuthority(&auth)

server/service/system/sys_user.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,11 @@ func (userService *UserService) SetUserAuthorities(adminAuthorityID, id uint, au
164164
if TxErr != nil {
165165
return TxErr
166166
}
167-
var childrenIDS []uint
168-
if global.GVA_CONFIG.System.UseStrictAuth {
169-
childrenIDS, err = AuthorityServiceApp.GetStructAuthorityList(adminAuthorityID)
170-
if err != nil {
171-
return errors.New("获取当前角色可用角色失败")
172-
}
173-
}
174-
175167
var useAuthority []system.SysUserAuthority
176168
for _, v := range authorityIds {
177-
if global.GVA_CONFIG.System.UseStrictAuth {
178-
hasAuth := false
179-
for i := range childrenIDS {
180-
if childrenIDS[i] == v {
181-
hasAuth = true
182-
break
183-
}
184-
}
185-
if !hasAuth {
186-
return errors.New("您提交的角色ID不合法")
187-
}
169+
e := AuthorityServiceApp.CheckAuthorityIDAuth(adminAuthorityID, v)
170+
if e != nil {
171+
return e
188172
}
189173
useAuthority = append(useAuthority, system.SysUserAuthority{
190174
SysUserId: id, SysAuthorityAuthorityId: v,

web/src/view/superAdmin/authority/components/datas.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const authDataEnter = async() => {
131131
132132
// 选择
133133
const selectAuthority = () => {
134+
dataAuthorityId.value = dataAuthorityId.value.filter(item => item)
134135
emit('changeRow', 'dataAuthorityId', dataAuthorityId.value)
135136
needConfirm.value = true
136137
}

0 commit comments

Comments
 (0)