Skip to content

Commit 6b2d81e

Browse files
authored
feat(user): enhance path management and role handling (#9249)
- Add `GetUsersByRole` function for fetching users by role. - Introduce `GetAllBasePathsFromRoles` to aggregate paths from roles. - Refine path handling in `pkg/utils/path.go` for normalization. - Comment out base path prefix updates to simplify role operations.
1 parent 85fe4e5 commit 6b2d81e

File tree

5 files changed

+89
-40
lines changed

5 files changed

+89
-40
lines changed

internal/model/user.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,21 @@ func (u *User) JoinPath(reqPath string) (string, error) {
149149
if err != nil {
150150
return "", err
151151
}
152-
if u.CheckPathLimit() && !utils.IsSubPath(u.BasePath, path) {
153-
return "", errs.PermissionDenied
152+
153+
if u.CheckPathLimit() {
154+
basePaths := GetAllBasePathsFromRoles(u)
155+
match := false
156+
for _, base := range basePaths {
157+
if utils.IsSubPath(base, path) {
158+
match = true
159+
break
160+
}
161+
}
162+
if !match {
163+
return "", errs.PermissionDenied
164+
}
154165
}
166+
155167
return path, nil
156168
}
157169

@@ -193,3 +205,22 @@ func (u *User) WebAuthnCredentials() []webauthn.Credential {
193205
func (u *User) WebAuthnIcon() string {
194206
return "https://alistgo.com/logo.svg"
195207
}
208+
209+
// GetAllBasePathsFromRoles returns all permission paths from user's roles
210+
func GetAllBasePathsFromRoles(u *User) []string {
211+
basePaths := make([]string, 0)
212+
seen := make(map[string]struct{})
213+
214+
for _, role := range u.RolesDetail {
215+
for _, entry := range role.PermissionScopes {
216+
if entry.Path == "" {
217+
continue
218+
}
219+
if _, ok := seen[entry.Path]; !ok {
220+
basePaths = append(basePaths, entry.Path)
221+
seen[entry.Path] = struct{}{}
222+
}
223+
}
224+
}
225+
return basePaths
226+
}

internal/op/role.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package op
22

33
import (
44
"fmt"
5-
"github.com/pkg/errors"
65
"time"
76

87
"github.com/Xhofe/go-cache"
@@ -106,26 +105,26 @@ func UpdateRole(r *model.Role) error {
106105
for i := range r.PermissionScopes {
107106
r.PermissionScopes[i].Path = utils.FixAndCleanPath(r.PermissionScopes[i].Path)
108107
}
109-
if len(old.PermissionScopes) > 0 && len(r.PermissionScopes) > 0 &&
110-
old.PermissionScopes[0].Path != r.PermissionScopes[0].Path {
111-
112-
oldPath := old.PermissionScopes[0].Path
113-
newPath := r.PermissionScopes[0].Path
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)
121-
if err != nil {
122-
return errors.WithMessage(err, "failed to update user base path when role updated")
123-
}
124-
125-
for _, name := range modifiedUsernames {
126-
userCache.Del(name)
127-
}
128-
}
108+
//if len(old.PermissionScopes) > 0 && len(r.PermissionScopes) > 0 &&
109+
// old.PermissionScopes[0].Path != r.PermissionScopes[0].Path {
110+
//
111+
// oldPath := old.PermissionScopes[0].Path
112+
// newPath := r.PermissionScopes[0].Path
113+
//
114+
// users, err := db.GetUsersByRole(int(r.ID))
115+
// if err != nil {
116+
// return errors.WithMessage(err, "failed to get users by role")
117+
// }
118+
//
119+
// modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldPath, newPath, users)
120+
// if err != nil {
121+
// return errors.WithMessage(err, "failed to update user base path when role updated")
122+
// }
123+
//
124+
// for _, name := range modifiedUsernames {
125+
// userCache.Del(name)
126+
// }
127+
//}
129128
roleCache.Del(fmt.Sprint(r.ID))
130129
roleCache.Del(r.Name)
131130
return db.UpdateRole(r)

internal/op/storage.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,20 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error {
232232
roleCache.Del(fmt.Sprint(id))
233233
}
234234

235-
modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldStorage.MountPath, storage.MountPath)
236-
if err != nil {
237-
return errors.WithMessage(err, "failed to update user base path")
238-
}
239-
for _, name := range modifiedUsernames {
240-
userCache.Del(name)
235+
//modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldStorage.MountPath, storage.MountPath)
236+
//if err != nil {
237+
// return errors.WithMessage(err, "failed to update user base path")
238+
//}
239+
for _, id := range modifiedRoleIDs {
240+
roleCache.Del(fmt.Sprint(id))
241+
242+
users, err := db.GetUsersByRole(int(id))
243+
if err != nil {
244+
return errors.WithMessage(err, "failed to get users by role")
245+
}
246+
for _, user := range users {
247+
userCache.Del(user.Username)
248+
}
241249
}
242250
}
243251
if err != nil {

internal/op/user.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func GetUserByRole(role int) (*model.User, error) {
5050
return db.GetUserByRole(role)
5151
}
5252

53+
func GetUsersByRole(role int) ([]model.User, error) {
54+
return db.GetUsersByRole(role)
55+
}
56+
5357
func GetUserByName(username string) (*model.User, error) {
5458
if username == "" {
5559
return nil, errs.EmptyUsername
@@ -124,17 +128,17 @@ func UpdateUser(u *model.User) error {
124128
}
125129
userCache.Del(old.Username)
126130
u.BasePath = utils.FixAndCleanPath(u.BasePath)
127-
if len(u.Role) > 0 {
128-
roles, err := GetRolesByUserID(u.ID)
129-
if err == nil {
130-
for _, role := range roles {
131-
if len(role.PermissionScopes) > 0 {
132-
u.BasePath = utils.FixAndCleanPath(role.PermissionScopes[0].Path)
133-
break
134-
}
135-
}
136-
}
137-
}
131+
//if len(u.Role) > 0 {
132+
// roles, err := GetRolesByUserID(u.ID)
133+
// if err == nil {
134+
// for _, role := range roles {
135+
// if len(role.PermissionScopes) > 0 {
136+
// u.BasePath = utils.FixAndCleanPath(role.PermissionScopes[0].Path)
137+
// break
138+
// }
139+
// }
140+
// }
141+
//}
138142
return db.UpdateUser(u)
139143
}
140144

pkg/utils/path.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ func JoinBasePath(basePath, reqPath string) (string, error) {
8888
strings.Contains(reqPath, "/../") {
8989
return "", errs.RelativePath
9090
}
91+
92+
reqPath = FixAndCleanPath(reqPath)
93+
94+
if strings.HasPrefix(reqPath, "/") {
95+
return reqPath, nil
96+
}
97+
9198
return stdpath.Join(FixAndCleanPath(basePath), FixAndCleanPath(reqPath)), nil
9299
}
93100

0 commit comments

Comments
 (0)