add client IP to audit logs
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-19 10:56:37 +11:00
parent 317e0ab83d
commit 8799f0f796
6 changed files with 32 additions and 12 deletions

View File

@@ -12,10 +12,11 @@ type Audit struct {
SecretId int `db:"SecretId" json:"secretId"`
EventText string `db:"EventText" json:"eventText"`
EventTime time.Time `db:"EventTime" json:"eventTime"`
IpAddress string `db:"IpAddress" json:"ipAddress"`
}
// AutidLogAdd adds a new audit record to the database
func (a *Audit) AutidLogAdd() (*Audit, error) {
// AuditLogAdd adds a new audit record to the database
func (a *Audit) AuditLogAdd() (*Audit, error) {
var err error
// Populate timestamp field if not already set
@@ -23,16 +24,16 @@ func (a *Audit) AutidLogAdd() (*Audit, error) {
a.EventTime = time.Now().UTC()
}
result, err := db.NamedExec(("INSERT INTO audit (UserId, SecretId, EventText, EventTime) VALUES (:UserId, :SecretId, :EventText, :EventTime);"), a)
result, err := db.NamedExec(("INSERT INTO audit (UserId, SecretId, EventText, EventTime, IpAddress) VALUES (:UserId, :SecretId, :EventText, :EventTime, :IpAddress);"), a)
if err != nil {
log.Printf("AutidLogAdd error executing sql record : '%s'\n", err)
log.Printf("AuditLogAdd error executing sql record : '%s'\n", err)
return &Audit{}, err
} else {
affected, _ := result.RowsAffected()
id, _ := result.LastInsertId()
a.AuditId = int(id)
log.Printf("AutidLogAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
log.Printf("AuditLogAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
}
return a, nil