1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/domain/audit/store.go

57 lines
1.4 KiB
Go
Raw Permalink Normal View History

// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
2017-07-26 10:50:26 +01:00
// Package audit records user events.
package audit
import (
"time"
2019-10-21 10:33:37 +01:00
"database/sql"
"github.com/documize/community/domain"
"github.com/documize/community/domain/store"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/model/audit"
)
// Store provides data access to audit log information.
type Store struct {
store.Context
store.AuditStorer
2017-07-26 10:50:26 +01:00
}
// Record adds event entry for specified user using own DB TX.
func (s Store) Record(ctx domain.RequestContext, t audit.EventType) {
2017-07-26 10:50:26 +01:00
e := audit.AppEvent{}
e.OrgID = ctx.OrgID
e.UserID = ctx.UserID
e.Created = time.Now().UTC()
2017-07-26 10:50:26 +01:00
e.IP = ctx.ClientIP
e.Type = string(t)
2019-10-21 10:33:37 +01:00
tx, ok := s.Runtime.StartTx(sql.LevelReadUncommitted)
if !ok {
s.Runtime.Log.Info("unable to start transaction")
return
}
2019-10-21 10:33:37 +01:00
_, err := tx.Exec(s.Bind("INSERT INTO dmz_audit_log (c_orgid, c_userid, c_eventtype, c_ip, c_created) VALUES (?, ?, ?, ?, ?)"),
2017-09-25 14:37:11 +01:00
e.OrgID, e.UserID, e.Type, e.IP, e.Created)
if err != nil {
2019-10-21 10:33:37 +01:00
s.Runtime.Rollback(tx)
2017-09-25 14:37:11 +01:00
s.Runtime.Log.Error("prepare audit insert", err)
return
}
2019-10-21 10:33:37 +01:00
s.Runtime.Commit(tx)
return
}