Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get totalResults value by user count #3906

Merged
merged 7 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9481,6 +9481,23 @@ protected List<User> doGetUserListOfRoleWithID(String roleName, String filter) t
"doGetUserListOfRoleWithID operation is not implemented in: " + this.getClass());
}

/**
* Return the count of users belong to the given role for the given filter.
asha15 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param roleName role name.
* @param filter filter.
* @return user count for the given role.
* @throws UserStoreException Thrown by the underlying UserStoreManager.
*/
protected int doGetUserCountOfRole(String roleName, String filter) throws UserStoreException {

if (log.isDebugEnabled()) {
log.debug("doGetUserCountOfRole operation is not implemented in: " + this.getClass());
}
throw new NotImplementedException(
"doGetUserCountOfRole operation is not implemented in: " + this.getClass());
}

/**
* Return the list of users belong to the given role for the given filter.
*
Expand Down Expand Up @@ -19161,4 +19178,36 @@ public UserUniqueIDDomainResolver getUserUniqueIDDomainResolver() {

return userUniqueIDDomainResolver;
}

public int getUserCountForRole(String roleName) throws UserStoreException {

if (!isSecureCall.get()) {
Class argTypes[] = new Class[] { String.class, String.class };
Object object = callSecure("getUserCountByRole", new Object[] { roleName, QUERY_FILTER_STRING_ANY }, argTypes);
return (int) object;
}

return getUserCountByRole(roleName, QUERY_FILTER_STRING_ANY);
}

public int getUserCountByRole(String roleName, String filter) throws UserStoreException {

int count = 0;
if (!isSecureCall.get()) {
Class argTypes[] = new Class[] { String.class, String.class };
Object object = callSecure("getUserCountByRole", new Object[] { roleName, filter }, argTypes);
return (int) object;
}

// If role does not exit, just return.
if (!isExistingRole(roleName)) {
return count;
}

if (readGroupsEnabled) {
count += doGetUserCountOfRole(roleName, filter);
}

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class JDBCRealmConstants {
public static final String GET_ROLE_LIST_H2 = "GetRoleListSQLH2";
public static final String GET_ROLE_LIST_WITH_ESCAPE = "GetRoleListSQLWithEscape";
public static final String GET_ROLE_LIST_WITH_ESCAPE_H2 = "GetRoleListSQLWithEscapeH2";
public static final String GET_ROLE_ID_BY_NAME = "GetRoleIDByNameSQL";
public static final String GET_SHARED_ROLE_LIST = "GetSharedRoleListSQL";
public static final String GET_SHARED_ROLE_LIST_H2 = "GetSharedRoleListSQLH2";
public static final String GET_USER_FILTER = "UserFilterSQL";
Expand All @@ -55,6 +56,7 @@ public final class JDBCRealmConstants {
public static final String GET_USERS_IN_ROLE = "GetUserListOfRoleSQL";
public static final String GET_USERS_IN_ROLE_FILTER = "GetUserListOfRoleFilterSQL";
public static final String GET_USERS_IN_ROLE_FILTER_WITH_ID = "GetUserListOfRoleFilterWithIDSQL";
public static final String GET_USERS_COUNT_WITH_FILTER_ROLE = "GetUserCountWithFilterRoleSQL";
public static final String GET_USERS_IN_SHARED_ROLE = "GetUserListOfSharedRoleSQL";
public static final String GET_USERS_IN_SHARED_ROLE_FILTER = "GetUserListOfSharedRoleFilterSQL";
public static final String GET_USERS_IN_SHARED_ROLE_FILTER_WITH_ID = "GetUserListOfSharedRoleFilterWithIDSQL";
Expand Down Expand Up @@ -267,6 +269,13 @@ public final class JDBCRealmConstants {
+ ".UM_USER_NAME LIKE ? AND UM_ROLE.UM_ROLE_NAME=? AND UM_USER.UM_ID=UM_USER_ROLE.UM_USER_ID AND "
+ "UM_ROLE.UM_ID=UM_USER_ROLE.UM_ROLE_ID AND UM_USER_ROLE.UM_TENANT_ID=? AND UM_ROLE"
+ ".UM_TENANT_ID=? AND UM_USER.UM_TENANT_ID=?";

public static final String GET_USERS_COUNT_WITH_FILTER_ROLE_SQL = "SELECT count(UM_ID) FROM UM_USER_ROLE " +
"WHERE UM_ROLE_ID = ? AND UM_TENANT_ID = ?";

public static final String GET_ROLE_ID_BY_NAME_SQL = "SELECT UM_ID FROM UM_ROLE " +
"WHERE UM_ROLE_NAME = ? AND UM_TENANT_ID = ? ";

public static final String GET_USERS_IN_SHARED_ROLE_SQL =
"SELECT UM_USER_NAME FROM UM_SHARED_USER_ROLE INNER JOIN UM_USER ON "
+ "UM_SHARED_USER_ROLE.UM_USER_ID = UM_USER.UM_ID INNER JOIN UM_ROLE ON "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ public List<User> doGetUserListOfRoleWithID(String roleName, String filter) thro
return getUserListOfJDBCRoleWithID(roleContext, filter);
}

@Override
public int doGetUserCountOfRole(String roleName, String filter)
throws UserStoreException {

RoleContext roleContext = createRoleContext(roleName);
return getUserCountByRole(roleContext, filter);
}

public List<User> getUserListOfJDBCRoleWithID(RoleContext ctx, String filter) throws UserStoreException {

return getUserListOfJDBCRoleWithID(ctx, filter, QUERY_MAX_ITEM_LIMIT_ANY);
Expand Down Expand Up @@ -417,6 +425,94 @@ public List<User> getUserListOfJDBCRoleWithID(RoleContext ctx, String filter, in
return users;
}

/**
* Return the count of users belong to the given role for the given {@link RoleContext} and filter.
*
* @param ctx {@link RoleContext} corresponding to the role.
* @param filter String filter for the users.
*/
public int getUserCountByRole(RoleContext ctx, String filter) throws UserStoreException {

String roleName = ctx.getRoleName();

if (StringUtils.isNotEmpty(filter)) {
filter = filter.trim();
filter = filter.replace("*", "%");
filter = filter.replace("?", "_");
} else {
filter = "%";
}
return getUserCountByRoleFromDatabase(roleName, filter);
}

/**
* Return the count of users belong to the given role for the given {@link RoleContext} and filter.
*
* @param roleName Name of the role.
* @param filter String filter for the users.
* @return The count of users matching the provided constraints.
* @throws UserStoreException
*/
public int getUserCountByRoleFromDatabase(String roleName, String filter)
throws UserStoreException {

String roleId = getRoleIdByName(roleName, tenantId);
Connection dbConnection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
int count = 0 ;
String sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE);
try {
dbConnection = getDBConnection();
prepStmt = dbConnection.prepareStatement(sqlStmt);
prepStmt.setString(1, roleId);
prepStmt.setInt(2, tenantId);
rs = prepStmt.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
}
return count;
} catch (SQLException e) {
String errorMessage =
"Error occurred while getting the count of users in the role : " + roleName;
if (log.isDebugEnabled()) {
log.debug(errorMessage, e);
}
throw new UserStoreException(errorMessage, e);
} finally {
DatabaseUtil.closeAllConnections(dbConnection, rs, prepStmt);
}
}

private String getRoleIdByName(String roleName, int tenantId) throws UserStoreException {

String roleID = null;
Connection dbConnection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
String sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_ROLE_ID_BY_NAME);
try {
dbConnection = getDBConnection();
prepStmt = dbConnection.prepareStatement(sqlStmt);
prepStmt.setString(1, roleName);
prepStmt.setInt(2, tenantId);
rs = prepStmt.executeQuery();
while (rs.next()) {
roleID = rs.getString(1);
}
return roleID;
} catch (SQLException e) {
String errorMessage =
"Error occurred while getting the role id for the role : " + roleName;
if (log.isDebugEnabled()) {
log.debug(errorMessage, e);
}
throw new UserStoreException(errorMessage, e);
} finally {
DatabaseUtil.closeAllConnections(dbConnection, rs, prepStmt);
}
}

@Override
public String[] getProfileNames(String userName) throws UserStoreException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@ public static Map<String, String> getSQL(Map<String, String> properties) {
JDBCRealmConstants.GET_USERS_PROPS_FOR_PROFILE_WITH_ID_SQL);
}

if (!properties.containsKey(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE)) {
properties.put(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE, JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE_SQL);
}

if (!properties.containsKey(JDBCRealmConstants.GET_ROLE_ID_BY_NAME)) {
properties.put(JDBCRealmConstants.GET_ROLE_ID_BY_NAME, JDBCRealmConstants.GET_ROLE_ID_BY_NAME_SQL);
}

if (!properties.containsKey(JDBCCaseInsensitiveConstants.GET_USERS_PROPS_FOR_PROFILE_CASE_INSENSITIVE)) {
properties.put(JDBCCaseInsensitiveConstants.GET_USERS_PROPS_FOR_PROFILE_CASE_INSENSITIVE,
JDBCCaseInsensitiveConstants.GET_USERS_PROPS_FOR_PROFILE_SQL_CASE_INSENSITIVE);
Expand Down