Skip to content

Commit 35fa2d7

Browse files
committed
fixed: 修复跨级操作角色权限的越权问题
1 parent a9076d7 commit 35fa2d7

File tree

5 files changed

+78
-185
lines changed

5 files changed

+78
-185
lines changed

server/api/v1/system/sys_user.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ func (b *BaseApi) SetUserAuthorities(c *gin.Context) {
285285
response.FailWithMessage(err.Error(), c)
286286
return
287287
}
288-
err = userService.SetUserAuthorities(sua.ID, sua.AuthorityIds)
288+
authorityID := utils.GetUserAuthorityId(c)
289+
err = userService.SetUserAuthorities(authorityID, sua.ID, sua.AuthorityIds)
289290
if err != nil {
290291
global.GVA_LOG.Error("修改失败!", zap.Error(err))
291292
response.FailWithMessage("修改失败", c)
@@ -350,9 +351,9 @@ func (b *BaseApi) SetUserInfo(c *gin.Context) {
350351
response.FailWithMessage(err.Error(), c)
351352
return
352353
}
353-
354354
if len(user.AuthorityIds) != 0 {
355-
err = userService.SetUserAuthorities(user.ID, user.AuthorityIds)
355+
authorityID := utils.GetUserAuthorityId(c)
356+
err = userService.SetUserAuthorities(authorityID, user.ID, user.AuthorityIds)
356357
if err != nil {
357358
global.GVA_LOG.Error("设置失败!", zap.Error(err))
358359
response.FailWithMessage("设置失败", c)

server/service/system/sys_authority.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (authorityService *AuthorityService) DeleteAuthority(auth *system.SysAuthor
183183
//@param: info request.PageInfo
184184
//@return: list interface{}, total int64, err error
185185

186-
func (authorityService *AuthorityService) GetAuthorityInfoList(authorityID uint) (list any, err error) {
186+
func (authorityService *AuthorityService) GetAuthorityInfoList(authorityID uint) (list []system.SysAuthority, err error) {
187187
var authority system.SysAuthority
188188
err = global.GVA_DB.Where("authority_id = ?", authorityID).First(&authority).Error
189189
if err != nil {
@@ -210,6 +210,24 @@ func (authorityService *AuthorityService) GetAuthorityInfoList(authorityID uint)
210210
return authorities, err
211211
}
212212

213+
//@author: [piexlmax](https://github.com/piexlmax)
214+
//@function: GetAuthorityInfoList
215+
//@description: 分页获取数据
216+
//@param: info request.PageInfo
217+
//@return: list interface{}, total int64, err error
218+
219+
func (authorityService *AuthorityService) GetStructAuthorityList(authorityID uint) (list []uint, err error) {
220+
var authorities []system.SysAuthority
221+
err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authorityID).Find(&authorities).Error
222+
if len(authorities) > 0 {
223+
for k := range authorities {
224+
list = append(list, authorities[k].AuthorityId)
225+
_, err = authorityService.GetStructAuthorityList(authorities[k].AuthorityId)
226+
}
227+
}
228+
return list, err
229+
}
230+
213231
//@author: [piexlmax](https://github.com/piexlmax)
214232
//@function: GetAuthorityInfo
215233
//@description: 获取所有角色信息
@@ -242,6 +260,22 @@ func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthor
242260

243261
func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
244262
var s system.SysAuthority
263+
if global.GVA_CONFIG.System.UseStrictAuth {
264+
authids, err := authorityService.GetStructAuthorityList(auth.AuthorityId)
265+
if err != nil {
266+
return err
267+
}
268+
hasAuth := false
269+
for _, v := range authids {
270+
if v == auth.AuthorityId {
271+
hasAuth = true
272+
break
273+
}
274+
}
275+
if !hasAuth {
276+
return errors.New("您提交的角色ID不合法")
277+
}
278+
}
245279
global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
246280
err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
247281
return err

server/service/system/sys_casbin.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ type CasbinService struct{}
2727
var CasbinServiceApp = new(CasbinService)
2828

2929
func (casbinService *CasbinService) UpdateCasbin(AuthorityID uint, casbinInfos []request.CasbinInfo) error {
30+
31+
if global.GVA_CONFIG.System.UseStrictAuth {
32+
authids, err := AuthorityServiceApp.GetStructAuthorityList(AuthorityID)
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+
}
46+
}
47+
3048
authorityId := strconv.Itoa(int(AuthorityID))
3149
casbinService.ClearCasbin(0, authorityId)
3250
rules := [][]string{}

server/service/system/sys_user.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (userService *UserService) SetUserAuthority(id uint, authorityId uint) (err
152152
//@param: id uint, authorityIds []string
153153
//@return: err error
154154

155-
func (userService *UserService) SetUserAuthorities(id uint, authorityIds []uint) (err error) {
155+
func (userService *UserService) SetUserAuthorities(adminAuthorityID, id uint, authorityIds []uint) (err error) {
156156
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
157157
var user system.SysUser
158158
TxErr := tx.Where("id = ?", id).First(&user).Error
@@ -164,8 +164,28 @@ func (userService *UserService) SetUserAuthorities(id uint, authorityIds []uint)
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+
167175
var useAuthority []system.SysUserAuthority
168176
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+
}
188+
}
169189
useAuthority = append(useAuthority, system.SysUserAuthority{
170190
SysUserId: id, SysAuthorityAuthorityId: v,
171191
})

server/utils/injection_code.go

Lines changed: 0 additions & 180 deletions
This file was deleted.

0 commit comments

Comments
 (0)