2017-07-24 16:24:21 +01:00
|
|
|
// 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
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2019-10-21 10:33:37 +01:00
|
|
|
"database/sql"
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
"github.com/documize/community/domain"
|
2018-09-26 17:59:56 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/model/audit"
|
2017-07-24 16:24:21 +01:00
|
|
|
)
|
|
|
|
|
2018-09-26 17:59:56 +01:00
|
|
|
// Store provides data access to audit log information.
|
|
|
|
type Store struct {
|
|
|
|
store.Context
|
2018-09-27 15:14:48 +01:00
|
|
|
store.AuditStorer
|
2017-07-26 10:50:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 15:43:57 +00:00
|
|
|
// Record adds event entry for specified user using own DB TX.
|
2018-09-26 17:59:56 +01:00
|
|
|
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
|
2017-07-24 16:24:21 +01:00
|
|
|
e.Created = time.Now().UTC()
|
2017-07-26 10:50:26 +01:00
|
|
|
e.IP = ctx.ClientIP
|
2017-07-24 16:24:21 +01:00
|
|
|
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")
|
2017-07-24 16:24:21 +01:00
|
|
|
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)
|
2017-07-24 16:24:21 +01:00
|
|
|
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)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 10:33:37 +01:00
|
|
|
s.Runtime.Commit(tx)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|