Files
bssapp/svc/routes/audit_helper.go
2026-02-11 17:46:22 +03:00

61 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package routes
import (
"bssapp-backend/auth"
"bssapp-backend/ctxkeys"
"bssapp-backend/internal/auditlog"
"bssapp-backend/permissions"
"bssapp-backend/repository"
"database/sql"
"encoding/json"
)
// auditLogFromRequest
// routes içinden çağrılır
// auditLogFromRequest
// routes içinden çağrılır
func auditLogFromRequest(
ctx any,
db *sql.DB,
actionType string,
meta map[string]any,
) {
al := auditlog.ActivityLog{
ActionType: actionType,
ActionCategory: "ADMIN",
IsSuccess: true,
}
// JWT → identity
if c, ok := ctx.(interface {
Value(any) any
}); ok {
if claims, ok := c.Value(ctxkeys.UserContextKey).(*auth.Claims); ok && claims != nil {
// ✅ TEK KİMLİK
al.DfUsrID = claims.ID
al.Username = claims.Username
al.RoleCode = claims.RoleCode
// 🔗 MULTI ROLE → ADMIN CHECK
roles, err := repository.GetUserRolesByUserID(db, claims.ID)
if err == nil {
_, isAdmin := permissions.ResolveEffectiveRoles(roles)
if isAdmin {
al.RoleCode = "admin"
}
}
}
}
// meta → description
if meta != nil {
if b, err := json.Marshal(meta); err == nil {
al.Description = string(b)
}
}
auditlog.Write(al)
}