Search in sources :

Example 1 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, Set<String> groupIds) {
    if (groupIds == null || groupIds.isEmpty()) {
        return 0;
    }
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> queryBuilder = builder.createQuery(Long.class);
    Root<UserGroupMembershipEntity> groupMembership = queryBuilder.from(UserGroupMembershipEntity.class);
    Join<UserGroupMembershipEntity, UserEntity> userJoin = groupMembership.join("user");
    queryBuilder.select(builder.count(userJoin));
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(userJoin.get("realmId"), realm.getId()));
    predicates.add(builder.or(getSearchOptionPredicateArray(search, builder, userJoin)));
    predicates.add(groupMembership.get("groupId").in(groupIds));
    queryBuilder.where(predicates.toArray(new Predicate[0]));
    return em.createQuery(queryBuilder).getSingleResult().intValue();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity) ArrayList(java.util.ArrayList) UserEntity(org.keycloak.models.jpa.entities.UserEntity) Predicate(javax.persistence.criteria.Predicate)

Example 2 with UserEntity

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

the class JpaUserProvider method getFederatedIdentitiesStream.

@Override
public Stream<FederatedIdentityModel> getFederatedIdentitiesStream(RealmModel realm, UserModel user) {
    TypedQuery<FederatedIdentityEntity> query = em.createNamedQuery("findFederatedIdentityByUser", FederatedIdentityEntity.class);
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    query.setParameter("user", userEntity);
    return closing(query.getResultStream().map(entity -> new FederatedIdentityModel(entity.getIdentityProvider(), entity.getUserId(), entity.getUserName(), entity.getToken())).distinct());
}
Also used : FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) UserEntity(org.keycloak.models.jpa.entities.UserEntity) FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Example 3 with UserEntity

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

the class JpaUserProvider method removeStoredCredential.

@Override
public boolean removeStoredCredential(RealmModel realm, UserModel user, String id) {
    CredentialEntity entity = credentialStore.removeCredentialEntity(realm, user, id);
    UserEntity userEntity = userInEntityManagerContext(user.getId());
    if (entity != null && userEntity != null) {
        userEntity.getCredentials().remove(entity);
    }
    return entity != null;
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 4 with UserEntity

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

the class JpaUserProvider method removeUser.

@Override
public boolean removeUser(RealmModel realm, UserModel user) {
    UserEntity userEntity = em.find(UserEntity.class, user.getId(), LockModeType.PESSIMISTIC_WRITE);
    if (userEntity == null)
        return false;
    removeUser(userEntity);
    return true;
}
Also used : UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 5 with UserEntity

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

the class JpaUserProvider method addUser.

@Override
public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) {
    if (id == null) {
        id = KeycloakModelUtils.generateId();
    }
    UserEntity entity = new UserEntity();
    entity.setId(id);
    entity.setCreatedTimestamp(System.currentTimeMillis());
    entity.setUsername(username.toLowerCase());
    entity.setRealmId(realm.getId());
    em.persist(entity);
    em.flush();
    UserAdapter userModel = new UserAdapter(session, realm, em, entity);
    if (addDefaultRoles) {
        userModel.grantRole(realm.getDefaultRole());
        // No need to check if user has group as it's new user
        realm.getDefaultGroupsStream().forEach(userModel::joinGroupImpl);
    }
    if (addDefaultRequiredActions) {
        realm.getRequiredActionProvidersStream().filter(RequiredActionProviderModel::isEnabled).filter(RequiredActionProviderModel::isDefaultAction).map(RequiredActionProviderModel::getAlias).forEach(userModel::addRequiredAction);
    }
    return userModel;
}
Also used : RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) 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