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();
}
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());
}
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;
}
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;
}
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;
}
Aggregations