Changeset 0.28.0.4 (#176)
This commit is contained in:
@@ -29,25 +29,28 @@ func (s *AuditStore) Log(ctx context.Context, entry *models.AuditEntry) error {
|
||||
}
|
||||
|
||||
func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]models.AuditEntry, int, error) {
|
||||
b := NewSelect("id, actor_id, action, resource_type, resource_id, metadata, ip_address, user_agent, created_at", "audit_log")
|
||||
b := NewSelect(`al.id, al.actor_id, COALESCE(u.username, '') AS actor_name,
|
||||
al.action, al.resource_type, al.resource_id, al.metadata,
|
||||
al.ip_address, al.user_agent, al.created_at`, "audit_log al")
|
||||
b.Join("LEFT JOIN users u ON al.actor_id = u.id")
|
||||
|
||||
if opts.ActorID != "" {
|
||||
b.Where("actor_id = ?", opts.ActorID)
|
||||
b.Where("al.actor_id = ?", opts.ActorID)
|
||||
}
|
||||
if opts.Action != "" {
|
||||
b.Where("action = ?", opts.Action)
|
||||
b.Where("al.action = ?", opts.Action)
|
||||
}
|
||||
if opts.ResourceType != "" {
|
||||
b.Where("resource_type = ?", opts.ResourceType)
|
||||
b.Where("al.resource_type = ?", opts.ResourceType)
|
||||
}
|
||||
if opts.ResourceID != "" {
|
||||
b.Where("resource_id = ?", opts.ResourceID)
|
||||
b.Where("al.resource_id = ?", opts.ResourceID)
|
||||
}
|
||||
if opts.Since != nil {
|
||||
b.Where("created_at >= ?", *opts.Since)
|
||||
b.Where("al.created_at >= ?", *opts.Since)
|
||||
}
|
||||
if opts.Until != nil {
|
||||
b.Where("created_at <= ?", *opts.Until)
|
||||
b.Where("al.created_at <= ?", *opts.Until)
|
||||
}
|
||||
|
||||
countQ, countArgs := b.CountBuild()
|
||||
@@ -55,7 +58,7 @@ func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]m
|
||||
DB.QueryRowContext(ctx, countQ, countArgs...).Scan(&total)
|
||||
|
||||
if opts.Sort == "" {
|
||||
b.OrderBy("created_at", "DESC")
|
||||
b.OrderBy("al.created_at", "DESC")
|
||||
}
|
||||
b.Paginate(opts.ListOptions)
|
||||
|
||||
@@ -69,16 +72,40 @@ func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]m
|
||||
var result []models.AuditEntry
|
||||
for rows.Next() {
|
||||
var e models.AuditEntry
|
||||
var actorID sql.NullString
|
||||
var actorID, actorName sql.NullString
|
||||
var metadataJSON string
|
||||
err := rows.Scan(&e.ID, &actorID, &e.Action, &e.ResourceType, &e.ResourceID,
|
||||
err := rows.Scan(&e.ID, &actorID, &actorName, &e.Action, &e.ResourceType, &e.ResourceID,
|
||||
&metadataJSON, &e.IPAddress, &e.UserAgent, st(&e.CreatedAt))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
e.ActorID = NullableStringPtr(actorID)
|
||||
if actorName.Valid && actorName.String != "" {
|
||||
e.ActorName = &actorName.String
|
||||
}
|
||||
json.Unmarshal([]byte(metadataJSON), &e.Metadata)
|
||||
result = append(result, e)
|
||||
}
|
||||
return result, total, rows.Err()
|
||||
}
|
||||
|
||||
func (s *AuditStore) ListActions(ctx context.Context) ([]string, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT DISTINCT action FROM audit_log ORDER BY action ASC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var actions []string
|
||||
for rows.Next() {
|
||||
var a string
|
||||
if rows.Scan(&a) == nil {
|
||||
actions = append(actions, a)
|
||||
}
|
||||
}
|
||||
if actions == nil {
|
||||
actions = []string{}
|
||||
}
|
||||
return actions, rows.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user