Search in sources :

Example 11 with UserEntity

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

the class JpaUserCredentialStore method createCredentialEntity.

CredentialEntity createCredentialEntity(RealmModel realm, UserModel user, CredentialModel cred) {
    CredentialEntity entity = new CredentialEntity();
    String id = cred.getId() == null ? KeycloakModelUtils.generateId() : cred.getId();
    entity.setId(id);
    entity.setCreatedDate(cred.getCreatedDate());
    entity.setUserLabel(cred.getUserLabel());
    entity.setType(cred.getType());
    entity.setSecretData(cred.getSecretData());
    entity.setCredentialData(cred.getCredentialData());
    UserEntity userRef = em.getReference(UserEntity.class, user.getId());
    entity.setUser(userRef);
    // add in linkedlist to last position
    List<CredentialEntity> credentials = getStoredCredentialEntities(realm, user).collect(Collectors.toList());
    int priority = credentials.isEmpty() ? PRIORITY_DIFFERENCE : credentials.get(credentials.size() - 1).getPriority() + PRIORITY_DIFFERENCE;
    entity.setPriority(priority);
    em.persist(entity);
    return entity;
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 12 with UserEntity

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

the class JpaUserProvider method getUsersCount.

@Override
public int getUsersCount(RealmModel realm, String search) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> queryBuilder = builder.createQuery(Long.class);
    Root<UserEntity> root = queryBuilder.from(UserEntity.class);
    queryBuilder.select(builder.count(root));
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(root.get("realmId"), realm.getId()));
    predicates.add(builder.or(getSearchOptionPredicateArray(search, builder, root)));
    queryBuilder.where(predicates.toArray(new Predicate[0]));
    return em.createQuery(queryBuilder).getSingleResult().intValue();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) UserEntity(org.keycloak.models.jpa.entities.UserEntity) Predicate(javax.persistence.criteria.Predicate)

Example 13 with UserEntity

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

the class JpaUserProvider method ensureEmailConstraint.

// Could override this to provide a custom behavior.
protected void ensureEmailConstraint(List<UserEntity> users, RealmModel realm) {
    UserEntity user = users.get(0);
    if (users.size() > 1) {
        // but duplicates haven't been removed.
        throw new ModelDuplicateException("Multiple users with email '" + user.getEmail() + "' exist in Keycloak.");
    }
    if (realm.isDuplicateEmailsAllowed()) {
        return;
    }
    if (user.getEmail() != null && !user.getEmail().equals(user.getEmailConstraint())) {
        // Realm settings have been changed from allowing duplicate emails to not allowing them.
        // We need to update the email constraint to reflect this change in the user entities.
        user.setEmailConstraint(user.getEmail());
        em.persist(user);
    }
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 14 with UserEntity

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

the class JpaUserProvider method userInEntityManagerContext.

private UserEntity userInEntityManagerContext(String id) {
    UserEntity user = em.getReference(UserEntity.class, id);
    boolean isLoaded = em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(user);
    return isLoaded ? user : null;
}
Also used : UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 15 with UserEntity

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

the class JpaUserProvider method addFederatedIdentity.

@Override
public void addFederatedIdentity(RealmModel realm, UserModel user, FederatedIdentityModel identity) {
    FederatedIdentityEntity entity = new FederatedIdentityEntity();
    entity.setRealmId(realm.getId());
    entity.setIdentityProvider(identity.getIdentityProvider());
    entity.setUserId(identity.getUserId());
    entity.setUserName(identity.getUserName().toLowerCase());
    entity.setToken(identity.getToken());
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    entity.setUser(userEntity);
    em.persist(entity);
    em.flush();
}
Also used : UserEntity(org.keycloak.models.jpa.entities.UserEntity) FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

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