diff --git a/models/webdav.go b/models/webdav.go index c1492f3607..fef44957b3 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 d44cd18bd2..5f3f084ec9 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 53b1683c7d..d28a93bcf2 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 f2d7e8f52b..4f252ad58a 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)