61 lines
2.2 KiB
Go
61 lines
2.2 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"kra/internal/biz"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type dataAccessLogPO struct {
|
|
ID uint `gorm:"primaryKey"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
|
EventType, TargetTable, Operation string
|
|
UserID, AuthorityID uint
|
|
Scope int
|
|
RequestID, Method, Path, Detail string
|
|
}
|
|
|
|
func (dataAccessLogPO) TableName() string { return "sys_data_access_logs" }
|
|
|
|
func (r *auditRepo) RecordDataAccess(ctx context.Context, v *biz.DataAccessLog) error {
|
|
return r.data.gormDB.WithContext(ctx).Create(&dataAccessLogPO{EventType: v.EventType, TargetTable: v.TargetTable, Operation: v.Operation, UserID: v.UserID, AuthorityID: v.AuthorityID, Scope: v.Scope, RequestID: v.RequestID, Method: v.Method, Path: v.Path, Detail: v.Detail}).Error
|
|
}
|
|
func dataAccessFromPO(v dataAccessLogPO) *biz.DataAccessLog {
|
|
return &biz.DataAccessLog{ID: v.ID, CreatedAt: v.CreatedAt, EventType: v.EventType, TargetTable: v.TargetTable, Operation: v.Operation, UserID: v.UserID, AuthorityID: v.AuthorityID, Scope: v.Scope, RequestID: v.RequestID, Method: v.Method, Path: v.Path, Detail: v.Detail}
|
|
}
|
|
func (r *auditRepo) ListDataAccess(ctx context.Context, page, size int, q *biz.DataAccessLog) ([]*biz.DataAccessLog, int64, error) {
|
|
db := r.data.gormDB.WithContext(ctx).Model(&dataAccessLogPO{})
|
|
if q != nil {
|
|
if q.EventType != "" {
|
|
db = db.Where("event_type = ?", q.EventType)
|
|
}
|
|
if q.TargetTable != "" {
|
|
db = db.Where("target_table LIKE ?", "%"+q.TargetTable+"%")
|
|
}
|
|
if q.UserID != 0 {
|
|
db = db.Where("user_id = ?", q.UserID)
|
|
}
|
|
}
|
|
var total int64
|
|
if err := db.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var pos []dataAccessLogPO
|
|
if err := applyPagination(db.Order("id desc"), page, size, 100).Find(&pos).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]*biz.DataAccessLog, 0, len(pos))
|
|
for _, po := range pos {
|
|
out = append(out, dataAccessFromPO(po))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (r *auditRepo) DeleteDataAccess(ctx context.Context, ids []uint) error {
|
|
return r.data.gormDB.WithContext(ctx).Delete(&dataAccessLogPO{}, ids).Error
|
|
}
|