mirror of
				https://github.com/cloudreve/cloudreve.git
				synced 2025-11-04 04:47:24 +08:00 
			
		
		
		
	feat(webdav): add read-only option (#1629)
This commit is contained in:
		@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -699,6 +699,8 @@ func InitMasterRouter() *gin.Engine {
 | 
			
		||||
				webdav.POST("accounts", controllers.CreateWebDAVAccounts)
 | 
			
		||||
				// 删除账号
 | 
			
		||||
				webdav.DELETE("accounts/:id", controllers.DeleteWebDAVAccounts)
 | 
			
		||||
				// 更新账号可读性
 | 
			
		||||
				webdav.PATCH("accounts", controllers.UpdateWebDAVAccountsReadonly)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user