github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/webdav.go (about)

     1  package model
     2  
     3  import (
     4  	"github.com/jinzhu/gorm"
     5  )
     6  
     7  // Webdav 应用账户
     8  type Webdav struct {
     9  	gorm.Model
    10  	Name     string // 应用名称
    11  	Password string `gorm:"unique_index:password_only_on"` // 应用密码
    12  	UserID   uint   `gorm:"unique_index:password_only_on"` // 用户ID
    13  	Root     string `gorm:"type:text"`                     // 根目录
    14  	Readonly bool   `gorm:"type:bool"`                     // 是否只读
    15  	UseProxy bool   `gorm:"type:bool"`                     // 是否进行反代
    16  }
    17  
    18  // Create 创建账户
    19  func (webdav *Webdav) Create() (uint, error) {
    20  	if err := DB.Create(webdav).Error; err != nil {
    21  		return 0, err
    22  	}
    23  	return webdav.ID, nil
    24  }
    25  
    26  // GetWebdavByPassword 根据密码和用户查找Webdav应用
    27  func GetWebdavByPassword(password string, uid uint) (*Webdav, error) {
    28  	webdav := &Webdav{}
    29  	res := DB.Where("user_id = ? and password = ?", uid, password).First(webdav)
    30  	return webdav, res.Error
    31  }
    32  
    33  // ListWebDAVAccounts 列出用户的所有账号
    34  func ListWebDAVAccounts(uid uint) []Webdav {
    35  	var accounts []Webdav
    36  	DB.Where("user_id = ?", uid).Order("created_at desc").Find(&accounts)
    37  	return accounts
    38  }
    39  
    40  // DeleteWebDAVAccountByID 根据账户ID和UID删除账户
    41  func DeleteWebDAVAccountByID(id, uid uint) {
    42  	DB.Where("user_id = ? and id = ?", uid, id).Delete(&Webdav{})
    43  }
    44  
    45  // UpdateWebDAVAccountByID 根据账户ID和UID更新账户
    46  func UpdateWebDAVAccountByID(id, uid uint, updates map[string]interface{}) {
    47  	DB.Model(&Webdav{Model: gorm.Model{ID: id}, UserID: uid}).Updates(updates)
    48  }