241e3b5f2d
- Introduced a new `auditlog` package to handle audit logging for user actions, including creation, updates, deletions, and authentication events. - Enhanced existing services (customer, user) to log relevant actions using the new audit logger, capturing details such as actor ID, action type, target type, and success status. - Added middleware to enrich request context with metadata for audit logging, ensuring comprehensive tracking of user interactions. - Integrated Elasticsearch for persistent storage of audit logs, with fallback to file-only logging if Elasticsearch is unavailable. - Updated API documentation to include new audit log endpoints for administrative access. This update significantly improves the system's ability to track and audit user actions, enhancing security and accountability within the application.
70 lines
2.5 KiB
Go
70 lines
2.5 KiB
Go
package auditlog
|
|
|
|
import (
|
|
"tm/pkg/response"
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// Handler handles HTTP requests for audit log operations.
|
|
type Handler struct {
|
|
service Service
|
|
}
|
|
|
|
// NewHandler creates an audit log handler.
|
|
func NewHandler(service Service) *Handler {
|
|
return &Handler{service: service}
|
|
}
|
|
|
|
// Search returns paginated audit logs with optional filters.
|
|
// @Summary Search audit logs
|
|
// @Description Retrieve paginated user action audit logs with optional filtering by actor, action, target, and time range
|
|
// @Tags Admin-AuditLogs
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param actor_id query string false "Filter by actor ID"
|
|
// @Param actor_type query string false "Filter by actor type" Enums(admin,customer,system)
|
|
// @Param action query string false "Filter by action (e.g. auth.login.success)"
|
|
// @Param target_type query string false "Filter by target type"
|
|
// @Param target_id query string false "Filter by target ID"
|
|
// @Param from query int64 false "Filter by timestamp from (Unix seconds)"
|
|
// @Param to query int64 false "Filter by timestamp to (Unix seconds)"
|
|
// @Param limit query int false "Number of items per page (default: 10, max: 100)"
|
|
// @Param offset query int false "Number of items to skip (default: 0)"
|
|
// @Success 200 {object} response.APIResponse{data=ListResponse} "Audit logs retrieved successfully"
|
|
// @Failure 400 {object} response.APIResponse "Bad request"
|
|
// @Failure 401 {object} response.APIResponse "Unauthorized"
|
|
// @Failure 403 {object} response.APIResponse "Forbidden"
|
|
// @Failure 500 {object} response.APIResponse "Internal server error"
|
|
// @Router /admin/v1/audit-logs [get]
|
|
func (h *Handler) Search(c echo.Context) error {
|
|
form, err := response.Parse[SearchForm](c)
|
|
if err != nil {
|
|
return response.ValidationError(c, "Invalid request data", err.Error())
|
|
}
|
|
|
|
if _, err := govalidator.ValidateStruct(form); err != nil {
|
|
return response.ValidationError(c, "Validation failed", err.Error())
|
|
}
|
|
|
|
pagination, err := response.NewPagination(c)
|
|
if err != nil {
|
|
return response.PaginationBadRequest(c, err)
|
|
}
|
|
|
|
result, err := h.service.Search(c.Request().Context(), *form, pagination)
|
|
if err != nil {
|
|
return response.InternalServerError(c, "Failed to retrieve audit logs")
|
|
}
|
|
|
|
return response.SuccessWithMeta(c, result, &response.Meta{
|
|
Total: int(result.Meta.Total),
|
|
Limit: result.Meta.Limit,
|
|
Offset: result.Meta.Offset,
|
|
Page: result.Meta.Page,
|
|
Pages: result.Meta.Pages,
|
|
}, "audit logs retrieved successfully")
|
|
}
|