Recycle accounts

This commit is contained in:
LittleSheep 2024-06-26 15:52:58 +08:00
parent 880ed9a999
commit ea33857afb
6 changed files with 64 additions and 9 deletions

13
.idea/workspace.xml generated
View File

@ -4,8 +4,13 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Bug fixes"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":arrow_up: Fix notification listen">
<change beforePath="$PROJECT_DIR$/web/src/stores/notifications.ts" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/stores/notifications.ts" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/tokens.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/tokens.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/auth_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/auth_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/tokens.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/tokens.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/main.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -143,7 +148,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Firebase is back" />
<MESSAGE value=":sparkles: Apple push notification services" /> <MESSAGE value=":sparkles: Apple push notification services" />
<MESSAGE value=":bug: Bug fix and fix" /> <MESSAGE value=":bug: Bug fix and fix" />
<MESSAGE value=":bug: Fix APNs non-production" /> <MESSAGE value=":bug: Fix APNs non-production" />
@ -168,7 +172,8 @@
<MESSAGE value=":recycle: OAuth authenticate" /> <MESSAGE value=":recycle: OAuth authenticate" />
<MESSAGE value=":sparkles: Recommend app component" /> <MESSAGE value=":sparkles: Recommend app component" />
<MESSAGE value=":bug: Bug fixes" /> <MESSAGE value=":bug: Bug fixes" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Bug fixes" /> <MESSAGE value=":arrow_up: Fix notification listen" />
<option name="LAST_COMMIT_MESSAGE" value=":arrow_up: Fix notification listen" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -14,6 +14,6 @@ type MagicToken struct {
Code string `json:"code"` Code string `json:"code"`
Type int8 `json:"type"` Type int8 `json:"type"`
AssignTo *uint `json:"assign_to"` AccountID *uint `json:"account_id"`
ExpiredAt *time.Time `json:"expired_at"` ExpiredAt *time.Time `json:"expired_at"`
} }

View File

@ -23,6 +23,8 @@ func doAuthenticate(c *fiber.Ctx) error {
user, err := services.LookupAccount(data.Username) user, err := services.LookupAccount(data.Username)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err.Error())) return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err.Error()))
} else if user.ConfirmedAt == nil {
return fiber.NewError(fiber.StatusForbidden, "account was not confirmed")
} }
ticket, err := services.NewTicket(user, c.IP(), c.Get(fiber.HeaderUserAgent)) ticket, err := services.NewTicket(user, c.IP(), c.Get(fiber.HeaderUserAgent))

View File

@ -2,6 +2,7 @@ package services
import ( import (
"fmt" "fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/datatypes" "gorm.io/datatypes"
"time" "time"
@ -93,7 +94,7 @@ func ConfirmAccount(code string) error {
var user models.Account var user models.Account
if err := database.C.Where(&models.Account{ if err := database.C.Where(&models.Account{
BaseModel: models.BaseModel{ID: *token.AssignTo}, BaseModel: models.BaseModel{ID: *token.AccountID},
}).First(&user).Error; err != nil { }).First(&user).Error; err != nil {
return err return err
} }
@ -121,3 +122,49 @@ func ConfirmAccount(code string) error {
return nil return nil
}) })
} }
func DeleteAccount(id uint) error {
tx := database.C.Begin()
for _, model := range []any{
&models.Badge{},
&models.RealmMember{},
&models.AccountContact{},
&models.AuthFactor{},
&models.AuthTicket{},
&models.MagicToken{},
&models.ThirdClient{},
&models.Notification{},
&models.NotificationSubscriber{},
&models.AccountFriendship{},
} {
if err := tx.Delete(model, "account_id = ?", id).Error; err != nil {
tx.Rollback()
return err
}
}
if err := tx.Delete(&models.Account{}, "id = ?", id).Error; err != nil {
tx.Rollback()
return err
}
return tx.Commit().Error
}
func RecycleUnConfirmAccount() {
var hitList []models.Account
if err := database.C.Where("confirmed_at IS NULL").Find(&hitList).Error; err != nil {
log.Error().Err(err).Msg("An error occurred while recycling accounts...")
return
}
if len(hitList) > 0 {
log.Info().Int("count", len(hitList)).Msg("Going to recycle those un-confirmed accounts...")
for _, entry := range hitList {
if err := DeleteAccount(entry.ID); err != nil {
log.Error().Err(err).Msg("An error occurred while recycling accounts...")
}
}
}
}

View File

@ -47,7 +47,7 @@ func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expired
token := models.MagicToken{ token := models.MagicToken{
Code: strings.Replace(uuid.NewString(), "-", "", -1), Code: strings.Replace(uuid.NewString(), "-", "", -1),
Type: mode, Type: mode,
AssignTo: &uid, AccountID: &uid,
ExpiredAt: expiredAt, ExpiredAt: expiredAt,
} }
@ -59,13 +59,13 @@ func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expired
} }
func NotifyMagicToken(token models.MagicToken) error { func NotifyMagicToken(token models.MagicToken) error {
if token.AssignTo == nil { if token.AccountID == nil {
return fmt.Errorf("could notify a non-assign magic token") return fmt.Errorf("could notify a non-assign magic token")
} }
var user models.Account var user models.Account
if err := database.C.Where(&models.Account{ if err := database.C.Where(&models.Account{
BaseModel: models.BaseModel{ID: *token.AssignTo}, BaseModel: models.BaseModel{ID: *token.AccountID},
}).Preload("Contacts").First(&user).Error; err != nil { }).Preload("Contacts").First(&user).Error; err != nil {
return err return err
} }

View File

@ -68,6 +68,7 @@ func main() {
quartz.AddFunc("@every 60m", services.DoAutoSignoff) quartz.AddFunc("@every 60m", services.DoAutoSignoff)
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup) quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
quartz.AddFunc("@every 60s", services.RecycleAuthContext) quartz.AddFunc("@every 60s", services.RecycleAuthContext)
quartz.AddFunc("@every 60m", services.RecycleUnConfirmAccount)
quartz.AddFunc("@every 5m", services.KexCleanup) quartz.AddFunc("@every 5m", services.KexCleanup)
quartz.Start() quartz.Start()