mirror of
https://github.com/AlistGo/alist.git
synced 2025-12-19 02:50:06 +08:00
Some checks are pending
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
* feat: improve WebDAV permission handling and user role fetching - Added logic to handle root permissions in WebDAV requests. - Improved the user role fetching mechanism. - Enhanced path checks and permission scopes in role_perm.go. - Set FetchRole function to avoid import cycles between modules. * fix(webdav): resolve connection reset issue by encoding paths - Adjust path encoding in webdav.go to prevent connection reset. - Utilize utils.EncodePath for correct path formatting. - Ensure proper handling of directory paths with trailing slash. * fix(webdav): resolve connection reset issue by encoding paths - Adjust path encoding in webdav.go to prevent connection reset. - Utilize utils.FixAndCleanPath for correct path formatting. - Ensure proper handling of directory paths with trailing slash. * fix: resolve webdav handshake error in permission checks - Updated role permission logic to handle bidirectional subpaths. - This adjustment fixes the issue where remote host terminates the handshake due to improper path matching. * fix: resolve webdav handshake error in permission checks (fix/fix-webdav-error) - Updated role permission logic to handle bidirectional subpaths, fixing handshake termination by remote host due to path mismatch. - Refactored function naming for consistency and clarity. - Enhanced filtering of objects based on user permissions. * fix: resolve webdav handshake error in permission checks - Updated role permission logic to handle bidirectional subpaths, fixing handshake termination by remote host due to path mismatch. - Refactored function naming for consistency and clarity. - Enhanced filtering of objects based on user permissions.
149 lines
3.4 KiB
Go
149 lines
3.4 KiB
Go
package op
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Xhofe/go-cache"
|
|
"github.com/alist-org/alist/v3/internal/db"
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/alist-org/alist/v3/pkg/singleflight"
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
)
|
|
|
|
var roleCache = cache.NewMemCache[*model.Role](cache.WithShards[*model.Role](2))
|
|
var roleG singleflight.Group[*model.Role]
|
|
|
|
func init() {
|
|
model.FetchRole = GetRole
|
|
}
|
|
|
|
func GetRole(id uint) (*model.Role, error) {
|
|
key := fmt.Sprint(id)
|
|
if r, ok := roleCache.Get(key); ok {
|
|
return r, nil
|
|
}
|
|
r, err, _ := roleG.Do(key, func() (*model.Role, error) {
|
|
_r, err := db.GetRole(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
roleCache.Set(key, _r, cache.WithEx[*model.Role](time.Hour))
|
|
return _r, nil
|
|
})
|
|
return r, err
|
|
}
|
|
|
|
func GetRoleByName(name string) (*model.Role, error) {
|
|
if r, ok := roleCache.Get(name); ok {
|
|
return r, nil
|
|
}
|
|
r, err, _ := roleG.Do(name, func() (*model.Role, error) {
|
|
_r, err := db.GetRoleByName(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
roleCache.Set(name, _r, cache.WithEx[*model.Role](time.Hour))
|
|
return _r, nil
|
|
})
|
|
return r, err
|
|
}
|
|
|
|
func GetRolesByUserID(userID uint) ([]model.Role, error) {
|
|
user, err := GetUserById(userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var roles []model.Role
|
|
for _, roleID := range user.Role {
|
|
key := fmt.Sprint(roleID)
|
|
|
|
if r, ok := roleCache.Get(key); ok {
|
|
roles = append(roles, *r)
|
|
continue
|
|
}
|
|
|
|
r, err, _ := roleG.Do(key, func() (*model.Role, error) {
|
|
_r, err := db.GetRole(uint(roleID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
roleCache.Set(key, _r, cache.WithEx[*model.Role](time.Hour))
|
|
return _r, nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
roles = append(roles, *r)
|
|
}
|
|
|
|
return roles, nil
|
|
}
|
|
|
|
func GetRoles(pageIndex, pageSize int) ([]model.Role, int64, error) {
|
|
return db.GetRoles(pageIndex, pageSize)
|
|
}
|
|
|
|
func CreateRole(r *model.Role) error {
|
|
for i := range r.PermissionScopes {
|
|
r.PermissionScopes[i].Path = utils.FixAndCleanPath(r.PermissionScopes[i].Path)
|
|
}
|
|
roleCache.Del(fmt.Sprint(r.ID))
|
|
roleCache.Del(r.Name)
|
|
return db.CreateRole(r)
|
|
}
|
|
|
|
func UpdateRole(r *model.Role) error {
|
|
old, err := db.GetRole(r.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch old.Name {
|
|
case "admin":
|
|
return errs.ErrChangeDefaultRole
|
|
case "guest":
|
|
r.Name = "guest"
|
|
}
|
|
for i := range r.PermissionScopes {
|
|
r.PermissionScopes[i].Path = utils.FixAndCleanPath(r.PermissionScopes[i].Path)
|
|
}
|
|
//if len(old.PermissionScopes) > 0 && len(r.PermissionScopes) > 0 &&
|
|
// old.PermissionScopes[0].Path != r.PermissionScopes[0].Path {
|
|
//
|
|
// oldPath := old.PermissionScopes[0].Path
|
|
// newPath := r.PermissionScopes[0].Path
|
|
//
|
|
// 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 {
|
|
// return errors.WithMessage(err, "failed to update user base path when role updated")
|
|
// }
|
|
//
|
|
// for _, name := range modifiedUsernames {
|
|
// userCache.Del(name)
|
|
// }
|
|
//}
|
|
roleCache.Del(fmt.Sprint(r.ID))
|
|
roleCache.Del(r.Name)
|
|
return db.UpdateRole(r)
|
|
}
|
|
|
|
func DeleteRole(id uint) error {
|
|
old, err := db.GetRole(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if old.Name == "admin" || old.Name == "guest" {
|
|
return errs.ErrChangeDefaultRole
|
|
}
|
|
roleCache.Del(fmt.Sprint(id))
|
|
roleCache.Del(old.Name)
|
|
return db.DeleteRole(id)
|
|
}
|