[KEYCLOAK-5660] - Adding UserQueryProvider.getUsersCount(realm, includeServiceAccount) method
This commit is contained in:
parent
4caa40fd0a
commit
17748d5ba8
7 changed files with 49 additions and 7 deletions
|
@ -597,9 +597,14 @@ public class UserCacheSession implements UserCache {
|
|||
return getDelegate().getUsers(realm, includeServiceAccounts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||
return getDelegate().getUsersCount(realm, includeServiceAccount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUsersCount(RealmModel realm) {
|
||||
return getDelegate().getUsersCount(realm);
|
||||
return getUsersCount(realm, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -596,13 +596,24 @@ public class JpaUserProvider implements UserProvider, UserCredentialStore {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getUsersCount(RealmModel realm) {
|
||||
Object count = em.createNamedQuery("getRealmUserCount")
|
||||
public int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||
String namedQuery = "getRealmUserCountExcludeServiceAccount";
|
||||
|
||||
if (includeServiceAccount) {
|
||||
namedQuery = "getRealmUserCount";
|
||||
}
|
||||
|
||||
Object count = em.createNamedQuery(namedQuery)
|
||||
.setParameter("realmId", realm.getId())
|
||||
.getSingleResult();
|
||||
return ((Number)count).intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUsersCount(RealmModel realm) {
|
||||
return getUsersCount(realm, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserModel> getUsers(RealmModel realm) {
|
||||
return getUsers(realm, false);
|
||||
|
|
|
@ -48,7 +48,8 @@ import java.util.Collection;
|
|||
@NamedQuery(name="getRealmUserByLastName", query="select u from UserEntity u where u.lastName = :lastName and u.realmId = :realmId"),
|
||||
@NamedQuery(name="getRealmUserByFirstLastName", query="select u from UserEntity u where u.firstName = :first and u.lastName = :last and u.realmId = :realmId"),
|
||||
@NamedQuery(name="getRealmUserByServiceAccount", query="select u from UserEntity u where u.serviceAccountClientLink = :clientInternalId and u.realmId = :realmId"),
|
||||
@NamedQuery(name="getRealmUserCount", query="select count(u) from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null)"),
|
||||
@NamedQuery(name="getRealmUserCount", query="select count(u) from UserEntity u where u.realmId = :realmId"),
|
||||
@NamedQuery(name="getRealmUserCountExcludeServiceAccount", query="select count(u) from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null)"),
|
||||
@NamedQuery(name="getRealmUsersByAttributeNameAndValue", query="select u from UserEntity u join u.attributes attr " +
|
||||
"where u.realmId = :realmId and attr.name = :name and attr.value = :value"),
|
||||
@NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId"),
|
||||
|
|
|
@ -35,8 +35,25 @@ import java.util.Map;
|
|||
*/
|
||||
public interface UserQueryProvider {
|
||||
|
||||
/**
|
||||
* Returns the number of users, without consider any service account.
|
||||
*
|
||||
* @param realm the realm
|
||||
* @return the number of users
|
||||
*/
|
||||
int getUsersCount(RealmModel realm);
|
||||
|
||||
/**
|
||||
* Returns the number of users.
|
||||
*
|
||||
* @param realm the realm
|
||||
* @param includeServiceAccount if true, the number of users will also include service accounts. Otherwise, only the number of users.
|
||||
* @return the number of users
|
||||
*/
|
||||
default int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||
throw new RuntimeException("Not implemented");
|
||||
}
|
||||
|
||||
List<UserModel> getUsers(RealmModel realm);
|
||||
List<UserModel> getUsers(RealmModel realm, int firstResult, int maxResults);
|
||||
|
||||
|
|
|
@ -81,7 +81,7 @@ public abstract class MultipleStepsExportProvider implements ExportProvider {
|
|||
|
||||
// Count total number of users
|
||||
if (!exportUsersIntoRealmFile) {
|
||||
usersHolder.totalCount = session.users().getUsersCount(realm);
|
||||
usersHolder.totalCount = session.users().getUsersCount(realm, true);
|
||||
federatedUsersHolder.totalCount = session.userFederatedStorage().getStoredUsersCount(realm);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -399,14 +399,19 @@ public class UserStorageManager implements UserProvider, OnUserCache, OnCreateCo
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getUsersCount(RealmModel realm) {
|
||||
int size = localStorage().getUsersCount(realm);
|
||||
public int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||
int size = localStorage().getUsersCount(realm, includeServiceAccount);
|
||||
for (UserQueryProvider provider : getStorageProviders(session, realm, UserQueryProvider.class)) {
|
||||
size += provider.getUsersCount(realm);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUsersCount(RealmModel realm) {
|
||||
return getUsersCount(realm, false);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface PaginatedQuery {
|
||||
List<UserModel> query(Object provider, int first, int max);
|
||||
|
|
|
@ -357,6 +357,9 @@ public class UserModelTest extends AbstractModelTest {
|
|||
Assert.assertTrue(users.contains(user1));
|
||||
Assert.assertTrue(users.contains(user2));
|
||||
|
||||
Assert.assertEquals(2, session.users().getUsersCount(realm, true));
|
||||
Assert.assertEquals(1, session.users().getUsersCount(realm, false));
|
||||
|
||||
// Remove client
|
||||
new ClientManager(realmManager).removeClient(realm, client);
|
||||
commit();
|
||||
|
|
Loading…
Reference in a new issue