Feat: cron / Fix: users status check

This commit is contained in:
HFO4
2020-02-15 14:02:21 +08:00
parent 44d6ca487c
commit faf46745bc
22 changed files with 503 additions and 28 deletions

View File

@@ -89,6 +89,7 @@ func addDefaultSettings() {
{Name: "siteDes", Value: `Cloudreve`, Type: "basic"},
{Name: "siteTitle", Value: `平步云端`, Type: "basic"},
{Name: "fromName", Value: `Cloudreve`, Type: "mail"},
{Name: "mail_keepalive", Value: `30`, Type: "mail"},
{Name: "fromAdress", Value: `no-reply@acg.blue`, Type: "mail"},
{Name: "smtpHost", Value: `smtp.mxhichina.com`, Type: "mail"},
{Name: "smtpPort", Value: `25`, Type: "mail"},
@@ -151,9 +152,7 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
{Name: "admin_color_body", Value: `fixed-nav sticky-footer bg-light`, Type: "admin"},
{Name: "admin_color_nav", Value: `navbar navbar-expand-lg fixed-top navbar-light bg-light`, Type: "admin"},
{Name: "js_code", Value: `<script type="text/javascript"></script>`, Type: "basic"},
{Name: "sendfile", Value: `0`, Type: "download"},
{Name: "defaultTheme", Value: `#3f51b5`, Type: "basic"},
{Name: "header", Value: `X-Sendfile`, Type: "download"},
{Name: "themes", Value: `{"#3f51b5":{"palette":{"primary":{"light":"#7986cb","main":"#3f51b5","dark":"#303f9f","contrastText":"#fff"},"secondary":{"light":"#ff4081","main":"#f50057","dark":"#c51162","contrastText":"#fff"},"error":{"light":"#e57373","main":"#f44336","dark":"#d32f2f","contrastText":"#fff"},"explorer":{"filename":"#474849","icon":"#8f8f8f","bgSelected":"#D5DAF0","emptyIcon":"#e8e8e8"}}}}`, Type: "basic"},
{Name: "refererCheck", Value: `true`, Type: "share"},
{Name: "header", Value: `X-Sendfile`, Type: "download"},
@@ -170,6 +169,9 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
{Name: "share_score_rate", Value: "80", Type: "score"},
{Name: "home_view_method", Value: "icon", Type: "view"},
{Name: "share_view_method", Value: "list", Type: "view"},
{Name: "cron_garbage_collect", Value: "@hourly", Type: "cron"},
{Name: "cron_notify_user", Value: "@hourly", Type: "cron"},
{Name: "cron_ban_user", Value: "@hourly", Type: "cron"},
}
for _, value := range defaultSettings {
@@ -241,7 +243,6 @@ func addDefaultUser() {
defaultUser.Nick = "admin"
defaultUser.Status = Active
defaultUser.GroupID = 1
defaultUser.PrimaryGroup = 1
err := defaultUser.SetPassword("admin")
if err != nil {
util.Log().Panic("无法创建密码, %s", err)

View File

@@ -59,3 +59,15 @@ func (user *User) GetAvailablePackSize() uint64 {
return total
}
// GetExpiredStoragePack 获取已过期的容量包
func GetExpiredStoragePack() []StoragePack {
var packs []StoragePack
DB.Where("expired_time < ?", time.Now()).Find(&packs)
return packs
}
// Delete 删除容量包
func (pack *StoragePack) Delete() error {
return DB.Delete(&pack).Error
}

View File

@@ -26,22 +26,23 @@ const (
type User struct {
// 表字段
gorm.Model
Email string `gorm:"type:varchar(100);unique_index"`
Nick string `gorm:"size:50"`
Password string `json:"-"`
Status int
GroupID uint
PrimaryGroup int
ActivationKey string `json:"-"`
Storage uint64
LastNotify *time.Time
OpenID string `json:"-"`
TwoFactor string `json:"-"`
Delay int
Avatar string
Options string `json:"-",gorm:"type:text"`
Authn string `gorm:"type:text"`
Score int
Email string `gorm:"type:varchar(100);unique_index"`
Nick string `gorm:"size:50"`
Password string `json:"-"`
Status int
GroupID uint
ActivationKey string `json:"-"`
Storage uint64
OpenID string `json:"-"`
TwoFactor string `json:"-"`
Delay int
Avatar string
Options string `json:"-",gorm:"type:text"`
Authn string `gorm:"type:text"`
Score int
PreviousGroupID uint // 初始用户组
GroupExpires *time.Time // 用户组过期日期
NotifyDate *time.Time // 通知超出配额时的日期
// 关联模型
Group Group `gorm:"association_autoupdate:false"`
@@ -165,6 +166,13 @@ func GetUserByID(ID interface{}) (User, error) {
return user, result.Error
}
// 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
}
// GetUserByEmail 用Email获取用户
func GetUserByEmail(email string) (User, error) {
var user User
@@ -271,3 +279,49 @@ func NewAnonymousUser() *User {
func (user *User) IsAnonymous() bool {
return user.ID == 0
}
// Notified 更新用户容量超额通知日期
func (user *User) Notified() {
if user.NotifyDate == nil {
timeNow := time.Now()
user.NotifyDate = &timeNow
DB.Model(&user).Update("notify_date", user.NotifyDate)
}
}
// ClearNotified 清除用户通知标记
func (user *User) ClearNotified() {
DB.Model(&user).Update("notify_date", nil)
}
// SetStatus 设定用户状态
func (user *User) SetStatus(status int) {
DB.Model(&user).Update("status", status)
}
// GetGroupExpiredUsers 获取用户组过期的用户
func GetGroupExpiredUsers() []User {
var users []User
DB.Where("group_expires < ? and previous_group_id <> 0", time.Now()).Find(&users)
return users
}
// GetTolerantExpiredUser 获取超过宽容期的用户
func GetTolerantExpiredUser() []User {
var users []User
DB.Set("gorm:auto_preload", true).Where("notify_date < ?", time.Now().Add(
time.Duration(-GetIntSetting("ban_time", 10))*time.Second),
).Find(&users)
return users
}
// GroupFallback 回退到初始用户组
func (user *User) GroupFallback() {
if user.GroupExpires != nil && user.PreviousGroupID != 0 {
DB.Model(&user).Updates(map[string]interface{}{
"group_expires": nil,
"previous_group_id": 0,
"group_id": user.PreviousGroupID,
})
}
}