feat: complete Kra admin backend migration

This commit is contained in:
2026-08-14 00:28:37 +08:00
parent c4bf7ee03b
commit acba9624d7
88 changed files with 1765 additions and 8623 deletions
+29 -6
View File
@@ -73,6 +73,16 @@ type apiTokenPO struct {
func (apiTokenPO) TableName() string { return "sys_api_tokens" }
type jwtBlacklistPO struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
JWT string `gorm:"column:jwt;type:text"`
}
func (jwtBlacklistPO) TableName() string { return "jwt_blacklists" }
type securityConfigPO struct {
ID uint `gorm:"primaryKey"`
CaptchaOpen int
@@ -435,15 +445,28 @@ func (r *settingsRepo) DisableAPIToken(ctx context.Context, id uint) (string, er
}
return po.Token, r.data.gormDB.WithContext(ctx).Model(&po).Update("status", false).Error
}
func (r *settingsRepo) IsTokenDisabled(ctx context.Context, token string) (bool, error) {
var po apiTokenPO
err := r.data.gormDB.WithContext(ctx).Where("token = ?", token).First(&po).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
func (r *settingsRepo) BlacklistToken(ctx context.Context, token string) error {
if token == "" {
return errors.New("token is empty")
}
if err != nil {
return r.data.gormDB.WithContext(ctx).Create(&jwtBlacklistPO{JWT: token}).Error
}
func (r *settingsRepo) IsTokenDisabled(ctx context.Context, token string) (bool, error) {
var blacklistCount int64
if err := r.data.gormDB.WithContext(ctx).Model(&jwtBlacklistPO{}).Where("jwt = ?", token).Count(&blacklistCount).Error; err != nil {
return false, err
}
if blacklistCount > 0 {
return true, nil
}
var po apiTokenPO
result := r.data.gormDB.WithContext(ctx).Where("token = ?", token).Limit(1).Find(&po)
if result.Error != nil {
return false, result.Error
}
if result.RowsAffected == 0 {
return false, nil
}
return !po.Status || time.Now().After(po.ExpiresAt), nil
}