Search in sources :

Example 1 with ClientMappingsRepresentation

use of org.keycloak.representations.idm.ClientMappingsRepresentation in project keycloak by keycloak.

the class ScopeMappedResource method getScopeMappings.

/**
 * Get all scope mappings for the client
 *
 * @return
 * @deprecated the method is not used neither from admin console or from admin client. It may be removed in future releases.
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
@Deprecated
public MappingsRepresentation getScopeMappings() {
    viewPermission.require();
    if (scopeContainer == null) {
        throw new NotFoundException("Could not find client");
    }
    MappingsRepresentation all = new MappingsRepresentation();
    List<RoleRepresentation> realmRep = scopeContainer.getRealmScopeMappingsStream().map(ModelToRepresentation::toBriefRepresentation).collect(Collectors.toList());
    if (!realmRep.isEmpty()) {
        all.setRealmMappings(realmRep);
    }
    Stream<ClientModel> clients = realm.getClientsStream();
    Map<String, ClientMappingsRepresentation> clientMappings = clients.map(c -> ScopeMappedUtil.toClientMappingsRepresentation(c, scopeContainer)).filter(Objects::nonNull).collect(Collectors.toMap(ClientMappingsRepresentation::getClient, Function.identity()));
    if (!clientMappings.isEmpty()) {
        all.setClientMappings(clientMappings);
    }
    return all;
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) ClientModel(org.keycloak.models.ClientModel) MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) NotFoundException(javax.ws.rs.NotFoundException) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 2 with ClientMappingsRepresentation

use of org.keycloak.representations.idm.ClientMappingsRepresentation in project keycloak by keycloak.

the class ScopeMappedUtil method toClientMappingsRepresentation.

public static ClientMappingsRepresentation toClientMappingsRepresentation(ClientModel client, ScopeContainerModel scopeContainer) {
    Set<RoleModel> roleMappings = KeycloakModelUtils.getClientScopeMappings(client, scopeContainer);
    if (!roleMappings.isEmpty()) {
        ClientMappingsRepresentation mappings = new ClientMappingsRepresentation();
        mappings.setId(client.getId());
        mappings.setClient(client.getClientId());
        List<RoleRepresentation> roles = new LinkedList<>();
        mappings.setMappings(roles);
        for (RoleModel role : roleMappings) {
            roles.add(ModelToRepresentation.toBriefRepresentation(role));
        }
        return mappings;
    } else {
        return null;
    }
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RoleModel(org.keycloak.models.RoleModel) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) LinkedList(java.util.LinkedList)

Example 3 with ClientMappingsRepresentation

use of org.keycloak.representations.idm.ClientMappingsRepresentation in project keycloak by keycloak.

the class RoleMapperResource method getRoleMappings.

/**
 * Get role mappings
 *
 * @return
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public MappingsRepresentation getRoleMappings() {
    viewPermission.require();
    List<RoleRepresentation> realmRolesRepresentation = new ArrayList<>();
    Map<String, ClientMappingsRepresentation> appMappings = new HashMap<>();
    final AtomicReference<ClientMappingsRepresentation> mappings = new AtomicReference<>();
    roleMapper.getRoleMappingsStream().forEach(roleMapping -> {
        RoleContainerModel container = roleMapping.getContainer();
        if (container instanceof RealmModel) {
            realmRolesRepresentation.add(ModelToRepresentation.toBriefRepresentation(roleMapping));
        } else if (container instanceof ClientModel) {
            ClientModel clientModel = (ClientModel) container;
            mappings.set(appMappings.get(clientModel.getClientId()));
            if (mappings.get() == null) {
                mappings.set(new ClientMappingsRepresentation());
                mappings.get().setId(clientModel.getId());
                mappings.get().setClient(clientModel.getClientId());
                mappings.get().setMappings(new ArrayList<>());
                appMappings.put(clientModel.getClientId(), mappings.get());
            }
            mappings.get().getMappings().add(ModelToRepresentation.toBriefRepresentation(roleMapping));
        }
    });
    MappingsRepresentation all = new MappingsRepresentation();
    if (!realmRolesRepresentation.isEmpty())
        all.setRealmMappings(realmRolesRepresentation);
    if (!appMappings.isEmpty())
        all.setClientMappings(appMappings);
    return all;
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) RoleContainerModel(org.keycloak.models.RoleContainerModel) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

ClientMappingsRepresentation (org.keycloak.representations.idm.ClientMappingsRepresentation)3 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)3 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 NoCache (org.jboss.resteasy.annotations.cache.NoCache)2 ClientModel (org.keycloak.models.ClientModel)2 MappingsRepresentation (org.keycloak.representations.idm.MappingsRepresentation)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NotFoundException (javax.ws.rs.NotFoundException)1 RealmModel (org.keycloak.models.RealmModel)1 RoleContainerModel (org.keycloak.models.RoleContainerModel)1 RoleModel (org.keycloak.models.RoleModel)1