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));
}
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);
}
}
}
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);
}
Aggregations