|
| 1 | +package org.example.controller; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation; |
| 4 | +import io.swagger.v3.oas.annotations.Parameter; |
| 5 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 6 | +import org.springframework.http.ResponseEntity; |
| 7 | +import org.springframework.web.bind.annotation.*; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.UUID; |
| 13 | + |
| 14 | +@RestController |
| 15 | +@RequestMapping("/api/audit") |
| 16 | +@Tag(name = "Audit", description = "Audit logging and history") |
| 17 | +public class AuditController { |
| 18 | + |
| 19 | + @GetMapping("/logs") |
| 20 | + @Operation(summary = "Get audit log detail", description = "Returns detailed audit log entry") |
| 21 | + public ResponseEntity<Map<String, Object>> getLogDetail( |
| 22 | + @Parameter(description = "Log ID") @RequestParam(required = false) String id, |
| 23 | + @Parameter(description = "Page number") @RequestParam(defaultValue = "1") int page, |
| 24 | + @Parameter(description = "Page size") @RequestParam(defaultValue = "20") int size, |
| 25 | + @Parameter(description = "Filter by action") @RequestParam(required = false) String action) { |
| 26 | + |
| 27 | + if (id != null) { |
| 28 | + return ResponseEntity.ok(Map.of( |
| 29 | + "id", id, |
| 30 | + "timestamp", System.currentTimeMillis(), |
| 31 | + "action", "update", |
| 32 | + "resource", "monitor", |
| 33 | + "resourceId", "mon-123", |
| 34 | + "user", "admin", |
| 35 | + "ip", "192.168.1.100", |
| 36 | + "userAgent", "Mozilla/5.0", |
| 37 | + "status", "success", |
| 38 | + "details", Map.of( |
| 39 | + "before", Map.of("status", "inactive"), |
| 40 | + "after", Map.of("status", "active") |
| 41 | + ) |
| 42 | + )); |
| 43 | + } |
| 44 | + |
| 45 | + int safePage = Math.max(page, 1); |
| 46 | + int safeSize = Math.min(Math.max(size, 1), 100); |
| 47 | + |
| 48 | + List<Map<String, Object>> logs = new ArrayList<>(); |
| 49 | + String[] actions = {"login", "logout", "create", "update", "delete", "view"}; |
| 50 | + String[] resources = {"monitor", "alert", "user", "config", "report"}; |
| 51 | + |
| 52 | + for (int i = 0; i < safeSize; i++) { |
| 53 | + String logAction = actions[i % actions.length]; |
| 54 | + if (action == null || action.equals(logAction)) { |
| 55 | + logs.add(Map.of( |
| 56 | + "id", UUID.randomUUID().toString().substring(0, 8), |
| 57 | + "timestamp", System.currentTimeMillis() - (i * 300000L), |
| 58 | + "action", logAction, |
| 59 | + "resource", resources[i % resources.length], |
| 60 | + "user", "admin", |
| 61 | + "ip", "192.168.1." + (100 + i % 50), |
| 62 | + "status", "success" |
| 63 | + )); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return ResponseEntity.ok(Map.of( |
| 68 | + "logs", logs, |
| 69 | + "page", safePage, |
| 70 | + "size", safeSize, |
| 71 | + "total", 1000, |
| 72 | + "totalPages", 50 |
| 73 | + )); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + @GetMapping("/summary") |
| 79 | + @Operation(summary = "Get audit summary", description = "Returns audit statistics summary") |
| 80 | + public ResponseEntity<Map<String, Object>> getSummary( |
| 81 | + @Parameter(description = "Time period") @RequestParam(defaultValue = "24h") String period) { |
| 82 | + return ResponseEntity.ok(Map.of( |
| 83 | + "period", period, |
| 84 | + "totalEvents", 1523, |
| 85 | + "byAction", Map.of( |
| 86 | + "login", 245, |
| 87 | + "logout", 230, |
| 88 | + "create", 156, |
| 89 | + "update", 489, |
| 90 | + "delete", 45, |
| 91 | + "view", 358 |
| 92 | + ), |
| 93 | + "byStatus", Map.of( |
| 94 | + "success", 1498, |
| 95 | + "failure", 25 |
| 96 | + ), |
| 97 | + "uniqueUsers", 12 |
| 98 | + )); |
| 99 | + } |
| 100 | + |
| 101 | + @GetMapping("/users") |
| 102 | + @Operation(summary = "Get user audit history", description = "Returns audit logs for specific user") |
| 103 | + public ResponseEntity<Map<String, Object>> getUserAudit( |
| 104 | + @Parameter(description = "User ID") @RequestParam String userId, |
| 105 | + @Parameter(description = "Limit") @RequestParam(defaultValue = "20") int limit) { |
| 106 | + |
| 107 | + int safeLimit = Math.min(Math.max(limit, 1), 100); |
| 108 | + List<Map<String, Object>> logs = new ArrayList<>(); |
| 109 | + |
| 110 | + for (int i = 0; i < safeLimit; i++) { |
| 111 | + logs.add(Map.of( |
| 112 | + "id", UUID.randomUUID().toString().substring(0, 8), |
| 113 | + "timestamp", System.currentTimeMillis() - (i * 600000L), |
| 114 | + "action", i % 2 == 0 ? "view" : "update", |
| 115 | + "resource", "monitor", |
| 116 | + "status", "success" |
| 117 | + )); |
| 118 | + } |
| 119 | + |
| 120 | + return ResponseEntity.ok(Map.of("userId", userId, "logs", logs, "total", logs.size())); |
| 121 | + } |
| 122 | + |
| 123 | + @GetMapping("/actions") |
| 124 | + @Operation(summary = "List audit actions", description = "Returns available audit action types") |
| 125 | + public ResponseEntity<Map<String, Object>> listActions() { |
| 126 | + return ResponseEntity.ok(Map.of( |
| 127 | + "actions", List.of( |
| 128 | + Map.of("id", "login", "name", "Login", "category", "auth"), |
| 129 | + Map.of("id", "logout", "name", "Logout", "category", "auth"), |
| 130 | + Map.of("id", "create", "name", "Create", "category", "crud"), |
| 131 | + Map.of("id", "update", "name", "Update", "category", "crud"), |
| 132 | + Map.of("id", "delete", "name", "Delete", "category", "crud"), |
| 133 | + Map.of("id", "view", "name", "View", "category", "crud") |
| 134 | + ), |
| 135 | + "total", 6 |
| 136 | + )); |
| 137 | + } |
| 138 | + |
| 139 | + @PostMapping("/export") |
| 140 | + @Operation(summary = "Export audit logs", description = "Exports audit logs in specified format") |
| 141 | + public ResponseEntity<Map<String, Object>> exportLogs( |
| 142 | + @Parameter(description = "Export format") @RequestParam(defaultValue = "json") String format, |
| 143 | + @Parameter(description = "Start date") @RequestParam(required = false) String startDate, |
| 144 | + @Parameter(description = "End date") @RequestParam(required = false) String endDate) { |
| 145 | + return ResponseEntity.ok(Map.of( |
| 146 | + "exportId", UUID.randomUUID().toString().substring(0, 8), |
| 147 | + "format", format, |
| 148 | + "status", "processing", |
| 149 | + "estimatedRecords", 1523, |
| 150 | + "requestedAt", System.currentTimeMillis() |
| 151 | + )); |
| 152 | + } |
| 153 | +} |
0 commit comments