Skip to content

Commit 280960c

Browse files
authored
feat(user-db): enhance user management with role-based queries (allow-edit-role-guest) (#9234)
- Add `GetUsersByRole` function to fetch users based on their roles. - Extend `UpdateUserBasePathPrefix` to accept optional user lists. - Ensure path cleaning in `UpdateUserBasePathPrefix` for consistency. - Integrate guest role fetching in `auth.go` middleware. - Utilize `GetUsersByRole` in `role.go` for base path modifications. - Remove redundant line in `role.go` role modification logic.
1 parent 74332e9 commit 280960c

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

internal/db/user.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pkg/errors"
1010
"gorm.io/gorm"
1111
"path"
12+
"slices"
1213
"strings"
1314
)
1415

@@ -25,6 +26,20 @@ func GetUserByRole(role int) (*model.User, error) {
2526
return nil, gorm.ErrRecordNotFound
2627
}
2728

29+
func GetUsersByRole(roleID int) ([]model.User, error) {
30+
var users []model.User
31+
if err := db.Find(&users).Error; err != nil {
32+
return nil, err
33+
}
34+
var result []model.User
35+
for _, u := range users {
36+
if slices.Contains(u.Role, roleID) {
37+
result = append(result, u)
38+
}
39+
}
40+
return result, nil
41+
}
42+
2843
func GetUserByName(username string) (*model.User, error) {
2944
user := model.User{Username: username}
3045
if err := db.Where(user).First(&user).Error; err != nil {
@@ -109,25 +124,29 @@ func RemoveAuthn(u *model.User, id string) error {
109124
return UpdateAuthn(u.ID, string(res))
110125
}
111126

112-
func UpdateUserBasePathPrefix(oldPath, newPath string) ([]string, error) {
127+
func UpdateUserBasePathPrefix(oldPath, newPath string, usersOpt ...[]model.User) ([]string, error) {
113128
var users []model.User
114129
var modifiedUsernames []string
115130

116-
if err := db.Find(&users).Error; err != nil {
117-
return nil, errors.WithMessage(err, "failed to load users")
118-
}
119-
120131
oldPathClean := path.Clean(oldPath)
121132

133+
if len(usersOpt) > 0 {
134+
users = usersOpt[0]
135+
} else {
136+
if err := db.Find(&users).Error; err != nil {
137+
return nil, errors.WithMessage(err, "failed to load users")
138+
}
139+
}
140+
122141
for _, user := range users {
123142
basePath := path.Clean(user.BasePath)
124143
updated := false
125144

126145
if basePath == oldPathClean {
127-
user.BasePath = newPath
146+
user.BasePath = path.Clean(newPath)
128147
updated = true
129148
} else if strings.HasPrefix(basePath, oldPathClean+"/") {
130-
user.BasePath = newPath + basePath[len(oldPathClean):]
149+
user.BasePath = path.Clean(newPath + basePath[len(oldPathClean):])
131150
updated = true
132151
}
133152

internal/op/role.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ func UpdateRole(r *model.Role) error {
100100
switch old.Name {
101101
case "admin":
102102
return errs.ErrChangeDefaultRole
103-
104103
case "guest":
105104
r.Name = "guest"
106105
}
@@ -112,7 +111,13 @@ func UpdateRole(r *model.Role) error {
112111

113112
oldPath := old.PermissionScopes[0].Path
114113
newPath := r.PermissionScopes[0].Path
115-
modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldPath, newPath)
114+
115+
users, err := db.GetUsersByRole(int(r.ID))
116+
if err != nil {
117+
return errors.WithMessage(err, "failed to get users by role")
118+
}
119+
120+
modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldPath, newPath, users)
116121
if err != nil {
117122
return errors.WithMessage(err, "failed to update user base path when role updated")
118123
}

server/middlewares/auth.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func Auth(c *gin.Context) {
4141
c.Abort()
4242
return
4343
}
44+
if len(guest.Role) > 0 {
45+
roles, err := op.GetRolesByUserID(guest.ID)
46+
if err != nil {
47+
common.ErrorStrResp(c, fmt.Sprintf("Fail to load guest roles: %v", err), 500)
48+
c.Abort()
49+
return
50+
}
51+
guest.RolesDetail = roles
52+
}
4453
c.Set("user", guest)
4554
log.Debugf("use empty token: %+v", guest)
4655
c.Next()

0 commit comments

Comments
 (0)