fix: enrich activity log actors and add admin integration guide
Record explicit actor on login/register flows and return actor_name, actor_email, and actor_kind from activity log APIs so admin audit rows no longer show System for authenticated events. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
36594e03d6
commit
eb0c87059f
1105
docs/activity-logs-admin-panel-integration.md
Normal file
1105
docs/activity-logs-admin-panel-integration.md
Normal file
File diff suppressed because it is too large
Load Diff
113
internal/web_server/handlers/activity_log_actor.go
Normal file
113
internal/web_server/handlers/activity_log_actor.go
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"Yimaru-Backend/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
type activityLogActorDisplay struct {
|
||||||
|
Name *string
|
||||||
|
Email *string
|
||||||
|
Kind *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func userDisplayName(u domain.User) string {
|
||||||
|
name := strings.TrimSpace(u.FirstName + " " + u.LastName)
|
||||||
|
if name != "" {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
if u.NickName != "" {
|
||||||
|
return u.NickName
|
||||||
|
}
|
||||||
|
if u.Email != "" {
|
||||||
|
return u.Email
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func teamMemberDisplayName(m domain.TeamMember) string {
|
||||||
|
name := strings.TrimSpace(m.FirstName + " " + m.LastName)
|
||||||
|
if name != "" {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
return m.Email
|
||||||
|
}
|
||||||
|
|
||||||
|
func preferTeamMemberActorLookup(log domain.ActivityLog, role string) bool {
|
||||||
|
switch domain.Role(role) {
|
||||||
|
case domain.RoleInstructor, domain.RoleSupport:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if log.ResourceType == string(domain.ResourceTeamMember) &&
|
||||||
|
log.ResourceID != nil &&
|
||||||
|
log.ActorID != nil &&
|
||||||
|
*log.ResourceID == *log.ActorID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if log.ResourceType == string(domain.ResourceTeamMember) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) resolveActivityLogActor(ctx context.Context, log domain.ActivityLog) activityLogActorDisplay {
|
||||||
|
if log.ActorID == nil {
|
||||||
|
return activityLogActorDisplay{}
|
||||||
|
}
|
||||||
|
|
||||||
|
role := ""
|
||||||
|
if log.ActorRole != nil {
|
||||||
|
role = *log.ActorRole
|
||||||
|
}
|
||||||
|
|
||||||
|
tryTeamFirst := preferTeamMemberActorLookup(log, role)
|
||||||
|
if tryTeamFirst {
|
||||||
|
if display, ok := h.lookupTeamMemberActor(ctx, *log.ActorID); ok {
|
||||||
|
return display
|
||||||
|
}
|
||||||
|
if display, ok := h.lookupUserActor(ctx, *log.ActorID); ok {
|
||||||
|
return display
|
||||||
|
}
|
||||||
|
return activityLogActorDisplay{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if display, ok := h.lookupUserActor(ctx, *log.ActorID); ok {
|
||||||
|
return display
|
||||||
|
}
|
||||||
|
if display, ok := h.lookupTeamMemberActor(ctx, *log.ActorID); ok {
|
||||||
|
return display
|
||||||
|
}
|
||||||
|
return activityLogActorDisplay{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) lookupUserActor(ctx context.Context, id int64) (activityLogActorDisplay, bool) {
|
||||||
|
user, err := h.userSvc.GetUserByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return activityLogActorDisplay{}, false
|
||||||
|
}
|
||||||
|
name := userDisplayName(user)
|
||||||
|
kind := "user"
|
||||||
|
email := user.Email
|
||||||
|
return activityLogActorDisplay{
|
||||||
|
Name: &name,
|
||||||
|
Email: &email,
|
||||||
|
Kind: &kind,
|
||||||
|
}, name != "" || email != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) lookupTeamMemberActor(ctx context.Context, id int64) (activityLogActorDisplay, bool) {
|
||||||
|
member, err := h.teamSvc.GetTeamMemberByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return activityLogActorDisplay{}, false
|
||||||
|
}
|
||||||
|
name := teamMemberDisplayName(member)
|
||||||
|
kind := "team_member"
|
||||||
|
email := member.Email
|
||||||
|
return activityLogActorDisplay{
|
||||||
|
Name: &name,
|
||||||
|
Email: &email,
|
||||||
|
Kind: &kind,
|
||||||
|
}, name != "" || email != ""
|
||||||
|
}
|
||||||
|
|
@ -29,6 +29,14 @@ func (h *Handler) recordActivity(c *fiber.Ctx, action domain.ActivityAction, res
|
||||||
go h.activityLogSvc.RecordAction(context.Background(), actorID, actorRole, action, resourceType, resourceID, message, metadata, ip, ua)
|
go h.activityLogSvc.RecordAction(context.Background(), actorID, actorRole, action, resourceType, resourceID, message, metadata, ip, ua)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// recordActivityAs records an activity for a known actor (e.g. login/register before JWT is in context).
|
||||||
|
func (h *Handler) recordActivityAs(c *fiber.Ctx, actorID int64, actorRole domain.Role, action domain.ActivityAction, resourceType domain.ResourceType, resourceID *int64, message string, metadata json.RawMessage) {
|
||||||
|
_, _, ip, ua := activityActorFromCtx(c)
|
||||||
|
id := actorID
|
||||||
|
roleStr := string(actorRole)
|
||||||
|
h.recordActivityWithActor(&id, &roleStr, ip, ua, action, resourceType, resourceID, message, metadata)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) recordActivityWithActor(actorID *int64, actorRole *string, ip, ua *string, action domain.ActivityAction, resourceType domain.ResourceType, resourceID *int64, message string, metadata json.RawMessage) {
|
func (h *Handler) recordActivityWithActor(actorID *int64, actorRole *string, ip, ua *string, action domain.ActivityAction, resourceType domain.ResourceType, resourceID *int64, message string, metadata json.RawMessage) {
|
||||||
go h.activityLogSvc.RecordAction(context.Background(), actorID, actorRole, action, resourceType, resourceID, message, metadata, ip, ua)
|
go h.activityLogSvc.RecordAction(context.Background(), actorID, actorRole, action, resourceType, resourceID, message, metadata, ip, ua)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"Yimaru-Backend/internal/domain"
|
"Yimaru-Backend/internal/domain"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -13,6 +14,9 @@ type activityLogRes struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
ActorID *int64 `json:"actor_id,omitempty"`
|
ActorID *int64 `json:"actor_id,omitempty"`
|
||||||
ActorRole *string `json:"actor_role,omitempty"`
|
ActorRole *string `json:"actor_role,omitempty"`
|
||||||
|
ActorName *string `json:"actor_name,omitempty"`
|
||||||
|
ActorEmail *string `json:"actor_email,omitempty"`
|
||||||
|
ActorKind *string `json:"actor_kind,omitempty"`
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
ResourceType string `json:"resource_type"`
|
ResourceType string `json:"resource_type"`
|
||||||
ResourceID *int64 `json:"resource_id,omitempty"`
|
ResourceID *int64 `json:"resource_id,omitempty"`
|
||||||
|
|
@ -135,19 +139,7 @@ func (h *Handler) GetActivityLogs(c *fiber.Ctx) error {
|
||||||
|
|
||||||
var logResponses []activityLogRes
|
var logResponses []activityLogRes
|
||||||
for _, l := range logs {
|
for _, l := range logs {
|
||||||
logResponses = append(logResponses, activityLogRes{
|
logResponses = append(logResponses, h.toActivityLogRes(c.Context(), l))
|
||||||
ID: l.ID,
|
|
||||||
ActorID: l.ActorID,
|
|
||||||
ActorRole: l.ActorRole,
|
|
||||||
Action: l.Action,
|
|
||||||
ResourceType: l.ResourceType,
|
|
||||||
ResourceID: l.ResourceID,
|
|
||||||
Message: l.Message,
|
|
||||||
Metadata: l.Metadata,
|
|
||||||
IPAddress: l.IPAddress,
|
|
||||||
UserAgent: l.UserAgent,
|
|
||||||
CreatedAt: l.CreatedAt,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(domain.Response{
|
return c.JSON(domain.Response{
|
||||||
|
|
@ -191,10 +183,19 @@ func (h *Handler) GetActivityLogByID(c *fiber.Ctx) error {
|
||||||
|
|
||||||
return c.JSON(domain.Response{
|
return c.JSON(domain.Response{
|
||||||
Message: "Activity log retrieved successfully",
|
Message: "Activity log retrieved successfully",
|
||||||
Data: activityLogRes{
|
Data: h.toActivityLogRes(c.Context(), l),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) toActivityLogRes(ctx context.Context, l domain.ActivityLog) activityLogRes {
|
||||||
|
actor := h.resolveActivityLogActor(ctx, l)
|
||||||
|
return activityLogRes{
|
||||||
ID: l.ID,
|
ID: l.ID,
|
||||||
ActorID: l.ActorID,
|
ActorID: l.ActorID,
|
||||||
ActorRole: l.ActorRole,
|
ActorRole: l.ActorRole,
|
||||||
|
ActorName: actor.Name,
|
||||||
|
ActorEmail: actor.Email,
|
||||||
|
ActorKind: actor.Kind,
|
||||||
Action: l.Action,
|
Action: l.Action,
|
||||||
ResourceType: l.ResourceType,
|
ResourceType: l.ResourceType,
|
||||||
ResourceID: l.ResourceID,
|
ResourceID: l.ResourceID,
|
||||||
|
|
@ -203,6 +204,5 @@ func (h *Handler) GetActivityLogByID(c *fiber.Ctx) error {
|
||||||
IPAddress: l.IPAddress,
|
IPAddress: l.IPAddress,
|
||||||
UserAgent: l.UserAgent,
|
UserAgent: l.UserAgent,
|
||||||
CreatedAt: l.CreatedAt,
|
CreatedAt: l.CreatedAt,
|
||||||
},
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ func (h *Handler) GoogleAndroidLogin(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := loginRes.UserId
|
userID := loginRes.UserId
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Google Android login (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, userID, loginRes.Role, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Google Android login (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Login successful",
|
Message: "Login successful",
|
||||||
|
|
@ -152,7 +152,7 @@ func (h *Handler) AppleLogin(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := loginRes.UserId
|
userID := loginRes.UserId
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Apple login (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, userID, loginRes.Role, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Apple login (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Login successful",
|
Message: "Login successful",
|
||||||
|
|
@ -228,7 +228,7 @@ func (h *Handler) GoogleCallback(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := loginRes.UserId
|
userID := loginRes.UserId
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Google OAuth login (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, userID, loginRes.Role, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Google OAuth login (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Login successful",
|
Message: "Login successful",
|
||||||
|
|
@ -366,7 +366,7 @@ func (h *Handler) LoginUser(c *fiber.Ctx) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
userID := successRes.UserId
|
userID := successRes.UserId
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("User login (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, successRes.UserId, successRes.Role, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("User login (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Login successful",
|
Message: "Login successful",
|
||||||
|
|
@ -484,7 +484,7 @@ func (h *Handler) LoginAdmin(c *fiber.Ctx) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
userID := successRes.UserId
|
userID := successRes.UserId
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Admin login (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, successRes.UserId, successRes.Role, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Admin login (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return response.WriteJSON(c, fiber.StatusOK, "Login successful", res, nil)
|
return response.WriteJSON(c, fiber.StatusOK, "Login successful", res, nil)
|
||||||
}
|
}
|
||||||
|
|
@ -585,7 +585,7 @@ func (h *Handler) LoginSuper(c *fiber.Ctx) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
userID := successRes.UserId
|
userID := successRes.UserId
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Super-admin login (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, successRes.UserId, successRes.Role, domain.ActionAuthLogin, domain.ResourceAuth, &userID, fmt.Sprintf("Super-admin login (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return response.WriteJSON(c, fiber.StatusOK, "Login successful", res, nil)
|
return response.WriteJSON(c, fiber.StatusOK, "Login successful", res, nil)
|
||||||
}
|
}
|
||||||
|
|
@ -707,7 +707,7 @@ func (h *Handler) RefreshToken(c *fiber.Ctx) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
userID := user.ID
|
userID := user.ID
|
||||||
h.recordActivity(c, domain.ActionAuthTokenRefreshed, domain.ResourceAuth, &userID, fmt.Sprintf("Token refreshed for user %d", userID), nil)
|
h.recordActivityAs(c, user.ID, user.Role, domain.ActionAuthTokenRefreshed, domain.ResourceAuth, &userID, fmt.Sprintf("Token refreshed for user %d", userID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Token refreshed successfully",
|
Message: "Token refreshed successfully",
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ func (h *Handler) TeamMemberLogin(c *fiber.Ctx) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
memberID := loginRes.ID
|
memberID := loginRes.ID
|
||||||
h.recordActivity(c, domain.ActionAuthLogin, domain.ResourceTeamMember, &memberID, fmt.Sprintf("Team member login (ID: %d)", loginRes.ID), nil)
|
h.recordActivityAs(c, memberID, role, domain.ActionAuthLogin, domain.ResourceTeamMember, &memberID, fmt.Sprintf("Team member login (ID: %d)", loginRes.ID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Login successful",
|
Message: "Login successful",
|
||||||
|
|
@ -248,7 +248,7 @@ func (h *Handler) TeamMemberRefresh(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
memberID := member.ID
|
memberID := member.ID
|
||||||
h.recordActivity(c, domain.ActionAuthTokenRefreshed, domain.ResourceTeamMember, &memberID, fmt.Sprintf("Team member token refreshed (ID: %d)", member.ID), nil)
|
h.recordActivityAs(c, memberID, role, domain.ActionAuthTokenRefreshed, domain.ResourceTeamMember, &memberID, fmt.Sprintf("Team member token refreshed (ID: %d)", member.ID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Token refreshed successfully",
|
Message: "Token refreshed successfully",
|
||||||
|
|
|
||||||
|
|
@ -277,7 +277,8 @@ func (h *Handler) AcceptTeamInvitation(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
rid := member.ID
|
rid := member.ID
|
||||||
h.recordActivity(c, domain.ActionTeamMemberUpdated, domain.ResourceTeamMember, &rid, "Accepted team invitation: "+member.Email, nil)
|
role := mapTeamRoleToRole(member.TeamRole)
|
||||||
|
h.recordActivityAs(c, rid, role, domain.ActionTeamMemberUpdated, domain.ResourceTeamMember, &rid, "Accepted team invitation: "+member.Email, nil)
|
||||||
|
|
||||||
return c.JSON(domain.Response{
|
return c.JSON(domain.Response{
|
||||||
Message: "Team account activated successfully. You can now sign in.",
|
Message: "Team account activated successfully. You can now sign in.",
|
||||||
|
|
|
||||||
|
|
@ -1166,8 +1166,8 @@ func (h *Handler) RegisterUser(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
userID := created.ID
|
userID := created.ID
|
||||||
h.recordActivity(c, domain.ActionUserCreated, domain.ResourceUser, &userID, fmt.Sprintf("Registered user ID: %d", userID), nil)
|
h.recordActivityAs(c, created.ID, created.Role, domain.ActionUserCreated, domain.ResourceUser, &userID, fmt.Sprintf("Registered user ID: %d", userID), nil)
|
||||||
h.recordActivity(c, domain.ActionAuthRegister, domain.ResourceAuth, &userID, fmt.Sprintf("User registration (user ID: %d)", userID), nil)
|
h.recordActivityAs(c, created.ID, created.Role, domain.ActionAuthRegister, domain.ResourceAuth, &userID, fmt.Sprintf("User registration (user ID: %d)", userID), nil)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
||||||
Message: "Registration successful",
|
Message: "Registration successful",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user