argos/dmp-backend/core/src/main/java/eu/eudat/service/keycloak/KeycloakServiceImpl.java

88 lines
4.5 KiB
Java
Raw Normal View History

2023-11-29 15:12:56 +01:00
package eu.eudat.service.keycloak;
2024-04-16 13:21:37 +02:00
import eu.eudat.convention.ConventionService;
import gr.cite.commons.web.keycloak.api.configuration.KeycloakClientConfiguration;
2023-11-29 15:12:56 +01:00
import gr.cite.tools.logging.LoggerService;
import org.jetbrains.annotations.NotNull;
import org.keycloak.representations.idm.GroupRepresentation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
2024-04-01 10:16:19 +02:00
import java.util.*;
2023-11-29 15:12:56 +01:00
@Service
public class KeycloakServiceImpl implements KeycloakService {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(KeycloakServiceImpl.class));
private final MyKeycloakAdminRestApi api;
2023-11-29 15:12:56 +01:00
private final KeycloakResourcesConfiguration configuration;
2024-04-16 13:21:37 +02:00
private final ConventionService conventionService;
2023-11-29 15:12:56 +01:00
@Autowired
2024-04-16 13:21:37 +02:00
public KeycloakServiceImpl(MyKeycloakAdminRestApi api, KeycloakResourcesConfiguration configuration, ConventionService conventionService) {
2023-11-29 15:12:56 +01:00
this.api = api;
this.configuration = configuration;
2024-04-16 13:21:37 +02:00
this.conventionService = conventionService;
2023-11-29 15:12:56 +01:00
}
@Override
2024-04-16 12:17:58 +02:00
public void addUserToGroup(@NotNull String subjectId, String groupId) {
api.users().addUserToGroup(subjectId, groupId);
2023-11-29 15:12:56 +01:00
}
@Override
2024-04-16 12:17:58 +02:00
public void removeUserFromGroup(@NotNull String subjectId, String groupId) {
api.users().removeUserFromGroup(subjectId, groupId);
2023-11-29 15:12:56 +01:00
}
@Override
2024-04-16 12:17:58 +02:00
public void addUserToGlobalRoleGroup(String subjectId, String role) {
2024-04-16 13:21:37 +02:00
if (this.configuration.getProperties().getAuthorities() == null) return;
KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().getOrDefault(role, null);
2024-04-16 12:17:58 +02:00
if (properties != null) addUserToGroup(subjectId, properties.getGroupId());
2023-11-29 15:12:56 +01:00
}
@Override
2024-04-16 12:17:58 +02:00
public void removeUserGlobalRoleGroup(@NotNull String subjectId, String role) {
2024-04-16 13:21:37 +02:00
if (this.configuration.getProperties().getAuthorities() == null) return;
KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().getOrDefault(role, null);
if (properties != null) removeUserFromGroup(subjectId, properties.getGroupId());
2023-11-29 15:12:56 +01:00
}
@Override
2024-04-16 12:17:58 +02:00
public void addUserToTenantRoleGroup(String subjectId, String tenantCode, String tenantRole) {
2024-04-16 13:21:37 +02:00
if (this.configuration.getProperties().getAuthorities() == null) return;
KeycloakTenantAuthorityProperties properties = this.configuration.getProperties().getTenantAuthorities().getOrDefault(tenantRole, null);
if (properties == null) return;
GroupRepresentation group = api.groups().findGroupByPath(getTenantAuthorityParentPath(properties) + "/" + configuration.getTenantGroupName(tenantCode));
if (group != null) addUserToGroup(subjectId, group.getId());
}
@Override
2024-04-16 12:17:58 +02:00
public void removeUserTenantRoleGroup(String subjectId, String tenantCode, String tenantRole) {
2024-04-16 13:21:37 +02:00
KeycloakTenantAuthorityProperties properties = this.configuration.getProperties().getTenantAuthorities().getOrDefault(tenantRole, null);
if (properties == null) return;
GroupRepresentation group = api.groups().findGroupByPath(getTenantAuthorityParentPath(properties) + "/" + configuration.getTenantGroupName(tenantCode));
if (group != null) removeUserFromGroup(subjectId, group.getId());
}
2024-04-16 12:17:58 +02:00
2024-04-16 13:21:37 +02:00
private String getTenantAuthorityParentPath(KeycloakTenantAuthorityProperties keycloakTenantAuthorityProperties) {
GroupRepresentation parent = api.groups().findGroupById(keycloakTenantAuthorityProperties.getParent());
2024-04-16 12:17:58 +02:00
return parent.getPath();
2023-11-29 15:12:56 +01:00
}
2024-04-16 12:17:58 +02:00
@Override
public void createTenantGroups(String tenantCode) {
2024-04-16 13:21:37 +02:00
if (this.configuration.getProperties().getTenantAuthorities() == null) return;
2024-04-16 12:17:58 +02:00
for (Map.Entry<String,KeycloakTenantAuthorityProperties> entry :configuration.getProperties().getTenantAuthorities().entrySet()){
GroupRepresentation group = new GroupRepresentation();
group.setName(configuration.getTenantGroupName(tenantCode));
HashMap<String, List<String>> user_attributes = new HashMap<>();
2024-04-16 13:21:37 +02:00
if (!this.conventionService.isNullOrEmpty(this.configuration.getProperties().getTenantRoleAttributeName())) user_attributes.put(this.configuration.getProperties().getTenantRoleAttributeName(), List.of(configuration.getTenantRoleAttributeValue(tenantCode, entry.getValue())));
2024-04-16 12:17:58 +02:00
group.setAttributes(user_attributes);
2024-04-16 13:21:37 +02:00
api.groups().addGroupWithParent(group, entry.getValue().getParent());
2024-04-16 12:17:58 +02:00
}
}
2023-11-29 15:12:56 +01:00
}