Skip to content
Snippets Groups Projects
Select Git revision
  • 90c18b5ad9acf0b6cb8e6fd579b0caf47afd8c58
  • main default protected
  • OZG-7324-License-fix
  • release
  • 1.3.0
  • 1.2.0
  • 1.1.1
  • 1.0.0
8 results

handler.go

Blame
  • UserResourceMapper.java 3.11 KiB
    package de.itvsh.kop.user;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    import java.util.List;
    import java.util.Objects;
    import java.util.Optional;
    
    import javax.inject.Inject;
    
    import org.keycloak.admin.client.resource.UserResource;
    import org.keycloak.representations.idm.ClientMappingsRepresentation;
    import org.keycloak.representations.idm.RoleRepresentation;
    import org.mapstruct.Mapper;
    import org.mapstruct.Mapping;
    import org.mapstruct.ReportingPolicy;
    
    import de.itvsh.kop.user.keycloak.KeycloakApiProperties;
    
    @Mapper(unmappedTargetPolicy = ReportingPolicy.WARN)
    public abstract class UserResourceMapper {
    
    	@Inject
    	KeycloakApiProperties properties;
    
    	@Mapping(target = "createdAt", expression = "java(mapCreatedAt(userRes))")
    	@Mapping(target = "email", expression = "java(mapEmail(userRes))")
    	@Mapping(target = "firstName", expression = "java(mapFirstName(userRes))")
    	@Mapping(target = "lastName", expression = "java(mapLastName(userRes))")
    	@Mapping(target = "username", expression = "java(mapUsername(userRes))")
    	@Mapping(target = "externalId", expression = "java(mapId(userRes))")
    	@Mapping(target = "organisationsEinheitIds", expression = "java(mapOrganisationsEinheitIds(userRes))")
    	@Mapping(target = "roles", expression = "java(mapRoles(userRes))")
    	@Mapping(target = "lastSyncTimestamp", ignore = true)
    	@Mapping(target = "deleted", ignore = true)
    	public abstract User toKopUser(UserResource userRes);
    
    	Date mapCreatedAt(UserResource userRes) {
    		var createdAt = userRes.toRepresentation().getCreatedTimestamp();
    
    		return createdAt != null ? new Date(createdAt) : new Date();
    	}
    
    	List<String> mapOrganisationsEinheitIds(UserResource userRes) {
    		return getOrganisationsEinheitIdsFromUserAttributes(userRes);
    	}
    
    	private List<String> getOrganisationsEinheitIdsFromUserAttributes(UserResource userResource) {
    		var attributes = userResource.toRepresentation().getAttributes();
    		return attributes != null ? attributes.get(properties.organisationsEinheitIdKey()) : new ArrayList<>();
    	}
    
    	List<String> mapRoles(UserResource userRes) {
    		var roleRepresentation = Optional.ofNullable(userRes.roles().getAll().getClientMappings())
    				.filter(Objects::nonNull)
    				.filter(map -> map.containsKey(properties.client()))
    				.map(map -> map.get(properties.client()))
    				.map(ClientMappingsRepresentation::getMappings)
    				.orElseGet(Collections::emptyList);
    
    		return roleRepresentation.stream().map(RoleRepresentation::getName).toList();
    	}
    
    	String mapId(UserResource userRes) {
    		var userRepresentation = userRes.toRepresentation();
    		var attributes = userRepresentation.getAttributes();
    		var id = attributes != null ? attributes.get(properties.ldapIdKey()) : null;
    		return id != null ? id.get(0) : userRepresentation.getId();
    	}
    
    	String mapEmail(UserResource userRes) {
    		return userRes.toRepresentation().getEmail();
    	}
    
    	String mapFirstName(UserResource userRes) {
    		return userRes.toRepresentation().getFirstName();
    	}
    
    	String mapLastName(UserResource userRes) {
    		return userRes.toRepresentation().getLastName();
    	}
    
    	String mapUsername(UserResource userRes) {
    		return userRes.toRepresentation().getUsername();
    	}
    }