mirror of
https://github.com/imgfloat/server.git
synced 2026-02-05 03:39:26 +00:00
Fix sysadmin delete
This commit is contained in:
@@ -57,7 +57,11 @@ public class SystemAdministratorApiController {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Username is required");
|
||||
}
|
||||
String username = request.twitchUsername().trim();
|
||||
systemAdministratorService.addSysadmin(username);
|
||||
try {
|
||||
systemAdministratorService.addSysadmin(username);
|
||||
} catch (IllegalStateException e) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, e.getMessage(), e);
|
||||
}
|
||||
LOG.info("System administrator added: {} (requested by {})", username, sessionUsername);
|
||||
return ResponseEntity.ok().body(systemAdministratorService.listSysadmins());
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class SystemAdministratorService {
|
||||
String initialSysadmin = getInitialSysadmin();
|
||||
|
||||
if (initialSysadmin != null && initialSysadmin.equals(normalized)) {
|
||||
return;
|
||||
throw new IllegalStateException("Cannot add the initial system administrator");
|
||||
}
|
||||
|
||||
if (repo.existsByTwitchUsername(normalized)) {
|
||||
@@ -81,6 +81,7 @@ public class SystemAdministratorService {
|
||||
repo.save(new SystemAdministrator(normalized));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeSysadmin(String twitchUsername) {
|
||||
String normalized = normalize(twitchUsername);
|
||||
String initialSysadmin = getInitialSysadmin();
|
||||
@@ -118,11 +119,21 @@ public class SystemAdministratorService {
|
||||
}
|
||||
|
||||
public List<String> listSysadmins() {
|
||||
return repo
|
||||
String initialSysadmin = getInitialSysadmin();
|
||||
List<String> persistedAdmins = repo
|
||||
.findAllByOrderByTwitchUsernameAsc()
|
||||
.stream()
|
||||
.map(SystemAdministrator::getTwitchUsername)
|
||||
.collect(Collectors.toList());
|
||||
if (initialSysadmin == null) {
|
||||
return persistedAdmins;
|
||||
}
|
||||
return java.util.stream.Stream
|
||||
.concat(persistedAdmins.stream(), java.util.stream.Stream.of(initialSysadmin))
|
||||
.map(this::normalize)
|
||||
.distinct()
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String normalize(String username) {
|
||||
|
||||
@@ -187,14 +187,16 @@ function renderSystemAdministrators(admins) {
|
||||
const button = document.createElement("button");
|
||||
button.classList.add("button", "secondary");
|
||||
button.type = "button";
|
||||
button.textContent = "Remove";
|
||||
button.addEventListener("click", () => removeSystemAdministrator(admin));
|
||||
button.setAttribute("data-sysadmin-remove", "true");
|
||||
button.setAttribute("data-sysadmin-username", admin);
|
||||
|
||||
if (isInitialSysadmin) {
|
||||
button.disabled = true;
|
||||
button.title = "The initial system administrator cannot be removed.";
|
||||
button.textContent = "System default";
|
||||
button.title = "The system default administrator cannot be removed.";
|
||||
} else {
|
||||
button.textContent = "Remove";
|
||||
button.addEventListener("click", () => removeSystemAdministrator(admin));
|
||||
}
|
||||
|
||||
listItem.appendChild(text);
|
||||
@@ -226,6 +228,10 @@ function addSystemAdministrator() {
|
||||
showToast("Enter a Twitch username", "warning");
|
||||
return;
|
||||
}
|
||||
if (initialSysadmin && username.toLowerCase() === initialSysadmin) {
|
||||
showToast("That user is already the system default administrator.", "warning");
|
||||
return;
|
||||
}
|
||||
fetch("/api/system-administrators", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
|
||||
Reference in New Issue
Block a user