Search in sources :

Example 1 with ResourceType

use of org.keycloak.events.admin.ResourceType in project keycloak by keycloak.

the class JpaAdminEventQuery method resourceType.

@Override
public AdminEventQuery resourceType(ResourceType... resourceTypes) {
    List<String> resourceTypeStrings = new LinkedList<String>();
    for (ResourceType e : resourceTypes) {
        resourceTypeStrings.add(e.toString());
    }
    predicates.add(root.get("resourceType").in(resourceTypeStrings));
    return this;
}
Also used : ResourceType(org.keycloak.events.admin.ResourceType) LinkedList(java.util.LinkedList)

Example 2 with ResourceType

use of org.keycloak.events.admin.ResourceType in project keycloak by keycloak.

the class RealmAdminResource method getEvents.

/**
 * Get admin events
 *
 * Returns all admin events, or filters events based on URL query parameters listed here
 *
 * @param operationTypes
 * @param authRealm
 * @param authClient
 * @param authUser user id
 * @param authIpAddress
 * @param resourcePath
 * @param dateTo
 * @param dateFrom
 * @param firstResult
 * @param maxResults Maximum results size (defaults to 100)
 * @return
 */
@Path("admin-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<AdminEventRepresentation> getEvents(@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, @QueryParam("resourceTypes") List<String> resourceTypes) {
    auth.realm().requireViewEvents();
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    AdminEventQuery query = eventStore.createAdminQuery().realm(realm.getId());
    ;
    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 (resourceTypes != null && !resourceTypes.isEmpty()) {
        ResourceType[] t = new ResourceType[resourceTypes.size()];
        for (int i = 0; i < t.length; i++) {
            t[i] = ResourceType.valueOf(resourceTypes.get(i));
        }
        query.resourceType(t);
    }
    if (dateFrom != null) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date from = null;
        try {
            from = df.parse(dateFrom);
        } catch (ParseException e) {
            throw new BadRequestException("Invalid value for 'Date(From)', expected format is yyyy-MM-dd");
        }
        query.fromTime(from);
    }
    if (dateTo != null) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date to = null;
        try {
            to = df.parse(dateTo);
        } catch (ParseException e) {
            throw new BadRequestException("Invalid value for 'Date(To)', expected format is yyyy-MM-dd");
        }
        query.toTime(to);
    }
    if (firstResult != null) {
        query.firstResult(firstResult);
    }
    if (maxResults != null) {
        query.maxResults(maxResults);
    } else {
        query.maxResults(Constants.DEFAULT_MAX_RESULTS);
    }
    return query.getResultStream().map(ModelToRepresentation::toRepresentation);
}
Also used : AdminEventQuery(org.keycloak.events.admin.AdminEventQuery) BadRequestException(javax.ws.rs.BadRequestException) ResourceType(org.keycloak.events.admin.ResourceType) OperationType(org.keycloak.events.admin.OperationType) ParseException(java.text.ParseException) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) SimpleDateFormat(java.text.SimpleDateFormat) 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 3 with ResourceType

use of org.keycloak.events.admin.ResourceType in project keycloak by keycloak.

the class ResourceSetService method getPermissions.

@Path("{id}/permissions")
@GET
@NoCache
@Produces("application/json")
public Response getPermissions(@PathParam("id") String id) {
    requireView();
    StoreFactory storeFactory = authorization.getStoreFactory();
    ResourceStore resourceStore = storeFactory.getResourceStore();
    Resource model = resourceStore.findById(id, resourceServer.getId());
    if (model == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    PolicyStore policyStore = authorization.getStoreFactory().getPolicyStore();
    Set<Policy> policies = new HashSet<>();
    policies.addAll(policyStore.findByResource(model.getId(), resourceServer.getId()));
    if (model.getType() != null) {
        policies.addAll(policyStore.findByResourceType(model.getType(), resourceServer.getId()));
        Map<Resource.FilterOption, String[]> resourceFilter = new EnumMap<>(Resource.FilterOption.class);
        resourceFilter.put(Resource.FilterOption.OWNER, new String[] { resourceServer.getId() });
        resourceFilter.put(Resource.FilterOption.TYPE, new String[] { model.getType() });
        for (Resource resourceType : resourceStore.findByResourceServer(resourceFilter, resourceServer.getId(), -1, -1)) {
            policies.addAll(policyStore.findByResource(resourceType.getId(), resourceServer.getId()));
        }
    }
    policies.addAll(policyStore.findByScopeIds(model.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toList()), id, resourceServer.getId()));
    policies.addAll(policyStore.findByScopeIds(model.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toList()), null, resourceServer.getId()));
    List<PolicyRepresentation> representation = new ArrayList<>();
    for (Policy policyModel : policies) {
        if (!"uma".equalsIgnoreCase(policyModel.getType())) {
            PolicyRepresentation policy = new PolicyRepresentation();
            policy.setId(policyModel.getId());
            policy.setName(policyModel.getName());
            policy.setType(policyModel.getType());
            if (!representation.contains(policy)) {
                representation.add(policy);
            }
        }
    }
    return Response.ok(representation).build();
}
Also used : Policy(org.keycloak.authorization.model.Policy) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ResourceType(org.keycloak.events.admin.ResourceType) Produces(javax.ws.rs.Produces) BiFunction(java.util.function.BiFunction) Path(javax.ws.rs.Path) OAuthErrorException(org.keycloak.OAuthErrorException) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) ErrorResponseException(org.keycloak.services.ErrorResponseException) ModelToRepresentation.toRepresentation(org.keycloak.models.utils.ModelToRepresentation.toRepresentation) Map(java.util.Map) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) DELETE(javax.ws.rs.DELETE) RealmModel(org.keycloak.models.RealmModel) EnumMap(java.util.EnumMap) Collection(java.util.Collection) Set(java.util.Set) PolicyStore(org.keycloak.authorization.store.PolicyStore) ResourceStore(org.keycloak.authorization.store.ResourceStore) Collectors(java.util.stream.Collectors) List(java.util.List) Response(javax.ws.rs.core.Response) RepresentationToModel.toModel(org.keycloak.models.utils.RepresentationToModel.toModel) ClientModel(org.keycloak.models.ClientModel) OperationType(org.keycloak.events.admin.OperationType) PathParam(javax.ws.rs.PathParam) Scope(org.keycloak.authorization.model.Scope) GET(javax.ws.rs.GET) StoreFactory(org.keycloak.authorization.store.StoreFactory) Constants(org.keycloak.models.Constants) HashMap(java.util.HashMap) Function(java.util.function.Function) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UserModel(org.keycloak.models.UserModel) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) Status(javax.ws.rs.core.Response.Status) PathMatcher(org.keycloak.common.util.PathMatcher) ResourceServer(org.keycloak.authorization.model.ResourceServer) POST(javax.ws.rs.POST) AdminPermissionEvaluator(org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator) KeycloakSession(org.keycloak.models.KeycloakSession) Policy(org.keycloak.authorization.model.Policy) NoCache(org.jboss.resteasy.annotations.cache.NoCache) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) Resource(org.keycloak.authorization.model.Resource) AdminEventBuilder(org.keycloak.services.resources.admin.AdminEventBuilder) Resource(org.keycloak.authorization.model.Resource) ArrayList(java.util.ArrayList) ResourceStore(org.keycloak.authorization.store.ResourceStore) StoreFactory(org.keycloak.authorization.store.StoreFactory) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) PolicyStore(org.keycloak.authorization.store.PolicyStore) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

ResourceType (org.keycloak.events.admin.ResourceType)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 NoCache (org.jboss.resteasy.annotations.cache.NoCache)2 OperationType (org.keycloak.events.admin.OperationType)2 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1