Search in sources :

Example 1 with ClientEntity

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;
}
Also used : ClientEntity(org.keycloak.models.jpa.entities.ClientEntity) ClientModel(org.keycloak.models.ClientModel)

Example 2 with ClientEntity

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;
}
Also used : ClientEntity(org.keycloak.models.jpa.entities.ClientEntity)

Example 3 with ClientEntity

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));
}
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 4 with ClientEntity

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;
}
Also used : ClientModel(org.keycloak.models.ClientModel) ClientEntity(org.keycloak.models.jpa.entities.ClientEntity) KeycloakSession(org.keycloak.models.KeycloakSession)

Aggregations

ClientEntity (org.keycloak.models.jpa.entities.ClientEntity)4 ClientModel (org.keycloak.models.ClientModel)2 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 KeycloakSession (org.keycloak.models.KeycloakSession)1 ClientAttributeEntity (org.keycloak.models.jpa.entities.ClientAttributeEntity)1