feat: publish sanitized custom.97 source
This commit is contained in:
1022 files changed
+69903
-19207
No files matched your search
+86
-84
@@ -92,8 +92,8 @@ type User struct {
|
||||
WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
|
||||
TelegramId string `json:"telegram_id" gorm:"column:telegram_id;index"`
|
||||
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
|
||||
AccessToken *string `json:"-" gorm:"type:char(32);column:access_token;uniqueIndex"` // this token is for system management
|
||||
AccessTokenCreatedAt *int64 `json:"-" gorm:"type:bigint;column:access_token_created_at"`
|
||||
AccessToken *string `json:"-" gorm:"type:char(32);column:access_token;uniqueIndex"` // Deprecated: 旧版面板访问令牌,仅在升级后的过渡期内使用;删除 users.access_token 列时一并移除。
|
||||
AccessTokenCreatedAt *int64 `json:"-" gorm:"type:bigint;column:access_token_created_at"` // Deprecated: 旧版面板访问令牌,仅在升级后的过渡期内使用;删除 users.access_token 列时一并移除。
|
||||
Quota int `json:"quota" gorm:"type:int;default:0"`
|
||||
UsedQuota int `json:"used_quota" gorm:"type:int;default:0;column:used_quota"` // used quota
|
||||
RequestCount int `json:"request_count" gorm:"type:int;default:0;"` // request number
|
||||
@@ -130,6 +130,7 @@ func (user *User) ToBaseUser() *UserBase {
|
||||
return cache
|
||||
}
|
||||
|
||||
// Deprecated: 旧版面板访问令牌,仅在升级后的过渡期内使用;删除 users.access_token 列时一并移除。
|
||||
func (user *User) GetAccessToken() string {
|
||||
if user.AccessToken == nil {
|
||||
return ""
|
||||
@@ -137,29 +138,14 @@ func (user *User) GetAccessToken() string {
|
||||
return *user.AccessToken
|
||||
}
|
||||
|
||||
// Deprecated: 旧版面板访问令牌,仅在升级后的过渡期内使用;删除 users.access_token 列时一并移除。
|
||||
func (user *User) SetAccessToken(token string) {
|
||||
user.AccessToken = &token
|
||||
}
|
||||
|
||||
// UpdateUserAccessToken rotates a dashboard personal access token without
|
||||
// writing a stale user snapshot back over concurrently updated fields.
|
||||
func UpdateUserAccessToken(id int, token string) error {
|
||||
if id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
}
|
||||
result := DB.Model(&User{}).Where("id = ?", id).Updates(map[string]any{
|
||||
"access_token": token, "access_token_created_at": common.GetTimestamp(),
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RevokeUserAccessToken returns the generation actually revoked under the row lock.
|
||||
//
|
||||
// Deprecated: 旧版面板访问令牌,仅在升级后的过渡期内使用;删除 users.access_token 列时一并移除。
|
||||
func RevokeUserAccessToken(id int) (string, error) {
|
||||
var tokenRef string
|
||||
err := DB.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -181,7 +167,7 @@ func (user *User) GetSetting() dto.UserSetting {
|
||||
if user.Setting != "" {
|
||||
err := common.Unmarshal([]byte(user.Setting), &setting)
|
||||
if err != nil {
|
||||
common.SysLog("failed to unmarshal setting: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to unmarshal setting: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
return setting
|
||||
@@ -190,7 +176,7 @@ func (user *User) GetSetting() dto.UserSetting {
|
||||
func (user *User) SetSetting(setting dto.UserSetting) {
|
||||
settingBytes, err := common.Marshal(setting)
|
||||
if err != nil {
|
||||
common.SysLog("failed to marshal setting: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to marshal setting: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
user.Setting = string(settingBytes)
|
||||
@@ -198,7 +184,7 @@ func (user *User) SetSetting(setting dto.UserSetting) {
|
||||
|
||||
func UpdateUserSetting(userId int, setting dto.UserSetting) error {
|
||||
if userId == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return common.NewMessage("ID is empty!")
|
||||
}
|
||||
settingBytes, err := common.Marshal(setting)
|
||||
if err != nil {
|
||||
@@ -227,7 +213,7 @@ var userBindColumns = map[string]bool{
|
||||
// 角色、状态、分组只允许通过各自带锁/CAS 的专用方法修改。
|
||||
func UpdateUserBindColumn(userId int, column string, value string) error {
|
||||
if userId <= 0 {
|
||||
return errors.New("id 为空!")
|
||||
return common.NewMessage("ID is empty!")
|
||||
}
|
||||
if !userBindColumns[column] {
|
||||
return fmt.Errorf("invalid user bind column: %s", column)
|
||||
@@ -290,7 +276,7 @@ func generateDefaultSidebarConfigForRole(userRole int) string {
|
||||
// 转换为JSON字符串
|
||||
configBytes, err := common.Marshal(defaultConfig)
|
||||
if err != nil {
|
||||
common.SysLog("生成默认边栏配置失败: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to generate the default sidebar config: %s", err.Error()))
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -520,7 +506,7 @@ func SearchUsers(keyword string, group string, role *int, status *int, startIdx
|
||||
|
||||
func GetUserById(id int, selectAll bool) (*User, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("id 为空!")
|
||||
return nil, common.NewMessage("ID is empty!")
|
||||
}
|
||||
user := User{Id: id}
|
||||
var err error = nil
|
||||
@@ -536,7 +522,7 @@ func GetUserById(id int, selectAll bool) (*User, error) {
|
||||
// query. The password hash and management access token are never selected.
|
||||
func GetSelfUserById(id int) (*User, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("id 为空!")
|
||||
return nil, common.NewMessage("ID is empty!")
|
||||
}
|
||||
var profile struct {
|
||||
User
|
||||
@@ -556,24 +542,28 @@ func GetSelfUserById(id int) (*User, error) {
|
||||
|
||||
func GetUserIdByAffCode(affCode string) (int, error) {
|
||||
if affCode == "" {
|
||||
return 0, errors.New("affCode 为空!")
|
||||
return 0, errors.New("affiliate code is empty")
|
||||
}
|
||||
var user User
|
||||
err := DB.Select("id").First(&user, "aff_code = ?", affCode).Error
|
||||
return user.Id, err
|
||||
}
|
||||
|
||||
func DeleteUserById(id int) (err error) {
|
||||
// DeleteUserById soft-deletes a user and returns how many scoped access tokens
|
||||
// were deleted with it.
|
||||
func DeleteUserById(id int) (int64, error) {
|
||||
if id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return 0, common.NewMessage("ID is empty!")
|
||||
}
|
||||
user := User{Id: id}
|
||||
return user.Delete()
|
||||
}
|
||||
|
||||
func HardDeleteUserById(id int) error {
|
||||
// HardDeleteUserById permanently deletes a user and returns how many scoped
|
||||
// access tokens were deleted with it.
|
||||
func HardDeleteUserById(id int) (int64, error) {
|
||||
if id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return 0, common.NewMessage("ID is empty!")
|
||||
}
|
||||
user := User{Id: id}
|
||||
return user.HardDelete()
|
||||
@@ -597,7 +587,7 @@ func inviteUser(inviterId int) error {
|
||||
func (user *User) TransferAffQuotaToQuota(quota int) error {
|
||||
// 检查quota是否小于最小额度
|
||||
if float64(quota) < common.QuotaPerUnit {
|
||||
return fmt.Errorf("转移额度最小为%s!", logger.LogQuota(common.QuotaFromFloat(common.QuotaPerUnit)))
|
||||
return common.NewMessage("Minimum transfer quota is {{min}}!", map[string]any{"min": logger.FormatQuota(common.QuotaFromFloat(common.QuotaPerUnit))})
|
||||
}
|
||||
|
||||
// 开始数据库事务
|
||||
@@ -615,7 +605,7 @@ func (user *User) TransferAffQuotaToQuota(quota int) error {
|
||||
|
||||
// 再次检查用户的AffQuota是否足够
|
||||
if user.AffQuota < quota {
|
||||
return errors.New("邀请额度不足!")
|
||||
return common.NewMessage("Invitation quota is insufficient!")
|
||||
}
|
||||
|
||||
// 更新用户额度
|
||||
@@ -720,21 +710,21 @@ func (user *User) finishInsert(inviterId int) {
|
||||
currentSetting.SidebarModules = defaultSidebarConfig
|
||||
createdUser.SetSetting(currentSetting)
|
||||
createdUser.Update(false)
|
||||
common.SysLog(fmt.Sprintf("为新用户 %s (角色: %d) 初始化边栏配置", createdUser.Username, createdUser.Role))
|
||||
common.SysLog(common.LogText("initialized the sidebar config for new user %s (role: %d)", createdUser.Username, createdUser.Role))
|
||||
}
|
||||
}
|
||||
|
||||
if common.QuotaForNewUser > 0 {
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %s", logger.LogQuota(common.QuotaForNewUser)))
|
||||
RecordLog(user.Id, LogTypeSystem, common.NewMessage("New user sign-up bonus {{quota}}", map[string]any{"quota": logger.FormatQuota(common.QuotaForNewUser)}))
|
||||
}
|
||||
if inviterId != 0 && operation_setting.IsPaymentComplianceConfirmed() {
|
||||
if common.QuotaForInvitee > 0 {
|
||||
_ = IncreaseUserQuota(user.Id, common.QuotaForInvitee, true)
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("使用邀请码赠送 %s", logger.LogQuota(common.QuotaForInvitee)))
|
||||
RecordLog(user.Id, LogTypeSystem, common.NewMessage("Invitation code bonus {{quota}}", map[string]any{"quota": logger.FormatQuota(common.QuotaForInvitee)}))
|
||||
}
|
||||
if common.QuotaForInviter > 0 {
|
||||
//_ = IncreaseUserQuota(inviterId, common.QuotaForInviter)
|
||||
RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", logger.LogQuota(common.QuotaForInviter)))
|
||||
RecordLog(inviterId, LogTypeSystem, common.NewMessage("Bonus for inviting a user {{quota}}", map[string]any{"quota": logger.FormatQuota(common.QuotaForInviter)}))
|
||||
_ = inviteUser(inviterId)
|
||||
}
|
||||
}
|
||||
@@ -777,20 +767,20 @@ func (user *User) FinalizeOAuthUserCreation(inviterId int) {
|
||||
currentSetting.SidebarModules = defaultSidebarConfig
|
||||
createdUser.SetSetting(currentSetting)
|
||||
createdUser.Update(false)
|
||||
common.SysLog(fmt.Sprintf("为新用户 %s (角色: %d) 初始化边栏配置", createdUser.Username, createdUser.Role))
|
||||
common.SysLog(common.LogText("initialized the sidebar config for new user %s (role: %d)", createdUser.Username, createdUser.Role))
|
||||
}
|
||||
}
|
||||
|
||||
if common.QuotaForNewUser > 0 {
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %s", logger.LogQuota(common.QuotaForNewUser)))
|
||||
RecordLog(user.Id, LogTypeSystem, common.NewMessage("New user sign-up bonus {{quota}}", map[string]any{"quota": logger.FormatQuota(common.QuotaForNewUser)}))
|
||||
}
|
||||
if inviterId != 0 && operation_setting.IsPaymentComplianceConfirmed() {
|
||||
if common.QuotaForInvitee > 0 {
|
||||
_ = IncreaseUserQuota(user.Id, common.QuotaForInvitee, true)
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("使用邀请码赠送 %s", logger.LogQuota(common.QuotaForInvitee)))
|
||||
RecordLog(user.Id, LogTypeSystem, common.NewMessage("Invitation code bonus {{quota}}", map[string]any{"quota": logger.FormatQuota(common.QuotaForInvitee)}))
|
||||
}
|
||||
if common.QuotaForInviter > 0 {
|
||||
RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", logger.LogQuota(common.QuotaForInviter)))
|
||||
RecordLog(inviterId, LogTypeSystem, common.NewMessage("Bonus for inviting a user {{quota}}", map[string]any{"quota": logger.FormatQuota(common.QuotaForInviter)}))
|
||||
_ = inviteUser(inviterId)
|
||||
}
|
||||
}
|
||||
@@ -953,20 +943,21 @@ func (user *User) ClearBinding(bindingType string) error {
|
||||
return updateUserCache(*user)
|
||||
}
|
||||
|
||||
func (user *User) Delete() error {
|
||||
func (user *User) Delete() (int64, error) {
|
||||
return user.delete(nil)
|
||||
}
|
||||
|
||||
func DeleteUserForSession(identity AuthSessionIdentity) error {
|
||||
func DeleteUserForSession(identity AuthSessionIdentity) (int64, error) {
|
||||
user := User{Id: identity.UserID}
|
||||
return user.delete(&identity)
|
||||
}
|
||||
|
||||
func (user *User) delete(identity *AuthSessionIdentity) error {
|
||||
func (user *User) delete(identity *AuthSessionIdentity) (int64, error) {
|
||||
if user.Id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return 0, common.NewMessage("ID is empty!")
|
||||
}
|
||||
var nextAuthVersion int64
|
||||
var revokedAccessTokens int64
|
||||
if err := DB.Transaction(func(tx *gorm.DB) error {
|
||||
if identity != nil {
|
||||
if err := ValidateAuthSessionWithTx(tx, *identity); err != nil {
|
||||
@@ -985,31 +976,40 @@ func (user *User) delete(identity *AuthSessionIdentity) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
revokedAccessTokens, err = DeleteUserAccessTokensWithTx(tx, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(user).Error
|
||||
}); err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
if err := publishCommittedUserAuthVersion(user.Id, nextAuthVersion); err != nil {
|
||||
return err
|
||||
return revokedAccessTokens, err
|
||||
}
|
||||
if _, err := RevokeAllUserSessions(user.Id, "user_deleted"); err != nil {
|
||||
return err
|
||||
return revokedAccessTokens, err
|
||||
}
|
||||
return invalidateUserCache(user.Id)
|
||||
return revokedAccessTokens, invalidateUserCache(user.Id)
|
||||
}
|
||||
|
||||
func (user *User) HardDelete() error {
|
||||
func (user *User) HardDelete() (int64, error) {
|
||||
if user.Id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return 0, common.NewMessage("ID is empty!")
|
||||
}
|
||||
var tokens []Token
|
||||
var deletedAuthVersion int64
|
||||
var revokedAccessTokens int64
|
||||
err := DB.Transaction(func(tx *gorm.DB) error {
|
||||
var err error
|
||||
deletedAuthVersion, err = IncrementUserAuthVersionWithTx(tx, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
revokedAccessTokens, err = DeleteUserAccessTokensWithTx(tx, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if common.RedisEnabled {
|
||||
if err := tx.Unscoped().Select("id", commonKeyCol).Where("user_id = ?", user.Id).Find(&tokens).Error; err != nil {
|
||||
return err
|
||||
@@ -1021,18 +1021,18 @@ func (user *User) HardDelete() error {
|
||||
return tx.Unscoped().Delete(user).Error
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
if err := publishCommittedUserAuthVersion(user.Id, deletedAuthVersion); err != nil {
|
||||
common.SysError(fmt.Sprintf("failed to publish auth tombstone after hard deleting user %d: %v", user.Id, err))
|
||||
common.SysError(common.LogText("failed to publish auth tombstone after hard deleting user %d: %v", user.Id, err))
|
||||
}
|
||||
if err := invalidateTokensCache(tokens); err != nil {
|
||||
common.SysError(fmt.Sprintf("failed to invalidate token cache after hard deleting user %d: %v", user.Id, err))
|
||||
common.SysError(common.LogText("failed to invalidate token cache after hard deleting user %d: %v", user.Id, err))
|
||||
}
|
||||
if err := invalidateUserCache(user.Id); err != nil {
|
||||
common.SysError(fmt.Sprintf("failed to invalidate user cache after hard deleting user %d: %v", user.Id, err))
|
||||
common.SysError(common.LogText("failed to invalidate user cache after hard deleting user %d: %v", user.Id, err))
|
||||
}
|
||||
return nil
|
||||
return revokedAccessTokens, nil
|
||||
}
|
||||
|
||||
func deleteUserAuthenticationData(tx *gorm.DB, userId int) error {
|
||||
@@ -1084,7 +1084,7 @@ func (user *User) ValidateAndFill() (err error) {
|
||||
|
||||
func (user *User) FillUserById() error {
|
||||
if user.Id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return common.NewMessage("ID is empty!")
|
||||
}
|
||||
DB.Where(User{Id: user.Id}).First(user)
|
||||
return nil
|
||||
@@ -1092,7 +1092,7 @@ func (user *User) FillUserById() error {
|
||||
|
||||
func (user *User) FillUserByEmail() error {
|
||||
if user.Email == "" {
|
||||
return errors.New("email 为空!")
|
||||
return common.NewMessage("Email is empty!")
|
||||
}
|
||||
DB.Where(User{Email: user.Email}).First(user)
|
||||
return nil
|
||||
@@ -1100,7 +1100,7 @@ func (user *User) FillUserByEmail() error {
|
||||
|
||||
func (user *User) FillUserByGitHubId() error {
|
||||
if user.GitHubId == "" {
|
||||
return errors.New("GitHub id 为空!")
|
||||
return common.NewMessage("GitHub ID is empty!")
|
||||
}
|
||||
DB.Where(User{GitHubId: user.GitHubId}).First(user)
|
||||
return nil
|
||||
@@ -1108,7 +1108,7 @@ func (user *User) FillUserByGitHubId() error {
|
||||
|
||||
func (user *User) FillUserByDiscordId() error {
|
||||
if user.DiscordId == "" {
|
||||
return errors.New("discord id 为空!")
|
||||
return common.NewMessage("Discord ID is empty!")
|
||||
}
|
||||
DB.Where(User{DiscordId: user.DiscordId}).First(user)
|
||||
return nil
|
||||
@@ -1116,7 +1116,7 @@ func (user *User) FillUserByDiscordId() error {
|
||||
|
||||
func (user *User) FillUserByOidcId() error {
|
||||
if user.OidcId == "" {
|
||||
return errors.New("oidc id 为空!")
|
||||
return common.NewMessage("OIDC ID is empty!")
|
||||
}
|
||||
DB.Where(User{OidcId: user.OidcId}).First(user)
|
||||
return nil
|
||||
@@ -1124,7 +1124,7 @@ func (user *User) FillUserByOidcId() error {
|
||||
|
||||
func (user *User) FillUserByWeChatId() error {
|
||||
if user.WeChatId == "" {
|
||||
return errors.New("WeChat id 为空!")
|
||||
return common.NewMessage("WeChat ID is empty!")
|
||||
}
|
||||
DB.Where(User{WeChatId: user.WeChatId}).First(user)
|
||||
return nil
|
||||
@@ -1132,11 +1132,11 @@ func (user *User) FillUserByWeChatId() error {
|
||||
|
||||
func (user *User) FillUserByTelegramId() error {
|
||||
if user.TelegramId == "" {
|
||||
return errors.New("Telegram id 为空!")
|
||||
return common.NewMessage("Telegram ID is empty!")
|
||||
}
|
||||
err := DB.Where(User{TelegramId: user.TelegramId}).First(user).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("该 Telegram 账户未绑定")
|
||||
return common.NewMessage("This Telegram account is not linked. Sign in using another method and link it first.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1187,7 +1187,7 @@ func IsTelegramIdAlreadyTaken(telegramId string) bool {
|
||||
|
||||
func ResetUserPasswordByEmail(email string, password string) error {
|
||||
if email == "" || password == "" {
|
||||
return errors.New("邮箱地址或密码为空!")
|
||||
return common.NewMessage("Email or password is empty!")
|
||||
}
|
||||
user, err := GetUniqueUserByEmail(email)
|
||||
if err != nil {
|
||||
@@ -1219,16 +1219,23 @@ func IsAdmin(userId int) bool {
|
||||
var user User
|
||||
err := DB.Where("id = ?", userId).Select("role").Find(&user).Error
|
||||
if err != nil {
|
||||
common.SysLog("no such user " + err.Error())
|
||||
common.SysLog(common.LogText("no such user %s", err.Error()))
|
||||
return false
|
||||
}
|
||||
return user.Role >= common.RoleAdminUser
|
||||
}
|
||||
|
||||
// ValidateAccessToken resolves a legacy plaintext access token. After the
|
||||
// transition deadline it rejects every value without querying the database.
|
||||
//
|
||||
// Deprecated: 旧版面板访问令牌,仅在升级后的过渡期内使用;删除 users.access_token 列时一并移除。
|
||||
func ValidateAccessToken(token string) (*User, error) {
|
||||
if token == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if LegacyAccessTokensRetired(common.GetTimestamp()) {
|
||||
return nil, ErrLegacyAccessTokenRetired
|
||||
}
|
||||
token = strings.Replace(token, "Bearer ", "", 1)
|
||||
user := &User{}
|
||||
err := DB.Where("access_token = ?", token).First(user).Error
|
||||
@@ -1271,7 +1278,7 @@ func GetUserGroup(id int, fromDB bool) (group string, err error) {
|
||||
if shouldUpdateRedis(fromDB, err) {
|
||||
gopool.Go(func() {
|
||||
if err := RefreshUserGroupCache(id); err != nil {
|
||||
common.SysLog("failed to update user group cache: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to update user group cache: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1300,7 +1307,7 @@ func GetUserSetting(id int, fromDB bool) (settingMap dto.UserSetting, err error)
|
||||
if shouldUpdateRedis(fromDB, err) {
|
||||
gopool.Go(func() {
|
||||
if err := updateUserSettingCache(id, setting); err != nil {
|
||||
common.SysLog("failed to update user setting cache: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to update user setting cache: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1332,7 +1339,7 @@ func GetUserSetting(id int, fromDB bool) (settingMap dto.UserSetting, err error)
|
||||
|
||||
func IncreaseUserQuota(id int, quota int, db bool) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative")
|
||||
}
|
||||
if err := common.ValidateWalletQuota(quota); err != nil {
|
||||
return err
|
||||
@@ -1341,7 +1348,7 @@ func IncreaseUserQuota(id int, quota int, db bool) (err error) {
|
||||
addNewRecord(BatchUpdateTypeUserQuota, id, quota)
|
||||
gopool.Go(func() {
|
||||
if err := cacheIncrUserQuota(id, int64(quota)); err != nil {
|
||||
common.SysLog("failed to increase user quota: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to increase user quota: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
return nil
|
||||
@@ -1351,7 +1358,7 @@ func IncreaseUserQuota(id int, quota int, db bool) (err error) {
|
||||
}
|
||||
gopool.Go(func() {
|
||||
if err := cacheIncrUserQuota(id, int64(quota)); err != nil {
|
||||
common.SysLog("failed to increase user quota: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to increase user quota: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
return nil
|
||||
@@ -1379,12 +1386,12 @@ func increaseUserQuota(id int, quota int) (err error) {
|
||||
|
||||
func DecreaseUserQuota(id int, quota int, db bool) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative")
|
||||
}
|
||||
gopool.Go(func() {
|
||||
err := cacheDecrUserQuota(id, int64(quota))
|
||||
if err != nil {
|
||||
common.SysLog("failed to decrease user quota: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to decrease user quota: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
if !db && common.BatchUpdateEnabled {
|
||||
@@ -1418,14 +1425,9 @@ func DeltaUpdateUserQuota(id int, delta int) (err error) {
|
||||
// return email
|
||||
//}
|
||||
|
||||
func GetRootUser() (user *User) {
|
||||
DB.Where("role = ?", common.RoleRootUser).First(&user)
|
||||
return user
|
||||
}
|
||||
|
||||
func UpdateUserLastLoginAt(id int) {
|
||||
if err := DB.Model(&User{}).Where("id = ?", id).Update("last_login_at", common.GetTimestamp()).Error; err != nil {
|
||||
common.SysLog("failed to update user last_login_at: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to update user last_login_at: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1445,7 +1447,7 @@ func UpdateUserUsedQuota(id int, quota int) {
|
||||
return
|
||||
}
|
||||
if err := DB.Model(&User{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error; err != nil {
|
||||
common.SysLog("failed to update user used quota: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to update user used quota: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1457,7 +1459,7 @@ func updateUserUsedQuotaAndRequestCount(id int, quota int, count int) {
|
||||
},
|
||||
).Error
|
||||
if err != nil {
|
||||
common.SysLog("failed to update user used quota and request count: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to update user used quota and request count: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1480,7 +1482,7 @@ func updateUserQuotaUsedQuotaAndRequestCount(id int, quota int, usedQuota int, r
|
||||
},
|
||||
).Error
|
||||
if err != nil {
|
||||
common.SysLog("failed to batch update user quota, used quota and request count: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to batch update user quota, used quota and request count: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1491,7 +1493,7 @@ func GetUsernameById(id int, fromDB bool) (username string, err error) {
|
||||
if shouldUpdateRedis(fromDB, err) {
|
||||
gopool.Go(func() {
|
||||
if err := updateUserNameCache(id, username); err != nil {
|
||||
common.SysLog("failed to update user name cache: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to update user name cache: %s", err.Error()))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user