mirror of
https://github.com/AlistGo/alist.git
synced 2025-12-19 11:00:06 +08:00
feat(user-db): enhance user management with role-based queries (allow-edit-role-guest) (#9234)
Some checks are pending
auto_lang / auto generate lang.json (1.21, ubuntu-latest) (push) Waiting to run
beta release / Beta Release Changelog (1.21, ubuntu-latest) (push) Waiting to run
beta release / Beta Release (md5, !(*musl*|*windows-arm64*|*android*|*freebsd*)) (push) Blocked by required conditions
beta release / Beta Release (md5-android, android-*) (push) Blocked by required conditions
beta release / Beta Release (md5-freebsd, freebsd-*) (push) Blocked by required conditions
beta release / Beta Release (md5-linux-musl, linux-!(arm*)-musl*) (push) Blocked by required conditions
beta release / Beta Release (md5-linux-musl-arm, linux-arm*-musl*) (push) Blocked by required conditions
beta release / Beta Release (md5-windows-arm64, windows-arm64) (push) Blocked by required conditions
beta release / Beta Release Desktop (push) Blocked by required conditions
build / Build (ubuntu-latest, android-arm64) (push) Waiting to run
build / Build (ubuntu-latest, darwin-amd64) (push) Waiting to run
build / Build (ubuntu-latest, darwin-arm64) (push) Waiting to run
build / Build (ubuntu-latest, linux-amd64-musl) (push) Waiting to run
build / Build (ubuntu-latest, linux-arm64-musl) (push) Waiting to run
build / Build (ubuntu-latest, windows-amd64) (push) Waiting to run
build / Build (ubuntu-latest, windows-arm64) (push) Waiting to run
release_docker / Build Binaries for Docker Release (push) Waiting to run
release_docker / Release Docker image (, latest, ) (push) Blocked by required conditions
release_docker / Release Docker image (INSTALL_ARIA2=true, aria2, suffix=-aria2,onlatest=true) (push) Blocked by required conditions
release_docker / Release Docker image (INSTALL_FFMPEG=true
INSTALL_ARIA2=true
, aio, suffix=-aio,onlatest=true) (push) Blocked by required conditions
release_docker / Release Docker image (INSTALL_FFMPEG=true, ffmpeg, suffix=-ffmpeg,onlatest=true) (push) Blocked by required conditions
Some checks are pending
auto_lang / auto generate lang.json (1.21, ubuntu-latest) (push) Waiting to run
beta release / Beta Release Changelog (1.21, ubuntu-latest) (push) Waiting to run
beta release / Beta Release (md5, !(*musl*|*windows-arm64*|*android*|*freebsd*)) (push) Blocked by required conditions
beta release / Beta Release (md5-android, android-*) (push) Blocked by required conditions
beta release / Beta Release (md5-freebsd, freebsd-*) (push) Blocked by required conditions
beta release / Beta Release (md5-linux-musl, linux-!(arm*)-musl*) (push) Blocked by required conditions
beta release / Beta Release (md5-linux-musl-arm, linux-arm*-musl*) (push) Blocked by required conditions
beta release / Beta Release (md5-windows-arm64, windows-arm64) (push) Blocked by required conditions
beta release / Beta Release Desktop (push) Blocked by required conditions
build / Build (ubuntu-latest, android-arm64) (push) Waiting to run
build / Build (ubuntu-latest, darwin-amd64) (push) Waiting to run
build / Build (ubuntu-latest, darwin-arm64) (push) Waiting to run
build / Build (ubuntu-latest, linux-amd64-musl) (push) Waiting to run
build / Build (ubuntu-latest, linux-arm64-musl) (push) Waiting to run
build / Build (ubuntu-latest, windows-amd64) (push) Waiting to run
build / Build (ubuntu-latest, windows-arm64) (push) Waiting to run
release_docker / Build Binaries for Docker Release (push) Waiting to run
release_docker / Release Docker image (, latest, ) (push) Blocked by required conditions
release_docker / Release Docker image (INSTALL_ARIA2=true, aria2, suffix=-aria2,onlatest=true) (push) Blocked by required conditions
release_docker / Release Docker image (INSTALL_FFMPEG=true
INSTALL_ARIA2=true
, aio, suffix=-aio,onlatest=true) (push) Blocked by required conditions
release_docker / Release Docker image (INSTALL_FFMPEG=true, ffmpeg, suffix=-ffmpeg,onlatest=true) (push) Blocked by required conditions
- 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.
This commit is contained in:
parent
74332e91fb
commit
280960ce3e
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"path"
|
"path"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,6 +26,20 @@ func GetUserByRole(role int) (*model.User, error) {
|
|||||||
return nil, gorm.ErrRecordNotFound
|
return nil, gorm.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUsersByRole(roleID int) ([]model.User, error) {
|
||||||
|
var users []model.User
|
||||||
|
if err := db.Find(&users).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result []model.User
|
||||||
|
for _, u := range users {
|
||||||
|
if slices.Contains(u.Role, roleID) {
|
||||||
|
result = append(result, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserByName(username string) (*model.User, error) {
|
func GetUserByName(username string) (*model.User, error) {
|
||||||
user := model.User{Username: username}
|
user := model.User{Username: username}
|
||||||
if err := db.Where(user).First(&user).Error; err != nil {
|
if err := db.Where(user).First(&user).Error; err != nil {
|
||||||
@ -109,25 +124,29 @@ func RemoveAuthn(u *model.User, id string) error {
|
|||||||
return UpdateAuthn(u.ID, string(res))
|
return UpdateAuthn(u.ID, string(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateUserBasePathPrefix(oldPath, newPath string) ([]string, error) {
|
func UpdateUserBasePathPrefix(oldPath, newPath string, usersOpt ...[]model.User) ([]string, error) {
|
||||||
var users []model.User
|
var users []model.User
|
||||||
var modifiedUsernames []string
|
var modifiedUsernames []string
|
||||||
|
|
||||||
|
oldPathClean := path.Clean(oldPath)
|
||||||
|
|
||||||
|
if len(usersOpt) > 0 {
|
||||||
|
users = usersOpt[0]
|
||||||
|
} else {
|
||||||
if err := db.Find(&users).Error; err != nil {
|
if err := db.Find(&users).Error; err != nil {
|
||||||
return nil, errors.WithMessage(err, "failed to load users")
|
return nil, errors.WithMessage(err, "failed to load users")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
oldPathClean := path.Clean(oldPath)
|
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
basePath := path.Clean(user.BasePath)
|
basePath := path.Clean(user.BasePath)
|
||||||
updated := false
|
updated := false
|
||||||
|
|
||||||
if basePath == oldPathClean {
|
if basePath == oldPathClean {
|
||||||
user.BasePath = newPath
|
user.BasePath = path.Clean(newPath)
|
||||||
updated = true
|
updated = true
|
||||||
} else if strings.HasPrefix(basePath, oldPathClean+"/") {
|
} else if strings.HasPrefix(basePath, oldPathClean+"/") {
|
||||||
user.BasePath = newPath + basePath[len(oldPathClean):]
|
user.BasePath = path.Clean(newPath + basePath[len(oldPathClean):])
|
||||||
updated = true
|
updated = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -100,7 +100,6 @@ func UpdateRole(r *model.Role) error {
|
|||||||
switch old.Name {
|
switch old.Name {
|
||||||
case "admin":
|
case "admin":
|
||||||
return errs.ErrChangeDefaultRole
|
return errs.ErrChangeDefaultRole
|
||||||
|
|
||||||
case "guest":
|
case "guest":
|
||||||
r.Name = "guest"
|
r.Name = "guest"
|
||||||
}
|
}
|
||||||
@ -112,7 +111,13 @@ func UpdateRole(r *model.Role) error {
|
|||||||
|
|
||||||
oldPath := old.PermissionScopes[0].Path
|
oldPath := old.PermissionScopes[0].Path
|
||||||
newPath := r.PermissionScopes[0].Path
|
newPath := r.PermissionScopes[0].Path
|
||||||
modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldPath, newPath)
|
|
||||||
|
users, err := db.GetUsersByRole(int(r.ID))
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "failed to get users by role")
|
||||||
|
}
|
||||||
|
|
||||||
|
modifiedUsernames, err := db.UpdateUserBasePathPrefix(oldPath, newPath, users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed to update user base path when role updated")
|
return errors.WithMessage(err, "failed to update user base path when role updated")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,6 +41,15 @@ func Auth(c *gin.Context) {
|
|||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(guest.Role) > 0 {
|
||||||
|
roles, err := op.GetRolesByUserID(guest.ID)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorStrResp(c, fmt.Sprintf("Fail to load guest roles: %v", err), 500)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guest.RolesDetail = roles
|
||||||
|
}
|
||||||
c.Set("user", guest)
|
c.Set("user", guest)
|
||||||
log.Debugf("use empty token: %+v", guest)
|
log.Debugf("use empty token: %+v", guest)
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user