feat: admin audit-log read API
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
adc62ac436
commit
8a527e9ffe
6 files changed
+284
-1
No files matched your search
+9
@@ -0,0 +1,9 @@
|
||||
package com.solvway.mobile.api.constant.api;
|
||||
|
||||
public class RestApiLogServiceConstants {
|
||||
|
||||
private RestApiLogServiceConstants() {}
|
||||
|
||||
public static final String API_REST_LOG_AUDIT_SEARCH = "/log/audit/search";
|
||||
public static final String API_REST_LOG_AUDIT_DETAIL = "/log/audit/{id}";
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.service.AdminAuditLogService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/** Audit log screen: search and detail of admin audit events. Reads are not audited. Must stay under {@code controller..} for AppAccessAspect. */
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/audit-log")
|
||||
@RequiredArgsConstructor
|
||||
@AdminModule("BO_AUDIT_LOG")
|
||||
public class AdminAuditLogController {
|
||||
|
||||
private final AdminAuditLogService auditLog;
|
||||
|
||||
@AppAuth
|
||||
@GetMapping
|
||||
public ResponseEntity<Map<String, Object>> list(
|
||||
@RequestParam(required = false) String actor,
|
||||
@RequestParam(required = false) String module,
|
||||
@RequestParam(required = false) String action,
|
||||
@RequestParam(required = false) String outcome,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to,
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "20") int size) {
|
||||
return ResponseEntity.ok(auditLog.search(actor, module, action, outcome, from, to, page, size));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> detail(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(auditLog.get(id));
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.mobile.api.constant.api.RestApiLogServiceConstants;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** Reads the audit trail from services-log for the Audit log screen, turning its 400/404 into coded admin errors. */
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AdminAuditLogService {
|
||||
|
||||
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private final RestTemplateCustomUtil restTemplateUtil;
|
||||
|
||||
public Map<String, Object> search(String actor, String module, String action, String outcome, String from, String to, int page, int size) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
put(body, "actor", actor);
|
||||
put(body, "module", module);
|
||||
put(body, "action", action);
|
||||
put(body, "outcome", outcome);
|
||||
put(body, "from", from);
|
||||
put(body, "to", to);
|
||||
body.put("page", page);
|
||||
body.put("size", size);
|
||||
try {
|
||||
return restTemplateUtil.postForEntity(url(RestApiLogServiceConstants.API_REST_LOG_AUDIT_SEARCH), MAP, body).getBody();
|
||||
} catch (HttpClientErrorException.BadRequest e) {
|
||||
throw new CodedResponseStatusException(HttpStatus.BAD_REQUEST, reason(e), "INVALID_FILTER");
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> get(Long id) {
|
||||
try {
|
||||
return restTemplateUtil.getForEntity(url(RestApiLogServiceConstants.API_REST_LOG_AUDIT_DETAIL), MAP, id).getBody();
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
throw new CodedResponseStatusException(HttpStatus.NOT_FOUND, "Audit event not found", "NOT_FOUND");
|
||||
}
|
||||
}
|
||||
|
||||
private static void put(Map<String, Object> body, String key, String value) {
|
||||
if (StringUtils.hasText(value)) body.put(key, value.trim());
|
||||
}
|
||||
|
||||
private static String reason(HttpClientErrorException e) {
|
||||
try {
|
||||
JsonNode detail = JSON.readTree(e.getResponseBodyAsString()).get("detail");
|
||||
if (detail != null && detail.isTextual() && StringUtils.hasText(detail.asText())) return detail.asText();
|
||||
} catch (Exception ignored) {
|
||||
// empty or unparsable body: fall through to the generic reason
|
||||
}
|
||||
return "Invalid request";
|
||||
}
|
||||
|
||||
private String url(String api) {
|
||||
return restTemplateUtil.getURLforApiService(api);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user