Temo commit

This commit is contained in:
2026-01-09 21:15:42 +01:00
parent cc9eea9c08
commit e8b9c31688
4 changed files with 62 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ import dev.kruhlmann.imgfloat.service.ChannelDirectoryService;
import dev.kruhlmann.imgfloat.service.GitInfoService;
import dev.kruhlmann.imgfloat.service.GithubReleaseService;
import dev.kruhlmann.imgfloat.service.SettingsService;
import dev.kruhlmann.imgfloat.service.SystemAdministratorService;
import dev.kruhlmann.imgfloat.service.VersionService;
import dev.kruhlmann.imgfloat.util.LogSanitizer;
import org.slf4j.Logger;
@@ -31,6 +32,7 @@ public class ViewController {
private final ObjectMapper objectMapper;
private final AuthorizationService authorizationService;
private final GithubReleaseService githubReleaseService;
private final SystemAdministratorService systemAdministratorService;
private final long uploadLimitBytes;
public ViewController(
@@ -41,6 +43,7 @@ public class ViewController {
ObjectMapper objectMapper,
AuthorizationService authorizationService,
GithubReleaseService githubReleaseService,
SystemAdministratorService systemAdministratorService,
long uploadLimitBytes
) {
this.channelDirectoryService = channelDirectoryService;
@@ -50,6 +53,7 @@ public class ViewController {
this.objectMapper = objectMapper;
this.authorizationService = authorizationService;
this.githubReleaseService = githubReleaseService;
this.systemAdministratorService = systemAdministratorService;
this.uploadLimitBytes = uploadLimitBytes;
}
@@ -111,6 +115,7 @@ public class ViewController {
LOG.error("Failed to serialize settings for settings view", e);
throw new ResponseStatusException(INTERNAL_SERVER_ERROR, "Failed to serialize settings");
}
model.addAttribute("initialSysadmin", systemAdministratorService.getInitialSysadmin());
return "settings";
}

View File

@@ -8,7 +8,6 @@ import java.util.Locale;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
@@ -18,25 +17,18 @@ public class SystemAdministratorService {
private static final Logger logger = LoggerFactory.getLogger(SystemAdministratorService.class);
private final SystemAdministratorRepository repo;
private final String initialSysadmin;
private final Environment environment;
public SystemAdministratorService(
SystemAdministratorRepository repo,
@Value("${IMGFLOAT_INITIAL_TWITCH_USERNAME_SYSADMIN:#{null}}") String initialSysadmin,
Environment environment
) {
this.repo = repo;
this.initialSysadmin = initialSysadmin;
this.environment = environment;
}
@PostConstruct
public void initDefaults() {
if (repo.count() > 0) {
return;
}
if (
Boolean.parseBoolean(
environment.getProperty("org.springframework.boot.test.context.SpringBootTestContextBootstrapper")
@@ -46,18 +38,34 @@ public class SystemAdministratorService {
return;
}
String initialSysadmin = getInitialSysadmin();
if (initialSysadmin != null) {
long deleted = repo.deleteByTwitchUsername(initialSysadmin);
if (deleted > 0) {
logger.info("Removed persisted initial system administrator '{}' to use environment value", initialSysadmin);
}
}
if (repo.count() > 0) {
return;
}
if (initialSysadmin == null || initialSysadmin.isBlank()) {
throw new IllegalStateException(
"No system administrators exist and IMGFLOAT_INITIAL_TWITCH_USERNAME_SYSADMIN is not set"
);
}
addSysadmin(initialSysadmin);
logger.info("Created initial system administrator '{}'", initialSysadmin);
logger.info("Using initial system administrator '{}' from environment", initialSysadmin);
}
public void addSysadmin(String twitchUsername) {
String normalized = normalize(twitchUsername);
String initialSysadmin = getInitialSysadmin();
if (initialSysadmin != null && initialSysadmin.equals(normalized)) {
return;
}
if (repo.existsByTwitchUsername(normalized)) {
return;
@@ -67,11 +75,18 @@ public class SystemAdministratorService {
}
public void removeSysadmin(String twitchUsername) {
if (repo.count() <= 1) {
String normalized = normalize(twitchUsername);
String initialSysadmin = getInitialSysadmin();
if (initialSysadmin != null && initialSysadmin.equals(normalized)) {
throw new IllegalStateException("Cannot remove the initial system administrator");
}
if (initialSysadmin == null && repo.count() <= 1) {
throw new IllegalStateException("Cannot remove the last system administrator");
}
long deleted = repo.deleteByTwitchUsername(normalize(twitchUsername));
long deleted = repo.deleteByTwitchUsername(normalized);
if (deleted == 0) {
throw new IllegalArgumentException("System administrator does not exist");
@@ -79,7 +94,20 @@ public class SystemAdministratorService {
}
public boolean isSysadmin(String twitchUsername) {
return repo.existsByTwitchUsername(normalize(twitchUsername));
String normalized = normalize(twitchUsername);
String initialSysadmin = getInitialSysadmin();
if (initialSysadmin != null && initialSysadmin.equals(normalized)) {
return true;
}
return repo.existsByTwitchUsername(normalized);
}
public String getInitialSysadmin() {
String value = environment.getProperty("IMGFLOAT_INITIAL_TWITCH_USERNAME_SYSADMIN");
if (value == null || value.isBlank()) {
return null;
}
return normalize(value);
}
public List<String> listSysadmins() {