feat: admin services supply allow-listed audit changes
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
1523dc566b
commit
adc62ac436
12 files changed
+668
-8
No files matched your search
+10
-1
@@ -5,6 +5,7 @@ import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.dto.admin.AdminModuleGrant;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AuditChanges;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
@@ -52,8 +53,12 @@ public class AdminAclService {
|
||||
List<Map<String, Object>> modules = modules();
|
||||
Set<Long> known = new HashSet<>();
|
||||
Set<Long> current = new HashSet<>();
|
||||
Map<Long, String> codes = new java.util.HashMap<>();
|
||||
for (Map<String, Object> m : modules) {
|
||||
if (isGrantable(m)) known.add(AdminRoleService.idOf(m));
|
||||
if (isGrantable(m)) {
|
||||
known.add(AdminRoleService.idOf(m));
|
||||
codes.put(AdminRoleService.idOf(m), String.valueOf(m.get("name")));
|
||||
}
|
||||
if (isGrantable(m) && grantedTo(m, roleId)) current.add(AdminRoleService.idOf(m));
|
||||
}
|
||||
Set<Long> wanted = new HashSet<>(moduleIds);
|
||||
@@ -62,6 +67,10 @@ public class AdminAclService {
|
||||
// ponytail: not atomic, adds run before deletes and a failure midway leaves a partial grant; a repeated PUT converges. Upgrade: one services-system endpoint that replaces grants in a transaction.
|
||||
for (Long id : wanted) if (!current.contains(id)) call(RestApiSystemServiceConstants.API_REST_SYSTEM_MODULE_ADD_MODULE_ROLE, id, roleId);
|
||||
for (Long id : current) if (!wanted.contains(id)) call(RestApiSystemServiceConstants.API_REST_SYSTEM_MODULE_DELETE_MODULE_ROLE, id, roleId);
|
||||
List<String> added = wanted.stream().filter(id -> !current.contains(id)).map(codes::get).sorted().toList();
|
||||
List<String> removed = current.stream().filter(id -> !wanted.contains(id)).map(codes::get).sorted().toList();
|
||||
if (!added.isEmpty()) AuditChanges.put("modulesAdded", null, added);
|
||||
if (!removed.isEmpty()) AuditChanges.put("modulesRemoved", null, removed);
|
||||
return getGrantTree(roleId);
|
||||
}
|
||||
|
||||
|
||||
+13
-2
@@ -10,6 +10,7 @@ import com.solvway.mobile.api.dto.admin.AdminEventLeadCaptureRequest;
|
||||
import com.solvway.mobile.api.dto.admin.AdminEventLeadUpdateRequest;
|
||||
import com.solvway.mobile.api.dto.admin.AdminLeadMatchRequest;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AuditChanges;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -23,6 +24,7 @@ import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -260,8 +262,17 @@ public class AdminEventLeadService {
|
||||
body.put("nextFollowUpDate", followUp == null ? null : followUp.toString());
|
||||
body.put("notes", trimmed(r.notes()));
|
||||
body.put("doneBy", caller.getUsername());
|
||||
return view(restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_LEAD_UPDATE), MAP, body).getBody(),
|
||||
known, new HashMap<>());
|
||||
Map<String, Object> saved = restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_LEAD_UPDATE), MAP, body).getBody();
|
||||
// field names only: follow-up values are personal data
|
||||
List<String> changed = new ArrayList<>();
|
||||
if (!Objects.equals(r.salespersonId(), currentSalesperson)) changed.add("salespersonId");
|
||||
if (!Objects.equals(temperature, current.get("temperature"))) changed.add("temperature");
|
||||
if (!Objects.equals(status, current.get("status"))) changed.add("status");
|
||||
if (!new HashSet<>(interestIds).equals(new HashSet<>(longs(current.get("interestIds"))))) changed.add("interestIds");
|
||||
if (!Objects.equals(body.get("nextFollowUpDate"), current.get("nextFollowUpDate") == null ? null : current.get("nextFollowUpDate").toString())) changed.add("nextFollowUpDate");
|
||||
if (!Objects.equals(body.get("notes"), current.get("notes"))) changed.add("notes");
|
||||
if (!changed.isEmpty()) AuditChanges.fields(changed.toArray(String[]::new));
|
||||
return view(saved, known, new HashMap<>());
|
||||
}
|
||||
|
||||
// --- rules ---
|
||||
|
||||
+23
-1
@@ -6,6 +6,7 @@ import com.solvway.mobile.api.constant.api.RestApiCounterServiceConstants;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.dto.admin.AdminLeadUpdateRequest;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AuditChanges;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
@@ -19,6 +20,7 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static com.solvway.mobile.api.service.AdminEventLeadService.email;
|
||||
import static com.solvway.mobile.api.service.AdminEventLeadService.error;
|
||||
@@ -118,11 +120,31 @@ public class AdminLeadService {
|
||||
body.put("jobTitle", jobTitle);
|
||||
body.put("notes", notes);
|
||||
body.put("doneBy", caller.getUsername());
|
||||
Map<String, Object> before = storedOrNull(caller.getCompanyId(), id);
|
||||
Map<String, Object> saved;
|
||||
try {
|
||||
return detail(restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_LEAD_UPDATE), MAP, body).getBody());
|
||||
saved = restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_LEAD_UPDATE), MAP, body).getBody();
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
throw notFound();
|
||||
}
|
||||
// field names only: lead values are personal data. No readable before-state means nothing is recorded.
|
||||
if (before != null) {
|
||||
List<String> changed = new ArrayList<>();
|
||||
for (String f : List.of("name", "mobileE164", "email", "companyName", "jobTitle", "notes")) {
|
||||
if (!Objects.equals(before.get(f), body.get(f))) changed.add("mobileE164".equals(f) ? "phone" : f);
|
||||
}
|
||||
if (!changed.isEmpty()) AuditChanges.fields(changed.toArray(String[]::new));
|
||||
}
|
||||
return detail(saved);
|
||||
}
|
||||
|
||||
/** Best-effort read of the lead before it is changed, for the audit trail only. */
|
||||
private Map<String, Object> storedOrNull(Long companyId, long id) {
|
||||
try {
|
||||
return restTemplateUtil.getForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_LEAD_DETAIL), MAP, companyId, id).getBody();
|
||||
} catch (RuntimeException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Digits to match against the stored E.164 mobile, or null when the term is not a phone-like number. */
|
||||
|
||||
+36
@@ -6,6 +6,7 @@ import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AuditChanges;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -15,9 +16,11 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Passes the Login security screen's calls to services-system, turning its 400s into coded admin errors. */
|
||||
@@ -59,7 +62,13 @@ public class AdminLoginSecurityService {
|
||||
}
|
||||
|
||||
public Map<String, Object> saveConfig(Map<String, Object> config) {
|
||||
Map<String, Object> before = readOrNull(this::config);
|
||||
Map<String, Object> saved = post(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_CONFIG, MAP, config);
|
||||
if (before != null && saved != null) {
|
||||
for (String key : List.of("enabled", "maxAttempts", "period", "unit")) {
|
||||
if (!Objects.equals(before.get(key), saved.get(key))) AuditChanges.put(key, before.get(key), saved.get(key));
|
||||
}
|
||||
}
|
||||
log.info("lockout settings changed by {}", callerName());
|
||||
return saved;
|
||||
}
|
||||
@@ -69,11 +78,38 @@ public class AdminLoginSecurityService {
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> savePasswordRules(List<Map<String, Object>> rules) {
|
||||
List<Map<String, Object>> before = readOrNull(this::passwordRules);
|
||||
List<Map<String, Object>> saved = post(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_PASSWORD_RULES, LIST, rules);
|
||||
if (before != null && saved != null) {
|
||||
Map<Object, Map<String, Object>> old = new HashMap<>();
|
||||
before.forEach(b -> old.put(b.get("param"), b));
|
||||
for (Map<String, Object> now : saved) {
|
||||
Map<String, Object> was = old.get(now.get("param"));
|
||||
if (was != null && !(Objects.equals(was.get("status"), now.get("status")) && Objects.equals(was.get("value"), now.get("value")))) {
|
||||
AuditChanges.put(String.valueOf(now.get("param")), ruleState(was), ruleState(now));
|
||||
}
|
||||
}
|
||||
}
|
||||
log.info("password rules changed by {}", callerName());
|
||||
return saved;
|
||||
}
|
||||
|
||||
private static Map<String, Object> ruleState(Map<String, Object> rule) {
|
||||
Map<String, Object> state = new HashMap<>();
|
||||
state.put("status", rule.get("status"));
|
||||
state.put("value", rule.get("value"));
|
||||
return state;
|
||||
}
|
||||
|
||||
/** Best-effort read of the settings before they change, for the audit trail only. */
|
||||
private static <T> T readOrNull(Supplier<T> read) {
|
||||
try {
|
||||
return read.get();
|
||||
} catch (RuntimeException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private <T> T post(String api, ParameterizedTypeReference<T> type, Object body) {
|
||||
return upstream(() -> restTemplateUtil.postForEntity(url(api), type, body).getBody());
|
||||
}
|
||||
|
||||
+16
-1
@@ -6,6 +6,7 @@ import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.dto.admin.AdminRoleRef;
|
||||
import com.solvway.mobile.api.dto.admin.AdminUserRequest;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AuditChanges;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -115,6 +116,8 @@ public class AdminUserService {
|
||||
if (adminAuthenticationService.setAdminPassword(((Number) saved.get("id")).longValue(), r.password()) == 0) {
|
||||
throw error(HttpStatus.INTERNAL_SERVER_ERROR, "User created but the password was not stored; set a temporary password", "SAVE_FAILED");
|
||||
}
|
||||
AuditChanges.put("roles", null, sortedRoleNames(user.get("roles")));
|
||||
AuditChanges.put("enabled", null, true);
|
||||
return clean(saved);
|
||||
}
|
||||
|
||||
@@ -132,6 +135,8 @@ public class AdminUserService {
|
||||
if (r.roles() != null && r.roles().isEmpty()) {
|
||||
throw error(HttpStatus.BAD_REQUEST, "At least one role is required", "REQUIRED_FIELD");
|
||||
}
|
||||
List<String> rolesBefore = sortedRoleNames(user.get("roles"));
|
||||
Object enabledBefore = user.get("enabled");
|
||||
if (StringUtils.hasText(r.name())) user.put("name", r.name());
|
||||
if (r.surname() != null) user.put("surname", r.surname());
|
||||
if (StringUtils.hasText(r.email())) user.put("email", r.email());
|
||||
@@ -141,7 +146,12 @@ public class AdminUserService {
|
||||
user.put("status", r.enabled() ? "Y" : "N");
|
||||
}
|
||||
user.put("doneBy", caller.getUsername());
|
||||
return clean(save(user));
|
||||
Map<String, Object> saved = save(user);
|
||||
// names and ids only: never the user's name, surname or email
|
||||
List<String> rolesAfter = sortedRoleNames(user.get("roles"));
|
||||
if (r.roles() != null && !rolesBefore.equals(rolesAfter)) AuditChanges.put("roles", rolesBefore, rolesAfter);
|
||||
if (r.enabled() != null && !r.enabled().equals(enabledBefore)) AuditChanges.put("enabled", enabledBefore, r.enabled());
|
||||
return clean(saved);
|
||||
}
|
||||
|
||||
public int resetPassword(Long id) {
|
||||
@@ -301,6 +311,11 @@ public class AdminUserService {
|
||||
return Comparator.comparing(m -> String.valueOf(m.get(key)).toLowerCase());
|
||||
}
|
||||
|
||||
private static List<String> sortedRoleNames(Object roles) {
|
||||
if (!(roles instanceof List<?> l)) return List.of();
|
||||
return l.stream().map(r -> r instanceof Map<?, ?> rm ? String.valueOf(rm.get("name")) : String.valueOf(r)).sorted().toList();
|
||||
}
|
||||
|
||||
private static String roleNames(Map<String, Object> m) {
|
||||
Object roles = m.get("roles");
|
||||
if (!(roles instanceof List<?> l)) return "";
|
||||
|
||||
+2
-2
@@ -39,11 +39,11 @@ public final class AuditChanges {
|
||||
changes.put("fields", List.of(names));
|
||||
}
|
||||
|
||||
static Map<String, Object> current() {
|
||||
public static Map<String, Object> current() {
|
||||
return CURRENT.get();
|
||||
}
|
||||
|
||||
static void clear() {
|
||||
public static void clear() {
|
||||
CURRENT.remove();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user