Search in sources :

Example 1 with ModelToRepresentation

use of org.keycloak.models.utils.ModelToRepresentation in project keycloak by keycloak.

the class ScopeMappedResource method deleteRealmScopeMappings.

/**
 * Remove a set of realm-level roles from the client's scope
 *
 * @param roles
 */
@Path("realm")
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
public void deleteRealmScopeMappings(List<RoleRepresentation> roles) {
    managePermission.require();
    if (scopeContainer == null) {
        throw new NotFoundException("Could not find client");
    }
    if (roles == null) {
        roles = scopeContainer.getRealmScopeMappingsStream().peek(scopeContainer::deleteScopeMapping).map(ModelToRepresentation::toBriefRepresentation).collect(Collectors.toList());
    } else {
        for (RoleRepresentation role : roles) {
            RoleModel roleModel = realm.getRoleById(role.getId());
            if (roleModel == null) {
                throw new NotFoundException("Role not found");
            }
            scopeContainer.deleteScopeMapping(roleModel);
        }
    }
    adminEvent.operation(OperationType.DELETE).resourcePath(session.getContext().getUri()).representation(roles).success();
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) NotFoundException(javax.ws.rs.NotFoundException) RoleModel(org.keycloak.models.RoleModel) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes)

Example 2 with ModelToRepresentation

use of org.keycloak.models.utils.ModelToRepresentation in project keycloak by keycloak.

the class RoleByIdResource method getRoleComposites.

/**
 * Get role's children
 *
 * Returns a set of role's children provided the role is a composite.
 *
 * @param id
 * @return
 */
@Path("{role-id}/composites")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<RoleRepresentation> getRoleComposites(@PathParam("role-id") final String id, @QueryParam("search") final String search, @QueryParam("first") final Integer first, @QueryParam("max") final Integer max) {
    if (logger.isDebugEnabled())
        logger.debug("*** getRoleComposites: '" + id + "'");
    RoleModel role = getRoleModel(id);
    auth.roles().requireView(role);
    if (search == null && first == null && max == null) {
        return role.getCompositesStream().map(ModelToRepresentation::toBriefRepresentation);
    }
    return role.getCompositesStream(search, first, max).map(ModelToRepresentation::toBriefRepresentation);
}
Also used : RoleModel(org.keycloak.models.RoleModel) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 3 with ModelToRepresentation

use of org.keycloak.models.utils.ModelToRepresentation in project keycloak by keycloak.

the class TestingResourceProvider method getAdminEvents.

/**
 * Get admin events
 *
 * Returns all admin events, or filters events based on URL query parameters listed here
 *
 * @param realmId
 * @param operationTypes
 * @param authRealm
 * @param authClient
 * @param authUser user id
 * @param authIpAddress
 * @param resourcePath
 * @param dateFrom
 * @param dateTo
 * @param firstResult
 * @param maxResults
 * @return
 */
@Path("query-admin-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<AdminEventRepresentation> getAdminEvents(@QueryParam("realmId") String realmId, @QueryParam("operationTypes") List<String> operationTypes, @QueryParam("authRealm") String authRealm, @QueryParam("authClient") String authClient, @QueryParam("authUser") String authUser, @QueryParam("authIpAddress") String authIpAddress, @QueryParam("resourcePath") String resourcePath, @QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo, @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResults) {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    AdminEventQuery query = eventStore.createAdminQuery();
    if (realmId != null) {
        query.realm(realmId);
    }
    if (authRealm != null) {
        query.authRealm(authRealm);
    }
    if (authClient != null) {
        query.authClient(authClient);
    }
    if (authUser != null) {
        query.authUser(authUser);
    }
    if (authIpAddress != null) {
        query.authIpAddress(authIpAddress);
    }
    if (resourcePath != null) {
        query.resourcePath(resourcePath);
    }
    if (operationTypes != null && !operationTypes.isEmpty()) {
        OperationType[] t = new OperationType[operationTypes.size()];
        for (int i = 0; i < t.length; i++) {
            t[i] = OperationType.valueOf(operationTypes.get(i));
        }
        query.operation(t);
    }
    if (dateFrom != null) {
        Date from = formatDate(dateFrom, "Date(From)");
        query.fromTime(from);
    }
    if (dateTo != null) {
        Date to = formatDate(dateTo, "Date(To)");
        query.toTime(to);
    }
    if (firstResult != null || maxResults != null) {
        if (firstResult == null) {
            firstResult = 0;
        }
        if (maxResults == null) {
            maxResults = 100;
        }
        query.firstResult(firstResult);
        query.maxResults(maxResults);
    }
    return query.getResultStream().map(ModelToRepresentation::toRepresentation);
}
Also used : AdminEventQuery(org.keycloak.events.admin.AdminEventQuery) OperationType(org.keycloak.events.admin.OperationType) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Date(java.util.Date) EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 4 with ModelToRepresentation

use of org.keycloak.models.utils.ModelToRepresentation in project keycloak by keycloak.

the class TestingResourceProvider method queryEvents.

/**
 * Query events
 * <p>
 * Returns all events, or filters them based on URL query parameters listed here
 *
 * @param realmId     The realm
 * @param types       The types of events to return
 * @param client      App or oauth client name
 * @param user        User id
 * @param dateFrom    From date
 * @param dateTo      To date
 * @param ipAddress   IP address
 * @param firstResult Paging offset
 * @param maxResults  Paging size
 * @return
 */
@Path("query-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<EventRepresentation> queryEvents(@QueryParam("realmId") String realmId, @QueryParam("type") List<String> types, @QueryParam("client") String client, @QueryParam("user") String user, @QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo, @QueryParam("ipAddress") String ipAddress, @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResults) {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    EventQuery query = eventStore.createQuery();
    if (realmId != null) {
        query.realm(realmId);
    }
    if (client != null) {
        query.client(client);
    }
    if (types != null & !types.isEmpty()) {
        EventType[] t = new EventType[types.size()];
        for (int i = 0; i < t.length; i++) {
            t[i] = EventType.valueOf(types.get(i));
        }
        query.type(t);
    }
    if (user != null) {
        query.user(user);
    }
    if (dateFrom != null) {
        Date from = formatDate(dateFrom, "Date(From)");
        query.fromDate(from);
    }
    if (dateTo != null) {
        Date to = formatDate(dateTo, "Date(To)");
        query.toDate(to);
    }
    if (ipAddress != null) {
        query.ipAddress(ipAddress);
    }
    if (firstResult != null) {
        query.firstResult(firstResult);
    }
    if (maxResults != null) {
        query.maxResults(maxResults);
    }
    return query.getResultStream().map(ModelToRepresentation::toRepresentation);
}
Also used : EventType(org.keycloak.events.EventType) AdminEventQuery(org.keycloak.events.admin.AdminEventQuery) EventQuery(org.keycloak.events.EventQuery) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Date(java.util.Date) EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 5 with ModelToRepresentation

use of org.keycloak.models.utils.ModelToRepresentation in project keycloak by keycloak.

the class IdentityProviderResource method getMapperTypes.

/**
 * Get mapper types for identity provider
 */
@GET
@Path("mapper-types")
@NoCache
public Map<String, IdentityProviderMapperTypeRepresentation> getMapperTypes() {
    this.auth.realm().requireViewIdentityProviders();
    if (identityProviderModel == null) {
        throw new javax.ws.rs.NotFoundException();
    }
    KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
    return sessionFactory.getProviderFactoriesStream(IdentityProviderMapper.class).map(IdentityProviderMapper.class::cast).map(mapper -> Arrays.stream(mapper.getCompatibleProviders()).filter(type -> Objects.equals(IdentityProviderMapper.ANY_PROVIDER, type) || Objects.equals(identityProviderModel.getProviderId(), type)).map(type -> {
        IdentityProviderMapperTypeRepresentation rep = new IdentityProviderMapperTypeRepresentation();
        rep.setId(mapper.getId());
        rep.setCategory(mapper.getDisplayCategory());
        rep.setName(mapper.getDisplayType());
        rep.setHelpText(mapper.getHelpText());
        rep.setProperties(mapper.getConfigProperties().stream().map(ModelToRepresentation::toRepresentation).collect(Collectors.toList()));
        return rep;
    }).findFirst().orElse(null)).filter(Objects::nonNull).collect(Collectors.toMap(IdentityProviderMapperTypeRepresentation::getId, Function.identity()));
}
Also used : OperationType(org.keycloak.events.admin.OperationType) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) PathParam(javax.ws.rs.PathParam) Arrays(java.util.Arrays) ResourceType(org.keycloak.events.admin.ResourceType) IdentityProviderFactory(org.keycloak.broker.provider.IdentityProviderFactory) Produces(javax.ws.rs.Produces) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) GET(javax.ws.rs.GET) Logger(org.jboss.logging.Logger) Path(javax.ws.rs.Path) IdentityProviderMapperTypeRepresentation(org.keycloak.representations.idm.IdentityProviderMapperTypeRepresentation) Function(java.util.function.Function) RepresentationToModel(org.keycloak.models.utils.RepresentationToModel) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) MediaType(javax.ws.rs.core.MediaType) UserModel(org.keycloak.models.UserModel) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) AdminPermissionManagement(org.keycloak.services.resources.admin.permissions.AdminPermissionManagement) StripSecretsUtils(org.keycloak.models.utils.StripSecretsUtils) IdentityProvider(org.keycloak.broker.provider.IdentityProvider) IdentityProviderMapper(org.keycloak.broker.provider.IdentityProviderMapper) DELETE(javax.ws.rs.DELETE) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) AdminPermissions(org.keycloak.services.resources.admin.permissions.AdminPermissions) RealmModel(org.keycloak.models.RealmModel) POST(javax.ws.rs.POST) AdminPermissionEvaluator(org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator) KeycloakSession(org.keycloak.models.KeycloakSession) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ManagementPermissionReference(org.keycloak.representations.idm.ManagementPermissionReference) Objects(java.util.Objects) SocialIdentityProvider(org.keycloak.broker.social.SocialIdentityProvider) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Stream(java.util.stream.Stream) NoCache(org.jboss.resteasy.annotations.cache.NoCache) Response(javax.ws.rs.core.Response) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) PUT(javax.ws.rs.PUT) ErrorResponse(org.keycloak.services.ErrorResponse) IdentityProviderMapperTypeRepresentation(org.keycloak.representations.idm.IdentityProviderMapperTypeRepresentation) NotFoundException(javax.ws.rs.NotFoundException) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

ModelToRepresentation (org.keycloak.models.utils.ModelToRepresentation)12 Path (javax.ws.rs.Path)9 GET (javax.ws.rs.GET)8 Produces (javax.ws.rs.Produces)8 NoCache (org.jboss.resteasy.annotations.cache.NoCache)8 RoleModel (org.keycloak.models.RoleModel)8 NotFoundException (javax.ws.rs.NotFoundException)6 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5 Consumes (javax.ws.rs.Consumes)5 DELETE (javax.ws.rs.DELETE)5 OperationType (org.keycloak.events.admin.OperationType)5 KeycloakSession (org.keycloak.models.KeycloakSession)5 RealmModel (org.keycloak.models.RealmModel)5 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)5 ResourceType (org.keycloak.events.admin.ResourceType)4 Date (java.util.Date)3 List (java.util.List)3 Map (java.util.Map)3 Predicate (java.util.function.Predicate)3