diff --git a/services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java b/services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java index 3b030739df..fc311fc341 100644 --- a/services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java @@ -26,6 +26,7 @@ import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.ModelException; import org.keycloak.models.RealmModel; +import org.keycloak.models.RoleContainerModel; import org.keycloak.models.RoleMapperModel; import org.keycloak.models.RoleModel; import org.keycloak.models.utils.ModelToRepresentation; @@ -33,7 +34,6 @@ import org.keycloak.representations.idm.ClientMappingsRepresentation; import org.keycloak.representations.idm.MappingsRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.services.ErrorResponseException; -import org.keycloak.services.managers.RealmManager; import org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator; import javax.ws.rs.Consumes; @@ -62,6 +62,7 @@ import java.util.stream.Collectors; * * @resource Role Mapper * @author Bill Burke + * @author Miguel P. Nunes * @version $Revision: 1 $ */ public class RoleMapperResource { @@ -113,36 +114,33 @@ public class RoleMapperResource { public MappingsRepresentation getRoleMappings() { viewPermission.require(); - MappingsRepresentation all = new MappingsRepresentation(); - Set realmMappings = roleMapper.getRealmRoleMappings(); - RealmManager manager = new RealmManager(session); - if (realmMappings.size() > 0) { - List realmRep = new ArrayList(); - for (RoleModel roleModel : realmMappings) { - realmRep.add(ModelToRepresentation.toBriefRepresentation(roleModel)); + List realmRolesRepresentation = new ArrayList<>(); + Map appMappings = new HashMap<>(); + + ClientModel clientModel; + ClientMappingsRepresentation mappings; + + for (RoleModel roleMapping : roleMapper.getRoleMappings()) { + RoleContainerModel container = roleMapping.getContainer(); + if (container instanceof RealmModel) { + realmRolesRepresentation.add(ModelToRepresentation.toBriefRepresentation(roleMapping)); + } else if (container instanceof ClientModel) { + clientModel = (ClientModel) container; + if ((mappings = appMappings.get(clientModel.getClientId())) == null) { + mappings = new ClientMappingsRepresentation(); + mappings.setId(clientModel.getId()); + mappings.setClient(clientModel.getClientId()); + mappings.setMappings(new ArrayList<>()); + appMappings.put(clientModel.getClientId(), mappings); + } + mappings.getMappings().add(ModelToRepresentation.toBriefRepresentation(roleMapping)); } - all.setRealmMappings(realmRep); } - List clients = realm.getClients(); - if (clients.size() > 0) { - Map appMappings = new HashMap(); - for (ClientModel client : clients) { - Set roleMappings = roleMapper.getClientRoleMappings(client); - if (roleMappings.size() > 0) { - ClientMappingsRepresentation mappings = new ClientMappingsRepresentation(); - mappings.setId(client.getId()); - mappings.setClient(client.getClientId()); - List roles = new ArrayList(); - mappings.setMappings(roles); - for (RoleModel role : roleMappings) { - roles.add(ModelToRepresentation.toBriefRepresentation(role)); - } - appMappings.put(client.getClientId(), mappings); - all.setClientMappings(appMappings); - } - } - } + MappingsRepresentation all = new MappingsRepresentation(); + if (!realmRolesRepresentation.isEmpty()) all.setRealmMappings(realmRolesRepresentation); + if (!appMappings.isEmpty()) all.setClientMappings(appMappings); + return all; }