Dispatch logs an entry to the audit log asynchronously. The actor is resolved from the request context (user, API token or system); the generation and publishing is delegated to the shared auditor.Dispatcher.
(ctx context.Context, entry auditor.LogEntry, orgID *uuid.UUID)
| 52 | // from the request context (user, API token or system); the generation and |
| 53 | // publishing is delegated to the shared auditor.Dispatcher. |
| 54 | func (uc *AuditorUseCase) Dispatch(ctx context.Context, entry auditor.LogEntry, orgID *uuid.UUID) { |
| 55 | ctx, span := otelx.Start(ctx, auditorTracer, "AuditorUseCase.Dispatch") |
| 56 | defer span.End() |
| 57 | |
| 58 | // dynamically load user information from the context |
| 59 | var opts []auditor.GeneratorOption |
| 60 | var gotActor bool |
| 61 | |
| 62 | // Determine actor from context |
| 63 | switch { |
| 64 | case entities.CurrentUser(ctx) != nil: |
| 65 | user := entities.CurrentUser(ctx) |
| 66 | parsedUUID, _ := uuid.Parse(user.ID) |
| 67 | fullName := strings.TrimSpace(fmt.Sprintf("%s %s", user.FirstName, user.LastName)) |
| 68 | opts = append(opts, auditor.WithActor(auditor.ActorTypeUser, parsedUUID, user.Email, fullName)) |
| 69 | gotActor = true |
| 70 | case entities.CurrentAPIToken(ctx) != nil: |
| 71 | apiToken := entities.CurrentAPIToken(ctx) |
| 72 | parsedUUID, _ := uuid.Parse(apiToken.ID) |
| 73 | opts = append(opts, auditor.WithActor(auditor.ActorTypeAPIToken, parsedUUID, "", apiToken.Name)) |
| 74 | gotActor = true |
| 75 | default: |
| 76 | opts = append(opts, auditor.WithActor(auditor.ActorTypeSystem, uuid.Nil, "", "")) |
| 77 | } |
| 78 | |
| 79 | if !gotActor && entry.RequiresActor() { |
| 80 | uc.log.Warn("failed to get actor information, required by the audit log entry") |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | if orgID != nil { |
| 85 | opts = append(opts, auditor.WithOrgID(*orgID)) |
| 86 | } |
| 87 | |
| 88 | uc.dispatcher.Dispatch(entry, opts...) |
| 89 | } |
no test coverage detected