use of org.keycloak.models.jpa.entities.ClientEntity in project keycloak by keycloak.
the class JpaRealmProvider method addClient.
@Override
public ClientModel addClient(RealmModel realm, String id, String clientId) {
if (id == null) {
id = KeycloakModelUtils.generateId();
}
if (clientId == null) {
clientId = id;
}
logger.tracef("addClient(%s, %s, %s)%s", realm, id, clientId, getShortStackTrace());
ClientEntity entity = new ClientEntity();
entity.setId(id);
entity.setClientId(clientId);
entity.setEnabled(true);
entity.setStandardFlowEnabled(true);
entity.setRealmId(realm.getId());
em.persist(entity);
final ClientModel resource = new ClientAdapter(realm, em, session, entity);
session.getKeycloakSessionFactory().publish((ClientModel.ClientCreationEvent) () -> resource);
return resource;
}
use of org.keycloak.models.jpa.entities.ClientEntity in project keycloak by keycloak.
the class JpaRealmProvider method getClientById.
@Override
public ClientModel getClientById(RealmModel realm, String id) {
logger.tracef("getClientById(%s, %s)%s", realm, id, getShortStackTrace());
ClientEntity client = em.find(ClientEntity.class, id);
// Check if client belongs to this realm
if (client == null || !realm.getId().equals(client.getRealmId()))
return null;
ClientAdapter adapter = new ClientAdapter(realm, em, session, client);
return adapter;
}
use of org.keycloak.models.jpa.entities.ClientEntity 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.ClientEntity in project keycloak by keycloak.
the class JpaRealmProvider method removeClient.
@Override
public boolean removeClient(RealmModel realm, String id) {
logger.tracef("removeClient(%s, %s)%s", realm, id, getShortStackTrace());
final ClientModel client = getClientById(realm, id);
if (client == null)
return false;
session.users().preRemove(realm, client);
session.roles().removeRoles(client);
ClientEntity clientEntity = em.find(ClientEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
session.getKeycloakSessionFactory().publish(new ClientModel.ClientRemovedEvent() {
@Override
public ClientModel getClient() {
return client;
}
@Override
public KeycloakSession getKeycloakSession() {
return session;
}
});
int countRemoved = em.createNamedQuery("deleteClientScopeClientMappingByClient").setParameter("clientId", clientEntity.getId()).executeUpdate();
// i have no idea why, but this needs to come before deleteScopeMapping
em.remove(clientEntity);
try {
em.flush();
} catch (RuntimeException e) {
logger.errorv("Unable to delete client entity: {0} from realm {1}", client.getClientId(), realm.getName());
throw e;
}
return true;
}
Aggregations