Compare commits

...
1 Commits
Author SHA1 Message Date
Anthony Stirling 09e472327b init, but broken 2026-02-27 11:01:50 +00:00
6 changed files with 63 additions and 4 deletions
@@ -169,7 +169,10 @@ public class PdfMetadataService {
.getAuthor(); .getAuthor();
if (userService != null) { if (userService != null) {
author = author.replace("username", userService.getCurrentUsername()); String username = userService.getCurrentUsername();
if (username != null) {
author = author.replace("username", username);
}
} }
} }
pdf.getDocumentInformation().setAuthor(author); pdf.getDocumentInformation().setAuthor(author);
@@ -168,7 +168,8 @@ public class UIDataController {
public ResponseEntity<SignData> getSignData() { public ResponseEntity<SignData> getSignData() {
String username = ""; String username = "";
if (userService != null) { if (userService != null) {
username = userService.getCurrentUsername(); String currentUsername = userService.getCurrentUsername();
username = currentUsername != null ? currentUsername : "";
} }
List<SignatureFile> signatures = signatureService.getAvailableSignatures(username); List<SignatureFile> signatures = signatureService.getAvailableSignatures(username);
@@ -191,6 +191,30 @@ class PdfMetadataServiceTest {
// We don't verify setProducer here to avoid the "Too many actual invocations" error // We don't verify setProducer here to avoid the "Too many actual invocations" error
} }
@Test
void testSetMetadataToPdf_WithProFeaturesAndMissingUsername() {
PDDocument testDocument = mock(PDDocument.class);
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
PdfMetadataService proService =
new PdfMetadataService(
applicationProperties, STIRLING_PDF_LABEL, true, userService);
PdfMetadata testMetadata = PdfMetadata.builder().author("Original Author").build();
CustomMetadata customMetadata =
applicationProperties.getPremium().getProFeatures().getCustomMetadata();
when(customMetadata.isAutoUpdateMetadata()).thenReturn(true);
when(customMetadata.getAuthor()).thenReturn("Pro Author username");
when(userService.getCurrentUsername()).thenReturn(null);
proService.setMetadataToPdf(testDocument, testMetadata, false);
// When username is null, the "username" placeholder should not be replaced
verify(testInfo).setAuthor("Pro Author username");
}
@Test @Test
void testSetMetadataToPdf_ExistingDocument() { void testSetMetadataToPdf_ExistingDocument() {
// Create a fresh document // Create a fresh document
@@ -522,7 +522,17 @@ public class UserService implements UserServiceInterface {
@Override @Override
public String getCurrentUsername() { public String getCurrentUsername() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
log.debug("No authentication found in security context when resolving username");
return null;
}
Object principal = authentication.getPrincipal();
if (principal == null) {
log.debug("No principal found on authentication object when resolving username");
return null;
}
if (principal instanceof UserDetails detailsUser) { if (principal instanceof UserDetails detailsUser) {
return detailsUser.getUsername(); return detailsUser.getUsername();
@@ -16,6 +16,8 @@ import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import stirling.software.common.model.ApplicationProperties; import stirling.software.common.model.ApplicationProperties;
@@ -150,6 +152,25 @@ class UserServiceTest {
assertEquals(AuthenticationType.SAML2, captured.getAuthenticationType()); assertEquals(AuthenticationType.SAML2, captured.getAuthenticationType());
} }
@Test
void getCurrentUsernameReturnsNullWhenAuthenticationMissing() {
SecurityContextHolder.clearContext();
assertNull(userService.getCurrentUsername());
}
@Test
void getCurrentUsernameReturnsUsernameForAuthenticatedPrincipal() {
SecurityContextHolder.getContext()
.setAuthentication(
new UsernamePasswordAuthenticationToken(
"alice", "n/a", java.util.List.of()));
assertEquals("alice", userService.getCurrentUsername());
SecurityContextHolder.clearContext();
}
@Test @Test
void addApiKeyToUserGeneratesAndPersists() { void addApiKeyToUserGeneratesAndPersists() {
User user = new User(); User user = new User();
+1 -1
View File
@@ -78,7 +78,7 @@ springBoot {
allprojects { allprojects {
group = 'stirling.software' group = 'stirling.software'
version = '2.5.3' version = '2.6.0'
configurations.configureEach { configurations.configureEach {
exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat" exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat"