Search in sources :

Example 1 with ClientAttributeEntity

use of org.keycloak.models.jpa.entities.ClientAttributeEntity in project keycloak by keycloak.

the class JpaRealmProvider method searchClientsByAttributes.

@Override
public Stream<ClientModel> searchClientsByAttributes(RealmModel realm, Map<String, String> attributes, Integer firstResult, Integer maxResults) {
    Map<String, String> filteredAttributes = clientSearchableAttributes == null ? attributes : attributes.entrySet().stream().filter(m -> clientSearchableAttributes.contains(m.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<String> queryBuilder = builder.createQuery(String.class);
    Root<ClientEntity> root = queryBuilder.from(ClientEntity.class);
    queryBuilder.select(root.get("id"));
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(root.get("realmId"), realm.getId()));
    for (Map.Entry<String, String> entry : filteredAttributes.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        Join<ClientEntity, ClientAttributeEntity> attributeJoin = root.join("attributes");
        Predicate attrNamePredicate = builder.equal(attributeJoin.get("name"), key);
        Predicate attrValuePredicate = builder.equal(attributeJoin.get("value"), value);
        predicates.add(builder.and(attrNamePredicate, attrValuePredicate));
    }
    Predicate finalPredicate = builder.and(predicates.toArray(new Predicate[0]));
    queryBuilder.where(finalPredicate).orderBy(builder.asc(root.get("clientId")));
    TypedQuery<String> query = em.createQuery(queryBuilder);
    return closing(paginateQuery(query, firstResult, maxResults).getResultStream()).map(id -> session.clients().getClientById(realm, id));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ClientEntity(org.keycloak.models.jpa.entities.ClientEntity) ClientAttributeEntity(org.keycloak.models.jpa.entities.ClientAttributeEntity) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ClientAttributeEntity

use of org.keycloak.models.jpa.entities.ClientAttributeEntity in project keycloak by keycloak.

the class ClientAdapter method removeAttribute.

@Override
public void removeAttribute(String name) {
    Iterator<ClientAttributeEntity> it = entity.getAttributes().iterator();
    while (it.hasNext()) {
        ClientAttributeEntity attr = it.next();
        if (attr.getName().equals(name)) {
            it.remove();
            em.remove(attr);
        }
    }
}
Also used : ClientAttributeEntity(org.keycloak.models.jpa.entities.ClientAttributeEntity)

Example 3 with ClientAttributeEntity

use of org.keycloak.models.jpa.entities.ClientAttributeEntity in project keycloak by keycloak.

the class ClientAdapter method setAttribute.

@Override
public void setAttribute(String name, String value) {
    boolean valueUndefined = value == null || "".equals(value.trim());
    for (ClientAttributeEntity attr : entity.getAttributes()) {
        if (attr.getName().equals(name)) {
            // we should remove this in future versions so that new clients never store empty/null attributes
            if (valueUndefined) {
                removeAttribute(name);
            } else {
                attr.setValue(value);
            }
            return;
        }
    }
    // do not create attributes if empty or null
    if (valueUndefined) {
        return;
    }
    ClientAttributeEntity attr = new ClientAttributeEntity();
    attr.setName(name);
    attr.setValue(value);
    attr.setClient(entity);
    em.persist(attr);
    entity.getAttributes().add(attr);
}
Also used : ClientAttributeEntity(org.keycloak.models.jpa.entities.ClientAttributeEntity)

Aggregations

ClientAttributeEntity (org.keycloak.models.jpa.entities.ClientAttributeEntity)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 ClientEntity (org.keycloak.models.jpa.entities.ClientEntity)1