diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java index 689a87363ab..cc4d5f36c2a 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java @@ -9481,6 +9481,23 @@ protected List 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. + * + * @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. * @@ -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; + } } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCRealmConstants.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCRealmConstants.java index a8d3f499e39..5f32db0413d 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCRealmConstants.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCRealmConstants.java @@ -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"; @@ -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"; @@ -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 " diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java index 0db943fe8a6..5c711c61ba5 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java @@ -351,6 +351,14 @@ public List 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 getUserListOfJDBCRoleWithID(RoleContext ctx, String filter) throws UserStoreException { return getUserListOfJDBCRoleWithID(ctx, filter, QUERY_MAX_ITEM_LIMIT_ANY); @@ -417,6 +425,94 @@ public List 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 { diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/JDBCRealmUtil.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/JDBCRealmUtil.java index 01e2d9395d8..7603468c133 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/JDBCRealmUtil.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/JDBCRealmUtil.java @@ -535,6 +535,14 @@ public static Map getSQL(Map 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);