Search in sources :

Example 6 with UserEntity

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

the class JpaUserProvider method setNotBeforeForUser.

@Override
public void setNotBeforeForUser(RealmModel realm, UserModel user, int notBefore) {
    UserEntity entity = em.getReference(UserEntity.class, user.getId());
    if (entity == null) {
        throw new ModelException("User does not exists");
    }
    entity.setNotBefore(notBefore);
}
Also used : ModelException(org.keycloak.models.ModelException) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 7 with UserEntity

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

the class JpaUserProvider method createCredential.

@Override
public CredentialModel createCredential(RealmModel realm, UserModel user, CredentialModel cred) {
    CredentialEntity entity = credentialStore.createCredentialEntity(realm, user, cred);
    UserEntity userEntity = userInEntityManagerContext(user.getId());
    if (userEntity != null) {
        userEntity.getCredentials().add(entity);
    }
    return toModel(entity);
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 8 with UserEntity

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

the class JpaUserProvider method findFederatedIdentity.

private FederatedIdentityEntity findFederatedIdentity(UserModel user, String identityProvider, LockModeType lockMode) {
    TypedQuery<FederatedIdentityEntity> query = em.createNamedQuery("findFederatedIdentityByUserAndProvider", FederatedIdentityEntity.class);
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    query.setParameter("user", userEntity);
    query.setParameter("identityProvider", identityProvider);
    query.setLockMode(lockMode);
    List<FederatedIdentityEntity> results = query.getResultList();
    return results.size() > 0 ? results.get(0) : null;
}
Also used : UserEntity(org.keycloak.models.jpa.entities.UserEntity) FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Example 9 with UserEntity

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

the class JpaUserProvider method searchForUserStream.

@Override
public Stream<UserModel> searchForUserStream(RealmModel realm, Map<String, String> attributes, Integer firstResult, Integer maxResults) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<UserEntity> queryBuilder = builder.createQuery(UserEntity.class);
    Root<UserEntity> root = queryBuilder.from(UserEntity.class);
    List<Predicate> predicates = new ArrayList<>();
    List<Predicate> attributePredicates = new ArrayList<>();
    predicates.add(builder.equal(root.get("realmId"), realm.getId()));
    if (!session.getAttributeOrDefault(UserModel.INCLUDE_SERVICE_ACCOUNT, true)) {
        predicates.add(root.get("serviceAccountClientLink").isNull());
    }
    Join<Object, Object> federatedIdentitiesJoin = null;
    for (Map.Entry<String, String> entry : attributes.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (value == null) {
            continue;
        }
        switch(key) {
            case UserModel.SEARCH:
                for (String stringToSearch : value.trim().split("\\s+")) {
                    predicates.add(builder.or(getSearchOptionPredicateArray(stringToSearch, builder, root)));
                }
                break;
            case USERNAME:
            case FIRST_NAME:
            case LAST_NAME:
            case EMAIL:
                if (Boolean.valueOf(attributes.getOrDefault(UserModel.EXACT, Boolean.FALSE.toString()))) {
                    predicates.add(builder.equal(builder.lower(root.get(key)), value.toLowerCase()));
                } else {
                    predicates.add(builder.like(builder.lower(root.get(key)), "%" + value.toLowerCase() + "%"));
                }
                break;
            case EMAIL_VERIFIED:
                predicates.add(builder.equal(root.get(key), Boolean.parseBoolean(value.toLowerCase())));
                break;
            case UserModel.ENABLED:
                predicates.add(builder.equal(root.get(key), Boolean.parseBoolean(value)));
                break;
            case UserModel.IDP_ALIAS:
                if (federatedIdentitiesJoin == null) {
                    federatedIdentitiesJoin = root.join("federatedIdentities");
                }
                predicates.add(builder.equal(federatedIdentitiesJoin.get("identityProvider"), value));
                break;
            case UserModel.IDP_USER_ID:
                if (federatedIdentitiesJoin == null) {
                    federatedIdentitiesJoin = root.join("federatedIdentities");
                }
                predicates.add(builder.equal(federatedIdentitiesJoin.get("userId"), value));
                break;
            case UserModel.EXACT:
                break;
            // All unknown attributes will be assumed as custom attributes
            default:
                Join<UserEntity, UserAttributeEntity> attributesJoin = root.join("attributes", JoinType.LEFT);
                attributePredicates.add(builder.and(builder.equal(builder.lower(attributesJoin.get("name")), key.toLowerCase()), builder.equal(builder.lower(attributesJoin.get("value")), value.toLowerCase())));
                break;
        }
    }
    if (!attributePredicates.isEmpty()) {
        predicates.add(builder.and(attributePredicates.toArray(new Predicate[0])));
    }
    Set<String> userGroups = (Set<String>) session.getAttribute(UserModel.GROUPS);
    if (userGroups != null) {
        Subquery subquery = queryBuilder.subquery(String.class);
        Root<UserGroupMembershipEntity> from = subquery.from(UserGroupMembershipEntity.class);
        subquery.select(builder.literal(1));
        List<Predicate> subPredicates = new ArrayList<>();
        subPredicates.add(from.get("groupId").in(userGroups));
        subPredicates.add(builder.equal(from.get("user").get("id"), root.get("id")));
        Subquery subquery1 = queryBuilder.subquery(String.class);
        subquery1.select(builder.literal(1));
        Root from1 = subquery1.from(ResourceEntity.class);
        List<Predicate> subs = new ArrayList<>();
        Expression<String> groupId = from.get("groupId");
        subs.add(builder.like(from1.get("name"), builder.concat("group.resource.", groupId)));
        subquery1.where(subs.toArray(new Predicate[subs.size()]));
        subPredicates.add(builder.exists(subquery1));
        subquery.where(subPredicates.toArray(new Predicate[subPredicates.size()]));
        predicates.add(builder.exists(subquery));
    }
    queryBuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get(UserModel.USERNAME)));
    TypedQuery<UserEntity> query = em.createQuery(queryBuilder);
    UserProvider users = session.users();
    return closing(paginateQuery(query, firstResult, maxResults).getResultStream()).map(userEntity -> users.getUserById(realm, userEntity.getId()));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserAttributeEntity(org.keycloak.models.jpa.entities.UserAttributeEntity) UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity) Set(java.util.Set) HashSet(java.util.HashSet) Root(javax.persistence.criteria.Root) ArrayList(java.util.ArrayList) Subquery(javax.persistence.criteria.Subquery) UserEntity(org.keycloak.models.jpa.entities.UserEntity) Predicate(javax.persistence.criteria.Predicate) UserProvider(org.keycloak.models.UserProvider) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with UserEntity

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

the class JpaUserCredentialStore method getStoredCredentialEntities.

private Stream<CredentialEntity> getStoredCredentialEntities(RealmModel realm, UserModel user) {
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    TypedQuery<CredentialEntity> query = em.createNamedQuery("credentialByUser", CredentialEntity.class).setParameter("user", userEntity);
    return closing(query.getResultStream());
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Aggregations

UserEntity (org.keycloak.models.jpa.entities.UserEntity)16 ArrayList (java.util.ArrayList)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 CredentialEntity (org.keycloak.models.jpa.entities.CredentialEntity)4 FederatedIdentityEntity (org.keycloak.models.jpa.entities.FederatedIdentityEntity)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UserGroupMembershipEntity (org.keycloak.models.jpa.entities.UserGroupMembershipEntity)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Root (javax.persistence.criteria.Root)1 Subquery (javax.persistence.criteria.Subquery)1 FederatedIdentityModel (org.keycloak.models.FederatedIdentityModel)1 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)1 ModelException (org.keycloak.models.ModelException)1 RequiredActionProviderModel (org.keycloak.models.RequiredActionProviderModel)1 UserProvider (org.keycloak.models.UserProvider)1 UserAttributeEntity (org.keycloak.models.jpa.entities.UserAttributeEntity)1