DM-406 - Add setting that allows the update of the keycloak theme in master realm

This commit is contained in:
Andrei Isvoran 2023-09-06 13:33:36 +02:00 committed by Timo Bejan
parent 3f7311b272
commit ba0cbae9c4
9 changed files with 43 additions and 6 deletions

View File

@ -9,9 +9,9 @@ import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.knecon.fforesight.tenantcommons.EncryptionDecryptionService;
import com.knecon.fforesight.tenantcommons.TenantContext;
@ -97,7 +97,7 @@ public class SMTPConfigurationController implements SMTPConfigurationResource, P
private Map<String, String> convertSMTPConfigurationModelToMap(SMTPConfiguration smtpConfigurationModel) {
Map<String, Object> propertiesMap = objectMapper.convertValue(smtpConfigurationModel, Map.class);
Map<String, Object> propertiesMap = objectMapper.convertValue(smtpConfigurationModel, new TypeReference<>() {});
Map<String, String> stringPropertiesMap = new HashMap<>();
propertiesMap.forEach((key, value) -> {
if (value != null) {

View File

@ -23,6 +23,7 @@ public class SearchConnectionEntity {
@Convert(converter = JSONStringSetConverter.class)
private Set<String> hosts;
@Column(name = "search_port")
@Builder.Default
private int port = 9300;
@Column(name = "search_scheme")
private String scheme;

View File

@ -48,5 +48,6 @@ public class TenantEntity {
@Basic(fetch = FetchType.EAGER)
@Column(columnDefinition = "text")
@Convert(converter = JSONMapConverter.class)
@Builder.Default
private Map<String, Object> details = new HashMap<>();
}

View File

@ -29,5 +29,6 @@ public class TenantUserManagementProperties {
private String defaultTheme = "redaction";
private List<String> validRedirectUris = new ArrayList<>();
private KCRoleMapping kcRoleMapping = new KCRoleMapping();
private String loginTheme;
}

View File

@ -1,14 +1,14 @@
package com.knecon.fforesight.tenantusermanagement.service;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.account.UserRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.knecon.fforesight.keycloakcommons.security.KeycloakSecurity;
import com.knecon.fforesight.tenantcommons.model.AuthDetails;
import com.knecon.fforesight.tenantusermanagement.properties.TenantUserManagementProperties;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -21,18 +21,27 @@ public class RealmService {
private final TenantUserManagementProperties tenantUserManagementProperties;
public final static String MASTER_REALM = "master";
public RealmResource realm(String tenantId) {
return keycloak.getAdminClient().realm(tenantId);
}
@PostConstruct
public void updateTheme() {
log.info("Updating master realm theme: {}", tenantUserManagementProperties.getLoginTheme());
RealmRepresentation realmRepresentation = realm(MASTER_REALM).toRepresentation();
realmRepresentation.setLoginTheme(tenantUserManagementProperties.getLoginTheme());
realm(MASTER_REALM).update(realmRepresentation);
}
public String getEmail(RealmResource resource) {
var user = resource.users().list().stream().filter(userRepresentation -> userRepresentation.getUsername().equals("admin")).findFirst();
return user.isPresent() ? user.get().getEmail() : "";
}
public AuthDetails getOpenIdConnectDetails(String tenantId) {
String openIdDetails = tenantUserManagementProperties.getServerUrl() + "/realms/" + tenantId + "/.well-known/openid-configuration";

View File

@ -44,7 +44,7 @@ public class UserListingService {
var allRoles = tenantUserManagementProperties.getKcRoleMapping().getAllRoles();
for (var role : allRoles) {
if (realmRoles.contains(role)) {
Set<UserRepresentation> users = realm.roles().get(role).getRoleUserMembers(0, 500);
List<UserRepresentation> users = realm.roles().get(role).getUserMembers(0, 500);
usersByRole.put(role, users.stream().map(UserRepresentation::getId).collect(Collectors.toSet()));
}
}

View File

@ -94,6 +94,7 @@ fforesight:
default-client-id: 'swagger-ui-client'
tenant-user-management:
base-path: '/tenant-user-management'
login-theme: 'redaction'
tenant-exchange:
name: 'tenants-exchange'
user-exchange:

View File

@ -0,0 +1,23 @@
package com.knecon.fforesight.tests;
import static com.knecon.fforesight.tenantusermanagement.service.RealmService.MASTER_REALM;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.knecon.fforesight.AbstractTenantUserManagementIntegrationTest;
import com.knecon.fforesight.tenantusermanagement.service.RealmService;
public class ThemeTest extends AbstractTenantUserManagementIntegrationTest {
@Autowired
private RealmService realmService;
@Test
public void testLoginTheme() {
var realm = realmService.realm(MASTER_REALM).toRepresentation();
assertThat(realm.getLoginTheme()).isEqualTo("redaction");
}
}

View File

@ -103,6 +103,7 @@ fforesight:
server-url: http://localhost:28181
client-secret: adminClientSecret
client-id: adminClient
login-theme: redaction
kc-role-mapping:
roles:
- name: SUPER_USER