From fba96d024f6d50f143ca1bb2c111aa6df690fc35 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 30 Jun 2022 15:53:57 +0800 Subject: [PATCH] feat: add write field to list resp --- internal/model/user.go | 33 ++++++++++++++------------------- server/controllers/fsmanage.go | 10 +++++----- server/controllers/fsread.go | 2 ++ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/internal/model/user.go b/internal/model/user.go index f6bb5556fa1..d9c79dd0585 100644 --- a/internal/model/user.go +++ b/internal/model/user.go @@ -21,14 +21,13 @@ type User struct { // 0: can see hidden files // 1: can access without password // 2: can add aria2 tasks - // 3: can mkdir - // 4: can upload - // 5: can rename - // 6: can move - // 7: can copy - // 8: can remove - // 9: webdav read - // 10: webdav write + // 3: can mkdir and upload + // 4: can rename + // 5: can move + // 6: can copy + // 7: can remove + // 8: webdav read + // 9: webdav write Permission int32 `json:"permission"` } @@ -62,34 +61,30 @@ func (u User) CanAddAria2Tasks() bool { return u.IsAdmin() || (u.Permission>>2)&1 == 1 } -func (u User) CanMkdir() bool { +func (u User) CanWrite() bool { return u.IsAdmin() || (u.Permission>>3)&1 == 1 } -func (u User) CanUpload() bool { - return u.IsAdmin() || (u.Permission>>4)&1 == 1 -} - func (u User) CanRename() bool { - return u.IsAdmin() || (u.Permission>>5)&1 == 1 + return u.IsAdmin() || (u.Permission>>4)&1 == 1 } func (u User) CanMove() bool { - return u.IsAdmin() || (u.Permission>>6)&1 == 1 + return u.IsAdmin() || (u.Permission>>5)&1 == 1 } func (u User) CanCopy() bool { - return u.IsAdmin() || (u.Permission>>7)&1 == 1 + return u.IsAdmin() || (u.Permission>>6)&1 == 1 } func (u User) CanRemove() bool { - return u.IsAdmin() || (u.Permission>>8)&1 == 1 + return u.IsAdmin() || (u.Permission>>7)&1 == 1 } func (u User) CanWebdavRead() bool { - return u.IsAdmin() || (u.Permission>>9)&1 == 1 + return u.IsAdmin() || (u.Permission>>8)&1 == 1 } func (u User) CanWebdavWrite() bool { - return u.IsAdmin() || (u.Permission>>10)&1 == 1 + return u.IsAdmin() || (u.Permission>>9)&1 == 1 } diff --git a/server/controllers/fsmanage.go b/server/controllers/fsmanage.go index 1cfd40f312f..36213578f6d 100644 --- a/server/controllers/fsmanage.go +++ b/server/controllers/fsmanage.go @@ -26,13 +26,13 @@ func FsMkdir(c *gin.Context) { } user := c.MustGet("user").(*model.User) req.Path = stdpath.Join(user.BasePath, req.Path) - if !user.CanMkdir() { + if !user.CanWrite() { meta, err := db.GetNearestMeta(req.Path) if err != nil { common.ErrorResp(c, err, 500) return } - if !canMkdirOrPut(meta, req.Path) { + if !canWrite(meta, req.Path) { common.ErrorResp(c, errs.PermissionDenied, 403) return } @@ -44,7 +44,7 @@ func FsMkdir(c *gin.Context) { common.SuccessResp(c) } -func canMkdirOrPut(meta *model.Meta, path string) bool { +func canWrite(meta *model.Meta, path string) bool { if meta == nil || !meta.Write { return false } @@ -178,13 +178,13 @@ func FsPut(c *gin.Context) { path := c.GetHeader("File-Path") user := c.MustGet("user").(*model.User) path = stdpath.Join(user.BasePath, path) - if !user.CanUpload() { + if !user.CanWrite() { meta, err := db.GetNearestMeta(path) if err != nil { common.ErrorResp(c, err, 500) return } - if !canMkdirOrPut(meta, path) { + if !canWrite(meta, path) { common.ErrorResp(c, errs.PermissionDenied, 403) return } diff --git a/server/controllers/fsread.go b/server/controllers/fsread.go index 42a1a3d095b..116c59ab640 100644 --- a/server/controllers/fsread.go +++ b/server/controllers/fsread.go @@ -35,6 +35,7 @@ type FsListResp struct { Content []ObjResp `json:"content"` Total int64 `json:"total"` Readme string `json:"readme"` + Write bool `json:"write"` } func FsList(c *gin.Context) { @@ -68,6 +69,7 @@ func FsList(c *gin.Context) { Content: toObjResp(objs), Total: int64(total), Readme: getReadme(meta, req.Path), + Write: user.CanWrite() || canWrite(meta, req.Path), }) }