2024-09-09 10:29:41 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql/driver"
|
|
|
|
|
"encoding/json"
|
2025-10-24 12:14:19 +02:00
|
|
|
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/utils"
|
2024-09-09 10:29:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AuditLog struct {
|
|
|
|
|
Base
|
|
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
Event AuditLogEvent `sortable:"true" filterable:"true"`
|
2025-06-30 05:04:30 -07:00
|
|
|
IpAddress *string `sortable:"true"`
|
2025-01-11 20:14:12 +01:00
|
|
|
Country string `sortable:"true"`
|
|
|
|
|
City string `sortable:"true"`
|
|
|
|
|
UserAgent string `sortable:"true"`
|
2025-04-03 10:11:49 -05:00
|
|
|
Username string `gorm:"-"`
|
2024-09-09 10:29:41 +02:00
|
|
|
Data AuditLogData
|
2025-04-03 10:11:49 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
UserID string `filterable:"true"`
|
2025-04-03 10:11:49 -05:00
|
|
|
User User
|
2024-09-09 10:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-27 16:48:36 +01:00
|
|
|
type AuditLogData map[string]string //nolint:recvcheck
|
2024-09-09 10:29:41 +02:00
|
|
|
|
2025-03-27 16:48:36 +01:00
|
|
|
type AuditLogEvent string //nolint:recvcheck
|
2024-09-09 10:29:41 +02:00
|
|
|
|
|
|
|
|
const (
|
2025-04-25 12:14:51 -05:00
|
|
|
AuditLogEventSignIn AuditLogEvent = "SIGN_IN"
|
|
|
|
|
AuditLogEventOneTimeAccessTokenSignIn AuditLogEvent = "TOKEN_SIGN_IN"
|
2025-06-27 15:01:10 -05:00
|
|
|
AuditLogEventAccountCreated AuditLogEvent = "ACCOUNT_CREATED"
|
2025-04-25 12:14:51 -05:00
|
|
|
AuditLogEventClientAuthorization AuditLogEvent = "CLIENT_AUTHORIZATION"
|
|
|
|
|
AuditLogEventNewClientAuthorization AuditLogEvent = "NEW_CLIENT_AUTHORIZATION"
|
|
|
|
|
AuditLogEventDeviceCodeAuthorization AuditLogEvent = "DEVICE_CODE_AUTHORIZATION"
|
|
|
|
|
AuditLogEventNewDeviceCodeAuthorization AuditLogEvent = "NEW_DEVICE_CODE_AUTHORIZATION"
|
2025-11-16 14:51:38 -08:00
|
|
|
AuditLogEventPasskeyAdded AuditLogEvent = "PASSKEY_ADDED"
|
|
|
|
|
AuditLogEventPasskeyRemoved AuditLogEvent = "PASSKEY_REMOVED"
|
2024-09-09 10:29:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Scan and Value methods for GORM to handle the custom type
|
|
|
|
|
|
2025-04-04 10:05:32 -07:00
|
|
|
func (e *AuditLogEvent) Scan(value any) error {
|
2024-09-09 10:29:41 +02:00
|
|
|
*e = AuditLogEvent(value.(string))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e AuditLogEvent) Value() (driver.Value, error) {
|
|
|
|
|
return string(e), nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 10:05:32 -07:00
|
|
|
func (d *AuditLogData) Scan(value any) error {
|
2025-10-24 12:14:19 +02:00
|
|
|
return utils.UnmarshalJSONFromDatabase(d, value)
|
2024-09-09 10:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-27 22:41:01 +01:00
|
|
|
func (d AuditLogData) Value() (driver.Value, error) {
|
2024-09-09 10:29:41 +02:00
|
|
|
return json.Marshal(d)
|
|
|
|
|
}
|