feat: add GET /v2/admin/audit-log

This commit is contained in:
sam 2023-12-28 14:28:59 +01:00
parent 2dd37f15bf
commit 73d132eb4c
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
3 changed files with 66 additions and 1 deletions

View file

@ -0,0 +1,65 @@
package admin
import (
"net/http"
"codeberg.org/pronounscc/pronouns.cc/backend/common"
"codeberg.org/pronounscc/pronouns.cc/backend/db"
"codeberg.org/pronounscc/pronouns.cc/backend/log"
"codeberg.org/pronounscc/pronouns.cc/backend/server"
"emperror.dev/errors"
"github.com/go-chi/render"
)
type AuditLogEntryResponse struct {
ID common.AuditLogID `json:"id"`
TargetUserID common.UserID `json:"target_user_id"`
TargetMemberID *common.MemberID `json:"target_member_id"`
ModeratorID common.UserID `json:"moderator_id"`
ReportID *int64 `json:"report_id"`
Reason string `json:"reason"`
ActionTaken string `json:"action_taken"`
ClearedData *db.AuditLogClearedData `json:"cleared_data"`
}
func dbAuditLogToResponse(e db.AuditLogEntry) AuditLogEntryResponse {
return AuditLogEntryResponse{
ID: e.ID,
TargetUserID: e.TargetUserID,
TargetMemberID: e.TargetMemberID,
ModeratorID: e.ModeratorID,
ReportID: e.ReportID,
Reason: e.Reason,
ActionTaken: e.ActionTaken,
ClearedData: e.ClearedData,
}
}
func dbAuditLogsToResponse(es []db.AuditLogEntry) []AuditLogEntryResponse {
resps := make([]AuditLogEntryResponse, len(es))
for i := range es {
resps[i] = dbAuditLogToResponse(es[i])
}
return resps
}
func (s *Server) AuditLog(w http.ResponseWriter, r *http.Request) (err error) {
beforeID := common.AuditLogID(0)
if s := r.FormValue("before"); s != "" {
sf, err := common.ParseSnowflake(s)
if err != nil {
return server.NewV2Error(server.ErrBadRequest, "", server.NewModelParseError("query", "Invalid snowflake"))
}
beforeID = common.AuditLogID(sf)
}
es, err := s.DB.AuditLog(r.Context(), beforeID)
if err != nil {
log.Errorf("getting audit logs before %v: %v", beforeID, err)
return errors.Wrap(err, "getting audit logs")
}
render.JSON(w, r, dbAuditLogsToResponse(es))
return nil
}

View file

@ -22,6 +22,7 @@ func Mount(srv *server.Server, r chi.Router) {
s := &Server{Server: srv} s := &Server{Server: srv}
r.With(MustAdmin).Route("/admin", func(r chi.Router) { r.With(MustAdmin).Route("/admin", func(r chi.Router) {
r.Get("/audit-log", server.WrapHandler(s.AuditLog))
r.Post("/users/{id}/actions", server.WrapHandler(s.UserAction)) r.Post("/users/{id}/actions", server.WrapHandler(s.UserAction))
}) })
} }

View file

@ -47,7 +47,6 @@ func WrapHandler(hn func(w http.ResponseWriter, r *http.Request) error) http.Han
if isExpectedError(err) { if isExpectedError(err) {
log.Infof("expected error in handler for %v %v, ignoring", rctx.RouteMethod, rctx.RoutePattern()) log.Infof("expected error in handler for %v %v, ignoring", rctx.RouteMethod, rctx.RoutePattern())
} else { } else {
log.Errorf("error in handler for %v %v: %v", rctx.RouteMethod, rctx.RoutePattern(), err)
eventID = hub.CaptureException(err) eventID = hub.CaptureException(err)
} }
apiErr := APIError{ID: eventID, Code: ErrInternalServerError} apiErr := APIError{ID: eventID, Code: ErrInternalServerError}