Cloudreve/models/user.go

273 lines
6.7 KiB
Go
Raw Normal View History

2019-11-07 15:56:05 +08:00
package model
import (
"crypto/md5"
2019-11-07 15:56:05 +08:00
"crypto/sha1"
"encoding/hex"
"encoding/json"
2019-11-16 16:11:37 +08:00
"github.com/HFO4/cloudreve/pkg/util"
2019-11-07 15:56:05 +08:00
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"strings"
)
const (
// Active 账户正常状态
Active = iota
// NotActivicated 未激活
NotActivicated
// Baned 被封禁
Baned
2020-02-13 11:53:24 +08:00
// OveruseBaned 超额使用被封禁
OveruseBaned
2019-11-07 15:56:05 +08:00
)
// User 用户模型
type User struct {
2019-11-13 16:28:14 +08:00
// 表字段
2019-11-07 15:56:05 +08:00
gorm.Model
2020-03-11 15:22:21 +08:00
Email string `gorm:"type:varchar(100);unique_index"`
Nick string `gorm:"size:50"`
Password string `json:"-"`
Status int
GroupID uint
Storage uint64
TwoFactor string
Avatar string
Options string `json:"-",gorm:"type:text"`
Authn string `gorm:"type:text"`
2019-11-13 16:28:14 +08:00
// 关联模型
2020-02-19 16:05:54 +08:00
Group Group `gorm:"save_associations:false:false"`
2019-11-14 14:18:10 +08:00
Policy Policy `gorm:"PRELOAD:false,association_autoupdate:false"`
2019-11-13 16:28:14 +08:00
// 数据库忽略字段
2019-11-12 15:34:54 +08:00
OptionsSerialized UserOption `gorm:"-"`
}
// UserOption 用户个性化配置字段
type UserOption struct {
2020-03-11 15:22:21 +08:00
ProfileOff bool `json:"profile_off,omitempty"`
PreferredTheme string `json:"preferred_theme,omitempty"`
2019-11-14 14:18:10 +08:00
}
// Root 获取用户的根目录
func (user *User) Root() (*Folder, error) {
var folder Folder
2020-01-29 13:45:27 +08:00
err := DB.Where("parent_id is NULL AND owner_id = ?", user.ID).First(&folder).Error
return &folder, err
}
// DeductionStorage 减少用户已用容量
func (user *User) DeductionStorage(size uint64) bool {
2019-12-01 14:31:29 +08:00
if size == 0 {
return true
}
if size <= user.Storage {
user.Storage -= size
2020-02-19 16:05:54 +08:00
DB.Model(user).Update("storage", gorm.Expr("storage - ?", size))
return true
}
// 如果要减少的容量超出已用容量,则设为零
2019-12-01 14:31:29 +08:00
user.Storage = 0
2020-02-19 16:05:54 +08:00
DB.Model(user).Update("storage", 0)
2019-12-01 14:31:29 +08:00
return false
}
// IncreaseStorage 检查并增加用户已用容量
func (user *User) IncreaseStorage(size uint64) bool {
2019-12-01 14:31:29 +08:00
if size == 0 {
return true
}
2019-11-16 16:05:10 +08:00
if size <= user.GetRemainingCapacity() {
user.Storage += size
2020-02-19 16:05:54 +08:00
DB.Model(user).Update("storage", gorm.Expr("storage + ?", size))
2019-11-16 16:05:10 +08:00
return true
}
return false
}
2019-12-03 16:32:23 +08:00
// IncreaseStorageWithoutCheck 忽略可用容量,增加用户已用容量
func (user *User) IncreaseStorageWithoutCheck(size uint64) {
if size == 0 {
return
}
user.Storage += size
2020-02-19 16:05:54 +08:00
DB.Model(user).Update("storage", gorm.Expr("storage + ?", size))
2019-12-03 16:32:23 +08:00
}
2019-11-16 16:05:10 +08:00
// GetRemainingCapacity 获取剩余配额
func (user *User) GetRemainingCapacity() uint64 {
2020-03-11 15:22:21 +08:00
total := user.Group.MaxStorage
2020-01-14 10:32:54 +08:00
if total <= user.Storage {
2019-11-16 16:05:10 +08:00
return 0
}
2020-01-14 10:32:54 +08:00
return total - user.Storage
2019-11-16 16:05:10 +08:00
}
2019-11-30 15:09:56 +08:00
// GetPolicyID 获取用户当前的存储策略ID
func (user *User) GetPolicyID(prefer uint) uint {
2020-03-11 18:03:37 +08:00
if len(user.Group.PolicyList) > 0 {
return user.Group.PolicyList[0]
}
return 0
2019-11-07 15:56:05 +08:00
}
2019-11-11 19:13:17 +08:00
// GetUserByID 用ID获取用户
func GetUserByID(ID interface{}) (User, error) {
2019-11-07 15:56:05 +08:00
var user User
2019-11-13 16:28:14 +08:00
result := DB.Set("gorm:auto_preload", true).First(&user, ID)
2019-11-07 15:56:05 +08:00
return user, result.Error
}
2020-02-15 14:02:21 +08:00
// GetActiveUserByID 用ID获取可登录用户
func GetActiveUserByID(ID interface{}) (User, error) {
var user User
result := DB.Set("gorm:auto_preload", true).Where("status = ?", Active).First(&user, ID)
return user, result.Error
}
2020-02-21 13:48:34 +08:00
// GetActiveUserByOpenID 用OpenID获取可登录用户
func GetActiveUserByOpenID(openid string) (User, error) {
var user User
result := DB.Set("gorm:auto_preload", true).Where("status = ? and open_id = ?", Active, openid).Find(&user)
return user, result.Error
}
2019-11-11 19:13:17 +08:00
// GetUserByEmail 用Email获取用户
func GetUserByEmail(email string) (User, error) {
var user User
2020-02-19 16:05:54 +08:00
result := DB.Set("gorm:auto_preload", true).Where("status = ? and email = ?", Active, email).First(&user)
2019-11-11 19:13:17 +08:00
return user, result.Error
}
2019-11-07 15:56:05 +08:00
// NewUser 返回一个新的空 User
func NewUser() User {
2020-02-18 15:34:40 +08:00
options := UserOption{}
2019-11-07 15:56:05 +08:00
return User{
2019-11-14 14:18:10 +08:00
OptionsSerialized: options,
2019-11-07 15:56:05 +08:00
}
}
2019-11-14 14:18:10 +08:00
// BeforeSave Save用户前的钩子
func (user *User) BeforeSave() (err error) {
err = user.SerializeOptions()
return err
}
2019-11-15 16:32:43 +08:00
// AfterCreate 创建用户后的钩子
func (user *User) AfterCreate(tx *gorm.DB) (err error) {
// 创建用户的默认根目录
defaultFolder := &Folder{
Name: "/",
OwnerID: user.ID,
2019-11-15 16:32:43 +08:00
}
tx.Create(defaultFolder)
return err
}
2019-11-11 19:13:17 +08:00
// AfterFind 找到用户后的钩子
func (user *User) AfterFind() (err error) {
// 解析用户设置到OptionsSerialized
2019-12-30 19:56:01 +08:00
if user.Options != "" {
err = json.Unmarshal([]byte(user.Options), &user.OptionsSerialized)
}
2019-11-14 14:18:10 +08:00
// 预加载存储策略
user.Policy, _ = GetPolicyByID(user.GetPolicyID(0))
2019-11-11 19:13:17 +08:00
return err
}
//SerializeOptions 将序列后的Option写入到数据库字段
func (user *User) SerializeOptions() (err error) {
optionsValue, err := json.Marshal(&user.OptionsSerialized)
user.Options = string(optionsValue)
return err
}
2019-11-07 15:56:05 +08:00
// CheckPassword 根据明文校验密码
func (user *User) CheckPassword(password string) (bool, error) {
// 根据存储密码拆分为 Salt 和 Digest
passwordStore := strings.Split(user.Password, ":")
if len(passwordStore) != 2 && len(passwordStore) != 3 {
2019-11-07 15:56:05 +08:00
return false, errors.New("Unknown password type")
}
// 兼容V2密码升级后存储格式为: md5:$HASH:$SALT
if len(passwordStore) == 3 {
if passwordStore[0] != "md5" {
return false, errors.New("Unknown password type")
}
hash := md5.New()
_, err := hash.Write([]byte(passwordStore[2] + password))
bs := hex.EncodeToString(hash.Sum(nil))
if err != nil {
return false, err
}
return bs == passwordStore[1], nil
}
2019-11-07 15:56:05 +08:00
//计算 Salt 和密码组合的SHA1摘要
hash := sha1.New()
_, err := hash.Write([]byte(password + passwordStore[0]))
bs := hex.EncodeToString(hash.Sum(nil))
if err != nil {
return false, err
}
return bs == passwordStore[1], nil
}
// SetPassword 根据给定明文设定 User 的 Password 字段
func (user *User) SetPassword(password string) error {
//生成16位 Salt
salt := util.RandStringRunes(16)
//计算 Salt 和密码组合的SHA1摘要
hash := sha1.New()
_, err := hash.Write([]byte(password + salt))
bs := hex.EncodeToString(hash.Sum(nil))
if err != nil {
return err
}
//存储 Salt 值和摘要, ":"分割
user.Password = salt + ":" + string(bs)
return nil
}
2020-01-26 14:57:07 +08:00
// NewAnonymousUser 返回一个匿名用户
func NewAnonymousUser() *User {
user := User{}
user.Policy.Type = "anonymous"
2020-01-26 14:57:07 +08:00
user.Group, _ = GetGroupByID(3)
return &user
}
// IsAnonymous 返回是否为未登录用户
func (user *User) IsAnonymous() bool {
return user.ID == 0
}
2020-02-15 14:02:21 +08:00
// SetStatus 设定用户状态
func (user *User) SetStatus(status int) {
DB.Model(&user).Update("status", status)
}
2020-02-19 16:05:54 +08:00
// Update 更新用户
func (user *User) Update(val map[string]interface{}) error {
return DB.Model(user).Updates(val).Error
}
2020-02-20 12:42:38 +08:00
// UpdateOptions 更新用户偏好设定
func (user *User) UpdateOptions() error {
if err := user.SerializeOptions(); err != nil {
return err
}
return user.Update(map[string]interface{}{"options": user.Options})
}