Recycle accounts

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

View File

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

View File

@ -23,6 +23,8 @@ func doAuthenticate(c *fiber.Ctx) error {
user, err := services.LookupAccount(data.Username)
if err != nil {
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))

View File

@ -2,6 +2,7 @@ package services
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"gorm.io/datatypes"
"time"
@ -93,7 +94,7 @@ func ConfirmAccount(code string) error {
var user 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 {
return err
}
@ -121,3 +122,49 @@ func ConfirmAccount(code string) error {
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{
Code: strings.Replace(uuid.NewString(), "-", "", -1),
Type: mode,
AssignTo: &uid,
AccountID: &uid,
ExpiredAt: expiredAt,
}
@ -59,13 +59,13 @@ func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expired
}
func NotifyMagicToken(token models.MagicToken) error {
if token.AssignTo == nil {
if token.AccountID == nil {
return fmt.Errorf("could notify a non-assign magic token")
}
var user 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 {
return err
}