Files
BackupX/server/internal/repository/audit_log_repository.go
Awuqing 09698cc767 feat: add community enhancements — password reset, audit logs, multi-source backup
Three community-requested features:

1. CLI password reset: `backupx reset-password --username admin --password xxx`
   Docker users can run via `docker exec`. No full app init needed.

2. Audit logging: async fire-and-forget audit trail for all key operations
   (login, CRUD on tasks/targets/records, settings changes).
   New UI page at /audit with category filter and pagination.

3. Multi-source path backup: file backup tasks now support multiple source
   directories packed into a single tar archive. Backward compatible with
   existing single sourcePath field.
2026-03-30 23:04:37 +08:00

57 lines
1.3 KiB
Go

package repository
import (
"context"
"backupx/server/internal/model"
"gorm.io/gorm"
)
type AuditLogListOptions struct {
Category string
Limit int
Offset int
}
type AuditLogListResult struct {
Items []model.AuditLog `json:"items"`
Total int64 `json:"total"`
}
type AuditLogRepository interface {
Create(ctx context.Context, log *model.AuditLog) error
List(ctx context.Context, opts AuditLogListOptions) (*AuditLogListResult, error)
}
type gormAuditLogRepository struct {
db *gorm.DB
}
func NewAuditLogRepository(db *gorm.DB) AuditLogRepository {
return &gormAuditLogRepository{db: db}
}
func (r *gormAuditLogRepository) Create(_ context.Context, log *model.AuditLog) error {
return r.db.Create(log).Error
}
func (r *gormAuditLogRepository) List(_ context.Context, opts AuditLogListOptions) (*AuditLogListResult, error) {
query := r.db.Model(&model.AuditLog{})
if opts.Category != "" {
query = query.Where("category = ?", opts.Category)
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, err
}
limit := opts.Limit
if limit <= 0 {
limit = 50
}
var items []model.AuditLog
if err := query.Order("created_at DESC").Offset(opts.Offset).Limit(limit).Find(&items).Error; err != nil {
return nil, err
}
return &AuditLogListResult{Items: items, Total: total}, nil
}