feat(config): add sensitive word filtering system with database support
Implement sensitive word filtering feature with configurable database and Redis support. Add security enhancements including WebSocket origin validation, improved password strength requirements, timing attack prevention, and production JWT secret validation. Add batch operation validation limits and optimize user blocking queries with subqueries. BREAKING CHANGE: Password validation now requires minimum 8 characters with uppercase, lowercase, and digit (was 6-50 characters without complexity requirements)
This commit is contained in:
66
internal/repository/sensitive_word_repo.go
Normal file
66
internal/repository/sensitive_word_repo.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"carrot_bbs/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type SensitiveWordRepository interface {
|
||||
Create(ctx context.Context, word *model.SensitiveWord) error
|
||||
GetByWord(ctx context.Context, word string) (*model.SensitiveWord, error)
|
||||
Update(ctx context.Context, word *model.SensitiveWord) error
|
||||
Upsert(ctx context.Context, word *model.SensitiveWord) error
|
||||
DeactivateByWord(ctx context.Context, word string) error
|
||||
ListActive(ctx context.Context) ([]model.SensitiveWord, error)
|
||||
}
|
||||
|
||||
type sensitiveWordRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewSensitiveWordRepository(db *gorm.DB) SensitiveWordRepository {
|
||||
return &sensitiveWordRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *sensitiveWordRepository) Create(ctx context.Context, word *model.SensitiveWord) error {
|
||||
return r.db.WithContext(ctx).Create(word).Error
|
||||
}
|
||||
|
||||
func (r *sensitiveWordRepository) GetByWord(ctx context.Context, word string) (*model.SensitiveWord, error) {
|
||||
var sw model.SensitiveWord
|
||||
err := r.db.WithContext(ctx).Where("word = ?", word).First(&sw).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sw, nil
|
||||
}
|
||||
|
||||
func (r *sensitiveWordRepository) Update(ctx context.Context, word *model.SensitiveWord) error {
|
||||
return r.db.WithContext(ctx).Save(word).Error
|
||||
}
|
||||
|
||||
func (r *sensitiveWordRepository) Upsert(ctx context.Context, word *model.SensitiveWord) error {
|
||||
return r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "word"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"category", "level", "is_active", "updated_by", "remark", "updated_at",
|
||||
}),
|
||||
}).Create(word).Error
|
||||
}
|
||||
|
||||
func (r *sensitiveWordRepository) DeactivateByWord(ctx context.Context, word string) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Model(&model.SensitiveWord{}).
|
||||
Where("word = ?", word).
|
||||
Update("is_active", false).Error
|
||||
}
|
||||
|
||||
func (r *sensitiveWordRepository) ListActive(ctx context.Context) ([]model.SensitiveWord, error) {
|
||||
var words []model.SensitiveWord
|
||||
err := r.db.WithContext(ctx).Where("is_active = ?", true).Find(&words).Error
|
||||
return words, err
|
||||
}
|
||||
Reference in New Issue
Block a user