package eu.eudat.service.keycloak; import eu.eudat.convention.ConventionService; import gr.cite.commons.web.keycloak.api.configuration.KeycloakClientConfiguration; 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; import java.util.*; @Service public class KeycloakServiceImpl implements KeycloakService { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(KeycloakServiceImpl.class)); private final MyKeycloakAdminRestApi api; private final KeycloakResourcesConfiguration configuration; private final ConventionService conventionService; @Autowired public KeycloakServiceImpl(MyKeycloakAdminRestApi api, KeycloakResourcesConfiguration configuration, ConventionService conventionService) { this.api = api; this.configuration = configuration; this.conventionService = conventionService; } @Override public void addUserToGroup(@NotNull String subjectId, String groupId) { api.users().addUserToGroup(subjectId, groupId); } @Override public void removeUserFromGroup(@NotNull String subjectId, String groupId) { api.users().removeUserFromGroup(subjectId, groupId); } @Override public void addUserToGlobalRoleGroup(String subjectId, String role) { if (this.configuration.getProperties().getAuthorities() == null) return; KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().getOrDefault(role, null); if (properties != null) addUserToGroup(subjectId, properties.getGroupId()); } @Override public void removeUserGlobalRoleGroup(@NotNull String subjectId, String role) { if (this.configuration.getProperties().getAuthorities() == null) return; KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().getOrDefault(role, null); if (properties != null) removeUserFromGroup(subjectId, properties.getGroupId()); } @Override public void addUserToTenantRoleGroup(String subjectId, String tenantCode, String tenantRole) { 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 public void removeUserTenantRoleGroup(String subjectId, String tenantCode, String tenantRole) { 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()); } private String getTenantAuthorityParentPath(KeycloakTenantAuthorityProperties keycloakTenantAuthorityProperties) { GroupRepresentation parent = api.groups().findGroupById(keycloakTenantAuthorityProperties.getParent()); return parent.getPath(); } @Override public void createTenantGroups(String tenantCode) { if (this.configuration.getProperties().getTenantAuthorities() == null) return; for (Map.Entry entry :configuration.getProperties().getTenantAuthorities().entrySet()){ GroupRepresentation group = new GroupRepresentation(); group.setName(configuration.getTenantGroupName(tenantCode)); HashMap> user_attributes = new HashMap<>(); if (!this.conventionService.isNullOrEmpty(this.configuration.getProperties().getTenantRoleAttributeName())) user_attributes.put(this.configuration.getProperties().getTenantRoleAttributeName(), List.of(configuration.getTenantRoleAttributeValue(tenantCode, entry.getValue()))); group.setAttributes(user_attributes); api.groups().addGroupWithParent(group, entry.getValue().getParent()); } } }