From a93ea2cfa05953baeb94b46379a1c14020ba6dba Mon Sep 17 00:00:00 2001 From: WeidiDeng Date: Tue, 7 Feb 2023 19:43:28 +0800 Subject: [PATCH] feat(webdav): add read-only option (#1629) --- models/webdav.go | 6 ++++++ routers/controllers/webdav.go | 21 +++++++++++++++++++++ routers/router.go | 2 ++ service/setting/webdav.go | 14 ++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/models/webdav.go b/models/webdav.go index c1492f3..fef4495 100644 --- a/models/webdav.go +++ b/models/webdav.go @@ -11,6 +11,7 @@ type Webdav struct { Password string `gorm:"unique_index:password_only_on"` // 应用密码 UserID uint `gorm:"unique_index:password_only_on"` // 用户ID Root string `gorm:"type:text"` // 根目录 + Readonly bool `gorm:"type:bool"` // 是否只读 } // Create 创建账户 @@ -39,3 +40,8 @@ func ListWebDAVAccounts(uid uint) []Webdav { func DeleteWebDAVAccountByID(id, uid uint) { DB.Where("user_id = ? and id = ?", uid, id).Delete(&Webdav{}) } + +// UpdateWebDAVAccountReadonlyByID 根据账户ID和UID更新账户的只读性 +func UpdateWebDAVAccountReadonlyByID(id, uid uint, readonly bool) { + DB.Model(&Webdav{Model: gorm.Model{ID: id}, UserID: uid}).UpdateColumn("readonly", readonly) +} diff --git a/routers/controllers/webdav.go b/routers/controllers/webdav.go index d44cd18..5f3f084 100644 --- a/routers/controllers/webdav.go +++ b/routers/controllers/webdav.go @@ -7,6 +7,7 @@ import ( "github.com/cloudreve/Cloudreve/v3/pkg/webdav" "github.com/cloudreve/Cloudreve/v3/service/setting" "github.com/gin-gonic/gin" + "net/http" "sync" ) @@ -39,6 +40,15 @@ func ServeWebDAV(c *gin.Context) { fs.Root = root } } + + // 检查是否只读 + if application.Readonly { + switch c.Request.Method { + case "DELETE", "PUT", "MKCOL", "COPY", "MOVE": + c.Status(http.StatusForbidden) + return + } + } } handler.ServeHTTP(c.Writer, c.Request, fs) @@ -66,6 +76,17 @@ func DeleteWebDAVAccounts(c *gin.Context) { } } +// UpdateWebDAVAccountsReadonly 更改WebDAV账户只读性 +func UpdateWebDAVAccountsReadonly(c *gin.Context) { + var service setting.WebDAVAccountUpdateReadonlyService + if err := c.ShouldBindJSON(&service); err == nil { + res := service.Update(c, CurrentUser(c)) + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) + } +} + // CreateWebDAVAccounts 创建WebDAV账户 func CreateWebDAVAccounts(c *gin.Context) { var service setting.WebDAVAccountCreateService diff --git a/routers/router.go b/routers/router.go index 53b1683..d28a93b 100644 --- a/routers/router.go +++ b/routers/router.go @@ -699,6 +699,8 @@ func InitMasterRouter() *gin.Engine { webdav.POST("accounts", controllers.CreateWebDAVAccounts) // 删除账号 webdav.DELETE("accounts/:id", controllers.DeleteWebDAVAccounts) + // 更新账号可读性 + webdav.PATCH("accounts", controllers.UpdateWebDAVAccountsReadonly) } } diff --git a/service/setting/webdav.go b/service/setting/webdav.go index f2d7e8f..4f252ad 100644 --- a/service/setting/webdav.go +++ b/service/setting/webdav.go @@ -22,6 +22,12 @@ type WebDAVAccountCreateService struct { Name string `json:"name" binding:"required,min=1,max=255"` } +// WebDAVAccountUpdateReadonlyService WebDAV 修改只读性服务 +type WebDAVAccountUpdateReadonlyService struct { + ID uint `json:"id" binding:"required,min=1"` + Readonly bool `json:"readonly"` +} + // WebDAVMountCreateService WebDAV 挂载创建服务 type WebDAVMountCreateService struct { Path string `json:"path" binding:"required,min=1,max=65535"` @@ -56,6 +62,14 @@ func (service *WebDAVAccountService) Delete(c *gin.Context, user *model.User) se return serializer.Response{} } +// Update 修改WebDAV账户的只读性 +func (service *WebDAVAccountUpdateReadonlyService) Update(c *gin.Context, user *model.User) serializer.Response { + model.UpdateWebDAVAccountReadonlyByID(service.ID, user.ID, service.Readonly) + return serializer.Response{Data: map[string]bool{ + "readonly": service.Readonly, + }} +} + // Accounts 列出WebDAV账号 func (service *WebDAVListService) Accounts(c *gin.Context, user *model.User) serializer.Response { accounts := model.ListWebDAVAccounts(user.ID)