Cloudreve/models/group.go

88 lines
2 KiB
Go
Raw Normal View History

2019-11-13 03:28:14 -05:00
package model
import (
"encoding/json"
"github.com/jinzhu/gorm"
)
// Group 用户组模型
type Group struct {
gorm.Model
2019-11-14 01:18:10 -05:00
Name string
Policies string
MaxStorage uint64
ShareEnabled bool
WebDAVEnabled bool
Aria2Option string
Color string
SpeedLimit int
2019-12-11 20:16:24 -05:00
Options string `json:"-",gorm:"size:4096"`
2019-11-13 03:28:14 -05:00
// 数据库忽略字段
2019-12-11 20:16:24 -05:00
PolicyList []uint `gorm:"-"`
OptionsSerialized GroupOption `gorm:"-"`
}
// GroupOption 用户组其他配置
type GroupOption struct {
ArchiveDownloadEnabled bool `json:"archive_download"`
ArchiveTaskEnabled bool `json:"archive_task"`
2019-11-13 03:28:14 -05:00
}
// GetAria2Option 获取用户离线下载设备
func (group *Group) GetAria2Option() [3]bool {
2019-12-04 00:49:28 -05:00
if len(group.Aria2Option) != 5 {
return [3]bool{false, false, false}
}
return [3]bool{
group.Aria2Option[0] == '1',
group.Aria2Option[2] == '1',
2019-12-04 00:49:28 -05:00
group.Aria2Option[4] == '1',
}
}
2019-11-13 03:28:14 -05:00
// GetGroupByID 用ID获取用户组
func GetGroupByID(ID interface{}) (Group, error) {
var group Group
result := DB.First(&group, ID)
return group, result.Error
}
// AfterFind 找到用户组后的钩子处理Policy列表
func (group *Group) AfterFind() (err error) {
2019-12-11 20:16:24 -05:00
// 解析用户组策略列表
if group.Policies != "" {
err = json.Unmarshal([]byte(group.Policies), &group.PolicyList)
}
if err != nil {
return err
}
// 解析用户组设置
if group.Options != "" {
err = json.Unmarshal([]byte(group.Options), &group.OptionsSerialized)
}
2019-11-13 03:28:14 -05:00
return err
}
2019-11-14 01:18:10 -05:00
// BeforeSave Save用户前的钩子
func (group *Group) BeforeSave() (err error) {
err = group.SerializePolicyList()
return err
}
2019-12-11 20:16:24 -05:00
//SerializePolicyList 将序列后的可选策略列表、配置写入数据库字段
// TODO 完善测试
2019-11-14 01:18:10 -05:00
func (group *Group) SerializePolicyList() (err error) {
2019-12-11 20:16:24 -05:00
policies, err := json.Marshal(&group.PolicyList)
group.Policies = string(policies)
if err != nil {
return err
}
optionsValue, err := json.Marshal(&group.OptionsSerialized)
group.Options = string(optionsValue)
2019-11-14 01:18:10 -05:00
return err
}