Files
amsterdam/database/audit.go
T

97 lines
2.5 KiB
Go

/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// The database package contains database management and storage logic.
package database
import (
"fmt"
"time"
)
// AuditRecord holds an audit record instance.
type AuditRecord struct {
Record int64 `db:"record"`
OnDate time.Time `db:"on_date"`
Event int32 `db:"event"`
Uid int32 `db:"uid"`
CommId int32 `db:"commid"`
IP *string `db:"ip"`
Data1 *string `db:"data1"`
Data2 *string `db:"data2"`
Data3 *string `db:"data3"`
Data4 *string `db:"data4"`
}
// These are the audit record types.
const (
AuditPublishToFrontPage = 1
AuditLoginOK = 101
AuditLoginFail = 102
AuditAccountCreated = 103
AuditVerifyEmailOK = 104
AuditVerifyEmailFail = 105
AuditSetUserContactInfo = 106
AuditResendEmailConfirm = 107
AuditChangePassword = 108
AuditAdminSetUserContectInfo = 109
AuditAdminChangeUserPassword = 110
AuditAdminChangeUserAccount = 111
AuditAdminSetAccountSecurity = 112
AuditAdminLockUnlockAccount = 113
)
/* AmNewAudit creates a new audit record.
* Parameters:
* rectype - Audit record type.
* uid - User ID of the user.
* ip - User's IP address.
* data - Argument data values for the audit record.
* Returns:
* The audit record pointer.
*/
func AmNewAudit(rectype int32, uid int32, ip string, data ...string) *AuditRecord {
rc := AuditRecord{Event: rectype, Uid: uid, CommId: 0}
if len(ip) > 0 {
rc.IP = &ip
}
if data != nil {
l := len(data)
if l > 0 {
rc.Data1 = &(data[0])
}
if l > 1 {
rc.Data2 = &(data[1])
}
if l > 2 {
rc.Data3 = &(data[2])
}
if l > 3 {
rc.Data4 = &(data[3])
}
}
return &rc
}
// Store stores the audit record in the database.
func (ar *AuditRecord) Store() error {
if ar.Record > 0 {
return fmt.Errorf("audit record %d already stored", ar.Record)
}
moment := time.Now()
rs, err := amdb.Exec(`INSERT INTO audit (on_date, event, uid, commid, ip, data1, data2, data3, data4)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);`, moment, ar.Event, ar.Uid, ar.CommId, ar.IP,
ar.Data1, ar.Data2, ar.Data3, ar.Data4)
if err != nil {
return err
}
ar.Record, _ = rs.LastInsertId()
ar.OnDate = moment
return nil
}