-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrentAdminClient.java
More file actions
141 lines (122 loc) · 4.72 KB
/
CurrentAdminClient.java
File metadata and controls
141 lines (122 loc) · 4.72 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
package apu.saerok_admin.infra;
import apu.saerok_admin.security.LoginSessionManager;
import apu.saerok_admin.web.view.CurrentAdminProfile;
import java.net.URI;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.util.UriBuilder;
@Component
public class CurrentAdminClient {
private static final Logger log = LoggerFactory.getLogger(CurrentAdminClient.class);
private static final Map<String, String> ROLE_DESCRIPTION_MAP = Map.of(
"ADMIN_VIEWER", "열람자",
"ADMIN_EDITOR", "운영자"
);
private static final String UNKNOWN_ROLE_DESCRIPTION = "알 수 없는 관리자 권한";
private final RestClient saerokRestClient;
private final List<String> missingPrefixSegments;
private final LoginSessionManager loginSessionManager;
public CurrentAdminClient(
RestClient saerokRestClient,
SaerokApiProps saerokApiProps,
LoginSessionManager loginSessionManager
) {
this.saerokRestClient = saerokRestClient;
this.missingPrefixSegments = saerokApiProps.missingPrefixSegments();
this.loginSessionManager = loginSessionManager;
}
public Optional<CurrentAdminProfile> fetchCurrentAdminProfile() {
if (loginSessionManager.currentAccessToken().isEmpty()) {
return Optional.empty();
}
try {
BackendUserProfileResponse response = saerokRestClient.get()
.uri(uriBuilder -> buildUri(uriBuilder, "user", "me"))
.retrieve()
.body(BackendUserProfileResponse.class);
if (response == null) {
return Optional.empty();
}
List<String> roles = response.roles() != null ? List.copyOf(response.roles()) : List.of();
return Optional.of(new CurrentAdminProfile(
response.nickname(),
response.email(),
response.profileImageUrl(),
toRoleDescriptions(roles),
normalizeRoleCodes(roles)
));
} catch (RestClientResponseException exception) {
log.warn(
"Failed to fetch current admin profile. status={}, body={}",
exception.getStatusCode(),
exception.getResponseBodyAsString(),
exception
);
} catch (RestClientException exception) {
log.warn("Failed to fetch current admin profile.", exception);
}
return Optional.empty();
}
private URI buildUri(UriBuilder builder, String... segments) {
if (!missingPrefixSegments.isEmpty()) {
builder.pathSegment(missingPrefixSegments.toArray(String[]::new));
}
builder.pathSegment(segments);
return builder.build();
}
private record BackendUserProfileResponse(
String nickname,
String email,
String profileImageUrl,
List<String> roles
) {
}
private List<String> toRoleDescriptions(List<String> roles) {
if (roles == null || roles.isEmpty()) {
return List.of();
}
Set<String> descriptions = new LinkedHashSet<>();
for (String role : roles) {
if (!StringUtils.hasText(role)) {
continue;
}
String normalized = role.toUpperCase(Locale.ROOT);
String description = ROLE_DESCRIPTION_MAP.get(normalized);
if (description != null) {
descriptions.add(description);
continue;
}
if (normalized.startsWith("ADMIN_")) {
descriptions.add(UNKNOWN_ROLE_DESCRIPTION);
}
}
return descriptions.isEmpty() ? List.of() : List.copyOf(descriptions);
}
private List<String> normalizeRoleCodes(List<String> roles) {
if (roles == null || roles.isEmpty()) {
return List.of();
}
Set<String> normalized = new LinkedHashSet<>();
for (String role : roles) {
if (!StringUtils.hasText(role)) {
continue;
}
normalized.add(role.toUpperCase(Locale.ROOT));
}
if (normalized.isEmpty()) {
return List.of();
}
return List.copyOf(normalized);
}
}