-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminAuditLogController.java
More file actions
224 lines (200 loc) · 11.7 KB
/
Copy pathAdminAuditLogController.java
File metadata and controls
224 lines (200 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package apu.saerok_admin.web;
import apu.saerok_admin.infra.audit.AdminAuditLogClient;
import apu.saerok_admin.infra.audit.dto.AdminAuditLogListResponse;
import apu.saerok_admin.web.view.AdminAuditLogItem;
import apu.saerok_admin.web.view.Breadcrumb;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;
@Controller
@RequestMapping("/admin/audit-logs")
public class AdminAuditLogController {
private static final Logger log = LoggerFactory.getLogger(AdminAuditLogController.class);
private static final int DEFAULT_PAGE_SIZE = 20;
private static final List<Integer> PAGE_SIZE_OPTIONS = List.of(10, 20, 50);
private static final Map<String, ActionPresentation> ACTION_PRESENTATIONS = Map.ofEntries(
Map.entry("REPORT_IGNORED", new ActionPresentation("신고 무시", "신고를 추가 조치 없이 마감했습니다.", "text-bg-secondary")),
Map.entry("COLLECTION_DELETED", new ActionPresentation("새록 삭제", "신고된 새록을 삭제했습니다.", "text-bg-danger")),
Map.entry("COMMENT_DELETED", new ActionPresentation("댓글 삭제", "신고된 댓글을 삭제했습니다.", "text-bg-danger")),
Map.entry("AD_CREATED", new ActionPresentation("광고 등록", "새 광고를 등록했습니다.", "text-bg-success")),
Map.entry("AD_UPDATED", new ActionPresentation("광고 수정", "기존 광고 정보를 수정했습니다.", "text-bg-info")),
Map.entry("AD_DELETED", new ActionPresentation("광고 삭제", "광고를 삭제했습니다.", "text-bg-danger")),
Map.entry("SLOT_CREATED", new ActionPresentation("광고 위치 등록", "새 광고 위치를 등록했습니다.", "text-bg-success")),
Map.entry("SLOT_UPDATED", new ActionPresentation("광고 위치 수정", "광고 위치 정보를 수정했습니다.", "text-bg-info")),
Map.entry("SLOT_DELETED", new ActionPresentation("광고 위치 삭제", "광고 위치를 삭제했습니다.", "text-bg-danger")),
Map.entry("AD_PLACEMENT_CREATED", new ActionPresentation("광고 스케줄 등록", "새 광고 스케줄을 등록했습니다.", "text-bg-success")),
Map.entry("AD_PLACEMENT_UPDATED", new ActionPresentation("광고 스케줄 수정", "광고 스케줄을 수정했습니다.", "text-bg-info")),
Map.entry("AD_PLACEMENT_DELETED", new ActionPresentation("광고 스케줄 삭제", "광고 스케줄을 삭제했습니다.", "text-bg-danger")),
Map.entry("ANNOUNCEMENT_CREATED", new ActionPresentation("공지 등록", "새 공지를 등록했습니다.", "text-bg-success")),
Map.entry("ANNOUNCEMENT_UPDATED", new ActionPresentation("공지 수정", "공지 내용을 수정했습니다.", "text-bg-info")),
Map.entry("ANNOUNCEMENT_DELETED", new ActionPresentation("공지 삭제", "공지를 삭제했습니다.", "text-bg-danger")),
Map.entry("FREEBOARD_POST_DELETED", new ActionPresentation("게시글 삭제", "신고된 자유게시판 게시글을 삭제했습니다.", "text-bg-danger")),
Map.entry("FREEBOARD_COMMENT_DELETED", new ActionPresentation("게시판 댓글 삭제", "신고된 자유게시판 댓글을 삭제했습니다.", "text-bg-danger")),
Map.entry("ADMIN_MESSAGE_SENT", new ActionPresentation("대상 공지 발송", "특정 사용자에게 관리자 메시지를 발송했습니다.", "text-bg-primary"))
);
private static final Map<String, String> TARGET_LABELS = Map.ofEntries(
Map.entry("REPORT_COLLECTION", "새록 신고"),
Map.entry("REPORT_COMMENT", "댓글 신고"),
Map.entry("REPORT_FREEBOARD_POST", "자유게시판 게시글 신고"),
Map.entry("REPORT_FREEBOARD_COMMENT", "자유게시판 댓글 신고"),
Map.entry("COLLECTION", "새록"),
Map.entry("COMMENT", "댓글"),
Map.entry("FREEBOARD_POST", "자유게시판 게시글"),
Map.entry("FREEBOARD_COMMENT", "자유게시판 댓글"),
Map.entry("AD", "광고"),
Map.entry("SLOT", "광고 위치"),
Map.entry("AD_PLACEMENT", "광고 스케줄"),
Map.entry("ANNOUNCEMENT", "공지"),
Map.entry("ADMIN_MESSAGE", "관리자 메시지")
);
private static final String UNKNOWN_ACTION_LABEL = "기록되지 않은 작업";
private static final String UNKNOWN_ACTION_DESCRIPTION = "정의되지 않은 관리자 활동입니다.";
private static final String UNKNOWN_ACTION_BADGE = "text-bg-secondary";
private static final String UNKNOWN_TARGET_LABEL = "알 수 없는 대상";
private static final ObjectMapper METADATA_MAPPER = new ObjectMapper();
private final AdminAuditLogClient adminAuditLogClient;
public AdminAuditLogController(AdminAuditLogClient adminAuditLogClient) {
this.adminAuditLogClient = adminAuditLogClient;
}
@GetMapping
public String list(@RequestParam(name = "page", required = false) Integer page,
@RequestParam(name = "size", required = false) Integer size,
Model model) {
model.addAttribute("pageTitle", "관리자 활동 로그");
model.addAttribute("activeMenu", "adminAuditLogs");
model.addAttribute("breadcrumbs", List.of(
Breadcrumb.of("대시보드", "/"),
Breadcrumb.active("관리자 활동 로그")
));
ensureToastMessages(model);
int resolvedPage = page != null && page >= 1 ? page : 1;
int resolvedSize = size != null && size > 0 ? size : DEFAULT_PAGE_SIZE;
try {
AdminAuditLogListResponse response = adminAuditLogClient.listAuditLogs(resolvedPage, resolvedSize);
List<AdminAuditLogItem> logs = response != null && response.items() != null
? response.items().stream().map(this::toViewItem).toList()
: List.of();
int rangeStart = logs.isEmpty() ? 0 : (resolvedPage - 1) * resolvedSize + 1;
int rangeEnd = (resolvedPage - 1) * resolvedSize + logs.size();
model.addAttribute("logs", logs);
model.addAttribute("page", resolvedPage);
model.addAttribute("size", resolvedSize);
model.addAttribute("hasPrevious", resolvedPage > 1);
model.addAttribute("hasNext", logs.size() == resolvedSize);
model.addAttribute("rangeStart", rangeStart);
model.addAttribute("rangeEnd", rangeEnd);
model.addAttribute("loadErrorMessage", null);
} catch (RestClientResponseException exception) {
log.warn("Failed to load admin audit logs. status={}, body={}",
exception.getStatusCode(), exception.getResponseBodyAsString(), exception);
attachEmptyState(model, resolvedPage, resolvedSize,
"관리자 활동 로그를 불러오지 못했습니다. 잠시 후 다시 시도해주세요.");
} catch (RestClientException exception) {
log.warn("Failed to load admin audit logs.", exception);
attachEmptyState(model, resolvedPage, resolvedSize,
"관리자 활동 로그를 불러오지 못했습니다. 잠시 후 다시 시도해주세요.");
}
model.addAttribute("pageSizeOptions", PAGE_SIZE_OPTIONS);
return "admin-audit/list";
}
private AdminAuditLogItem toViewItem(AdminAuditLogListResponse.Item item) {
long id = item.id() != null ? item.id() : 0L;
LocalDateTime occurredAt = item.createdAt();
String adminNickname = item.admin() != null && StringUtils.hasText(item.admin().nickname())
? item.admin().nickname()
: "알 수 없음";
ActionPresentation presentation = ACTION_PRESENTATIONS.getOrDefault(
normalizeKey(item.action()),
new ActionPresentation(UNKNOWN_ACTION_LABEL, UNKNOWN_ACTION_DESCRIPTION, UNKNOWN_ACTION_BADGE)
);
String targetLabel = TARGET_LABELS.getOrDefault(normalizeKey(item.targetType()), UNKNOWN_TARGET_LABEL);
String targetDisplay = buildTargetDisplay(targetLabel, item.targetId());
String reportDisplay = item.reportId() != null ? "신고 #" + item.reportId() : "-";
List<AdminAuditLogItem.MetadataEntry> metadataEntries = toMetadataEntries(item.metadata());
return new AdminAuditLogItem(
id,
occurredAt,
adminNickname,
presentation.label(),
presentation.description(),
presentation.badgeClass(),
targetDisplay,
reportDisplay,
metadataEntries
);
}
private String buildTargetDisplay(String targetLabel, Long targetId) {
boolean hasLabel = StringUtils.hasText(targetLabel) && !UNKNOWN_TARGET_LABEL.equals(targetLabel);
if (targetId == null) {
return hasLabel ? targetLabel : "-";
}
if (hasLabel) {
return targetLabel + " #" + targetId;
}
return "#" + targetId;
}
private List<AdminAuditLogItem.MetadataEntry> toMetadataEntries(Map<String, Object> metadata) {
if (metadata == null || metadata.isEmpty()) {
return List.of();
}
List<AdminAuditLogItem.MetadataEntry> entries = new ArrayList<>();
for (Map.Entry<String, Object> entry : new LinkedHashMap<>(metadata).entrySet()) {
String key = entry.getKey();
String value = formatMetadataValue(entry.getValue());
entries.add(new AdminAuditLogItem.MetadataEntry(key, value));
}
return List.copyOf(entries);
}
private String formatMetadataValue(Object value) {
if (value == null) {
return "-";
}
if (value instanceof Map || value instanceof List) {
try {
return METADATA_MAPPER.writeValueAsString(value);
} catch (JsonProcessingException exception) {
log.debug("Failed to serialize metadata value to JSON. value={}", value, exception);
return value.toString();
}
}
return value.toString();
}
private void attachEmptyState(Model model, int page, int size, String errorMessage) {
model.addAttribute("logs", List.of());
model.addAttribute("page", page);
model.addAttribute("size", size);
model.addAttribute("hasPrevious", page > 1);
model.addAttribute("hasNext", false);
model.addAttribute("rangeStart", 0);
model.addAttribute("rangeEnd", 0);
model.addAttribute("loadErrorMessage", errorMessage);
}
private void ensureToastMessages(Model model) {
if (!model.containsAttribute("toastMessages")) {
model.addAttribute("toastMessages", List.of());
}
}
private String normalizeKey(String value) {
if (!StringUtils.hasText(value)) {
return "";
}
return value.toUpperCase(Locale.ROOT);
}
private record ActionPresentation(String label, String description, String badgeClass) {
}
}