Skip to content

Commit

Permalink
feat: add write field to list resp
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 30, 2022
1 parent 35b04ff commit fba96d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
33 changes: 14 additions & 19 deletions internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions server/controllers/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions server/controllers/fsread.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
})
}

Expand Down

0 comments on commit fba96d0

Please sign in to comment.