-
Notifications
You must be signed in to change notification settings - Fork 14
/
admin_audit_log.go
166 lines (157 loc) · 4.01 KB
/
admin_audit_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2024 The Bucketeer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate mockgen -source=$GOFILE -package=mock -destination=./mock/$GOFILE
package v2
import (
"context"
"errors"
"fmt"
"strings"
"github.com/bucketeer-io/bucketeer/pkg/auditlog/domain"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
proto "github.com/bucketeer-io/bucketeer/proto/auditlog"
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
)
var (
ErrAdminAuditLogAlreadyExists = errors.New("auditlog: admin auditlog already exists")
)
type AdminAuditLogStorage interface {
CreateAdminAuditLogs(ctx context.Context, auditLogs []*domain.AuditLog) error
ListAdminAuditLogs(
ctx context.Context,
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
) ([]*proto.AuditLog, int, int64, error)
}
type adminAuditLogStorage struct {
qe mysql.QueryExecer
}
func NewAdminAuditLogStorage(qe mysql.QueryExecer) AdminAuditLogStorage {
return &adminAuditLogStorage{qe}
}
func (s *adminAuditLogStorage) CreateAdminAuditLogs(ctx context.Context, auditLogs []*domain.AuditLog) error {
if len(auditLogs) == 0 {
return nil
}
var query strings.Builder
query.WriteString(`
INSERT INTO admin_audit_log (
id,
timestamp,
entity_type,
entity_id,
type,
event,
editor,
options
) VALUES
`)
args := []interface{}{}
for i, al := range auditLogs {
if i != 0 {
query.WriteString(",")
}
query.WriteString(" (?, ?, ?, ?, ?, ?, ?, ?)")
args = append(
args,
al.Id,
al.Timestamp,
int32(al.EntityType),
al.EntityId,
int32(al.Type),
mysql.JSONObject{Val: al.Event},
mysql.JSONObject{Val: al.Editor},
mysql.JSONObject{Val: al.Options},
)
}
_, err := s.qe.ExecContext(ctx, query.String(), args...)
if err != nil {
if err == mysql.ErrDuplicateEntry {
return ErrAdminAuditLogAlreadyExists
}
return err
}
return nil
}
func (s *adminAuditLogStorage) ListAdminAuditLogs(
ctx context.Context,
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
) ([]*proto.AuditLog, int, int64, error) {
whereSQL, whereArgs := mysql.ConstructWhereSQLString(whereParts)
orderBySQL := mysql.ConstructOrderBySQLString(orders)
limitOffsetSQL := mysql.ConstructLimitOffsetSQLString(limit, offset)
query := fmt.Sprintf(`
SELECT
id,
timestamp,
entity_type,
entity_id,
type,
event,
editor,
options
FROM
admin_audit_log
%s %s %s
`, whereSQL, orderBySQL, limitOffsetSQL,
)
rows, err := s.qe.QueryContext(ctx, query, whereArgs...)
if err != nil {
return nil, 0, 0, err
}
defer rows.Close()
auditLogs := make([]*proto.AuditLog, 0, limit)
for rows.Next() {
auditLog := proto.AuditLog{}
var et int32
var t int32
err := rows.Scan(
&auditLog.Id,
&auditLog.Timestamp,
&et,
&auditLog.EntityId,
&t,
&mysql.JSONObject{Val: &auditLog.Event},
&mysql.JSONObject{Val: &auditLog.Editor},
&mysql.JSONObject{Val: &auditLog.Options},
)
if err != nil {
return nil, 0, 0, err
}
auditLog.EntityType = eventproto.Event_EntityType(et)
auditLog.Type = eventproto.Event_Type(t)
auditLogs = append(auditLogs, &auditLog)
}
if rows.Err() != nil {
return nil, 0, 0, err
}
nextOffset := offset + len(auditLogs)
var totalCount int64
countQuery := fmt.Sprintf(`
SELECT
COUNT(1)
FROM
admin_audit_log
%s %s
`, whereSQL, orderBySQL,
)
err = s.qe.QueryRowContext(ctx, countQuery, whereArgs...).Scan(&totalCount)
if err != nil {
return nil, 0, 0, err
}
return auditLogs, nextOffset, totalCount, nil
}