♻️ Refactored event audit system

This commit is contained in:
2025-03-15 16:37:47 +08:00
parent 35e5eadb05
commit 32e91e2601
15 changed files with 113 additions and 69 deletions

View File

@ -3,13 +3,14 @@ package authkit
import (
"context"
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/proto"
"github.com/gofiber/fiber/v2"
)
func AddEvent(nx *nex.Conn, userId uint, action, target, ip, ua string) error {
func AddEvent(nx *nex.Conn, userId uint, action string, meta map[string]any, ip, ua string) error {
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil {
return fmt.Errorf("failed to get auth service client: %v", err)
@ -17,14 +18,14 @@ func AddEvent(nx *nex.Conn, userId uint, action, target, ip, ua string) error {
_, err = proto.NewAuditServiceClient(conn).RecordEvent(context.Background(), &proto.RecordEventRequest{
UserId: uint64(userId),
Action: action,
Target: target,
Metadata: nex.EncodeMap(meta),
Ip: ip,
UserAgent: ua,
})
return err
}
func AddEventExt(nx *nex.Conn, action, target string, c *fiber.Ctx) error {
func AddEventExt(nx *nex.Conn, action string, meta map[string]any, c *fiber.Ctx) error {
user, ok := c.Locals("nex_user").(*sec.UserInfo)
if !ok {
return fmt.Errorf("failed to get user info, make sure you call this method behind the ContextMiddleware")
@ -37,7 +38,7 @@ func AddEventExt(nx *nex.Conn, action, target string, c *fiber.Ctx) error {
_, err = proto.NewAuditServiceClient(conn).RecordEvent(context.Background(), &proto.RecordEventRequest{
UserId: uint64(user.ID),
Action: action,
Target: target,
Metadata: nex.EncodeMap(meta),
Ip: c.IP(),
UserAgent: c.Get(fiber.HeaderUserAgent),
})

View File

@ -1,13 +1,15 @@
package models
import "gorm.io/datatypes"
type ActionEvent struct {
BaseModel
Type string `json:"type"`
Target string `json:"target"`
Location string `json:"location"`
IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
Type string `json:"type"`
Metadata datatypes.JSONMap `json:"metadata"`
Location string `json:"location"`
IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`