[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);
|
return getDelegate().getUsers(realm, includeServiceAccounts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||||
|
return getDelegate().getUsersCount(realm, includeServiceAccount);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getUsersCount(RealmModel realm) {
|
public int getUsersCount(RealmModel realm) {
|
||||||
return getDelegate().getUsersCount(realm);
|
return getUsersCount(realm, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -596,13 +596,24 @@ public class JpaUserProvider implements UserProvider, UserCredentialStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getUsersCount(RealmModel realm) {
|
public int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||||
Object count = em.createNamedQuery("getRealmUserCount")
|
String namedQuery = "getRealmUserCountExcludeServiceAccount";
|
||||||
|
|
||||||
|
if (includeServiceAccount) {
|
||||||
|
namedQuery = "getRealmUserCount";
|
||||||
|
}
|
||||||
|
|
||||||
|
Object count = em.createNamedQuery(namedQuery)
|
||||||
.setParameter("realmId", realm.getId())
|
.setParameter("realmId", realm.getId())
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
return ((Number)count).intValue();
|
return ((Number)count).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getUsersCount(RealmModel realm) {
|
||||||
|
return getUsersCount(realm, false);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<UserModel> getUsers(RealmModel realm) {
|
public List<UserModel> getUsers(RealmModel realm) {
|
||||||
return getUsers(realm, false);
|
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="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="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="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 " +
|
@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"),
|
"where u.realmId = :realmId and attr.name = :name and attr.value = :value"),
|
||||||
@NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId"),
|
@NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId"),
|
||||||
|
|
|
@ -35,8 +35,25 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public interface UserQueryProvider {
|
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);
|
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);
|
||||||
List<UserModel> getUsers(RealmModel realm, int firstResult, int maxResults);
|
List<UserModel> getUsers(RealmModel realm, int firstResult, int maxResults);
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ public abstract class MultipleStepsExportProvider implements ExportProvider {
|
||||||
|
|
||||||
// Count total number of users
|
// Count total number of users
|
||||||
if (!exportUsersIntoRealmFile) {
|
if (!exportUsersIntoRealmFile) {
|
||||||
usersHolder.totalCount = session.users().getUsersCount(realm);
|
usersHolder.totalCount = session.users().getUsersCount(realm, true);
|
||||||
federatedUsersHolder.totalCount = session.userFederatedStorage().getStoredUsersCount(realm);
|
federatedUsersHolder.totalCount = session.userFederatedStorage().getStoredUsersCount(realm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -399,14 +399,19 @@ public class UserStorageManager implements UserProvider, OnUserCache, OnCreateCo
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getUsersCount(RealmModel realm) {
|
public int getUsersCount(RealmModel realm, boolean includeServiceAccount) {
|
||||||
int size = localStorage().getUsersCount(realm);
|
int size = localStorage().getUsersCount(realm, includeServiceAccount);
|
||||||
for (UserQueryProvider provider : getStorageProviders(session, realm, UserQueryProvider.class)) {
|
for (UserQueryProvider provider : getStorageProviders(session, realm, UserQueryProvider.class)) {
|
||||||
size += provider.getUsersCount(realm);
|
size += provider.getUsersCount(realm);
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getUsersCount(RealmModel realm) {
|
||||||
|
return getUsersCount(realm, false);
|
||||||
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface PaginatedQuery {
|
interface PaginatedQuery {
|
||||||
List<UserModel> query(Object provider, int first, int max);
|
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(user1));
|
||||||
Assert.assertTrue(users.contains(user2));
|
Assert.assertTrue(users.contains(user2));
|
||||||
|
|
||||||
|
Assert.assertEquals(2, session.users().getUsersCount(realm, true));
|
||||||
|
Assert.assertEquals(1, session.users().getUsersCount(realm, false));
|
||||||
|
|
||||||
// Remove client
|
// Remove client
|
||||||
new ClientManager(realmManager).removeClient(realm, client);
|
new ClientManager(realmManager).removeClient(realm, client);
|
||||||
commit();
|
commit();
|
||||||
|
|
Loading…
Reference in a new issue