diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/UniqueIDUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/UniqueIDUserStoreManager.java index 6a5afcdca21..623af706e3e 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/UniqueIDUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/UniqueIDUserStoreManager.java @@ -1,7 +1,7 @@ /* - * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2019-2024, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, + * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at @@ -11,7 +11,7 @@ * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -158,6 +158,33 @@ AuthenticationResult authenticateWithID(List loginIdentifiers, */ List getUserListOfRoleWithID(String roleName, String filter, int maxItemLimit) throws UserStoreException; + /** + * Get user list of group. + * + * @param groupName Name of the group. + * @return An array of users that belongs to the given group. + * @throws UserStoreException Thrown by the underlying UserStoreManager. + */ + default List getUserListOfGroupWithID(String groupName) throws UserStoreException { + + throw new NotImplementedException( + "getUserListOfGroupWithID operation is not implemented in: " + this.getClass()); + } + + /** + * Get user list of group. + * + * @param groupName Name of the group. + * @return An array of users that belongs to the given group. + * @throws UserStoreException Thrown by the underlying UserStoreManager. + */ + default List getUserListOfGroupWithID(String groupName, String filter, int maxItemLimit) + throws UserStoreException { + + throw new NotImplementedException( + "getUserListOfGroupWithID operation is not implemented in: " + this.getClass()); + } + /** * Get user claim value in the profile. * 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 a5c3ddecd93..b788f3505af 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 @@ -1,12 +1,12 @@ /* - * Copyright (c) (2005-2021), WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2005-2024, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -9479,20 +9479,34 @@ protected List doGetUserListOfRoleWithID(String roleName, String filter) t } /** - * Return the count of users belong to the given role for the given filter. + * Return the count of users belong to the given role for the given filter when unique id feature is not enabled. * * @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 { + protected int doGetUserCountOfRole(String roleName) 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 count of users belong to the given role. + * + * @param roleName Name of the role. + * @return User count for the given role. + * @throws UserStoreException Thrown by the underlying UserStoreManager. + */ + protected int doGetUserCountOfRoleWithID(String roleName) throws UserStoreException { + + if (log.isDebugEnabled()) { + log.debug("doGetUserCountOfRoleWithID operation is not implemented in: " + this.getClass()); + } throw new NotImplementedException( - "doGetUserCountOfRole operation is not implemented in: " + this.getClass()); + "doGetUserCountOfRoleWithID operation is not implemented in: " + this.getClass()); } /** @@ -12721,6 +12735,74 @@ public final List getUserListOfRoleWithID(String roleName, String filter, return users; } + @Override + public final List getUserListOfGroupWithID(String groupName) throws UserStoreException { + + if (!isSecureCall.get()) { + Class argTypes[] = new Class[]{String.class}; + Object object = callSecure("getUserListOfGroupWithID", new Object[]{groupName}, argTypes); + return (List) object; + } + + return getUserListOfGroupWithID(groupName, QUERY_FILTER_STRING_ANY, QUERY_MAX_ITEM_LIMIT_ANY); + } + + @Override + public final List getUserListOfGroupWithID(String groupName, String filter, int maxItemLimit) + throws UserStoreException { + + if (!isSecureCall.get()) { + Class argTypes[] = new Class[]{String.class, String.class, int.class}; + Object object = callSecure("getUserListOfGroupWithID", new Object[]{groupName, filter, maxItemLimit}, + argTypes); + return (List) object; + } + + List users = new ArrayList<>(); + + // If group does not exit, just return + if (!isExistingRole(groupName)) { + handleDoPostGetUserListOfRoleWithID(groupName, users); + return users; + } + + UserStore userStore = getUserStoreOfRoles(groupName); + + if (userStore.isRecurssive()) { + UserStoreManager resolvedUserStoreManager = userStore.getUserStoreManager(); + if (resolvedUserStoreManager instanceof AbstractUserStoreManager) { + return ((AbstractUserStoreManager) resolvedUserStoreManager) + .getUserListOfGroupWithID(userStore.getDomainFreeName(), filter, maxItemLimit); + } else { + return ((UniqueIDUserStoreManager) resolvedUserStoreManager) + .getUserListOfGroupWithID(userStore.getDomainFreeName()); + } + } + + // #################### Domain Name Free Zone Starts Here ################################ + + if (userStore.isSystemStore() || userStore.isHybridRole()) { + // If the passed group is a role and if role, group separation is not enabled, + // call the user listing method for roles. + if (!isRoleAndGroupSeparationEnabled()) { + return getUserListOfRoleWithID(groupName, filter, maxItemLimit); + } + // If the passed group is a role and if role, group separation is enabled, just return. + return users; + } + + if (readGroupsEnabled) { + // If unique id feature is not enabled, we have to call the legacy methods. + if (!isUniqueUserIdEnabledInUserStore(userStore)) { + users = userUniqueIDManger.listUsers(doGetUserListOfRole(groupName, filter, maxItemLimit), this); + } else { + users = doGetUserListOfRoleWithID(groupName, filter, maxItemLimit); + } + handleDoPostGetUserListOfRoleWithID(groupName, users); + } + return users; + } + @Override public final String getUserClaimValueWithID(String userID, String claim, String profileName) throws UserStoreException { @@ -19198,33 +19280,56 @@ public UserUniqueIDDomainResolver getUserUniqueIDDomainResolver() { return userUniqueIDDomainResolver; } - public int getUserCountForRole(String roleName) throws UserStoreException { + /** + * Retrieves the user count that belongs to a given group. + * + * @param groupName Name of the group. + * @return User count of the given group. + * @throws UserStoreException If an unexpected error occurs while accessing user store. + */ + public int getUserCountForGroup(String groupName) throws UserStoreException { + int count = 0; if (!isSecureCall.get()) { - Class argTypes[] = new Class[] { String.class, String.class }; - Object object = callSecure("getUserCountByRole", new Object[] { roleName, QUERY_FILTER_STRING_ANY }, argTypes); + Class argTypes[] = new Class[]{String.class}; + Object object = callSecure("getUserCountForGroup", new Object[]{groupName}, argTypes); return (int) object; } - return getUserCountByRole(roleName, QUERY_FILTER_STRING_ANY); - } + // If group does not exit, just return. + if (!isExistingRole(groupName)) { + return count; + } - public int getUserCountByRole(String roleName, String filter) throws UserStoreException { + UserStore userStore = getUserStoreOfRoles(groupName); - 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 (userStore.isRecurssive()) { + UserStoreManager resolvedUserStoreManager = userStore.getUserStoreManager(); + if (resolvedUserStoreManager instanceof AbstractUserStoreManager) { + return ((AbstractUserStoreManager) resolvedUserStoreManager) + .getUserCountForGroup(userStore.getDomainFreeName()); + } } - // If role does not exit, just return. - if (!isExistingRole(roleName)) { + // #################### Domain Name Free Zone Starts Here ################################ + + if (userStore.isSystemStore() || userStore.isHybridRole()) { + // If the passed group is a role and if role, group separation is not enabled, + // call the user listing method for roles. + if (!isRoleAndGroupSeparationEnabled()) { + return getUserListOfRoleWithID(groupName).size(); + } + // If the passed group is a role and if role, group separation is enabled, just return. return count; } if (readGroupsEnabled) { - count += doGetUserCountOfRole(roleName, filter); + // If unique id feature is not enabled, we have to call the legacy methods. + if (!isUniqueUserIdEnabledInUserStore(userStore)) { + count += doGetUserCountOfRole(groupName); + } else { + count += doGetUserCountOfRoleWithID(groupName); + } } 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 5f32db0413d..c05a98ddbac 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 @@ -1,12 +1,12 @@ /* - * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2005-2024, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -57,6 +57,7 @@ public final class JDBCRealmConstants { 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_COUNT_WITH_FILTER_ROLE_WITH_ID = "GetUserCountWithFilterRoleWithIDSQL"; 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"; @@ -270,7 +271,11 @@ public final class JDBCRealmConstants { + "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 " + + public static final String GET_USERS_COUNT_WITH_FILTER_ROLE_SQL = "SELECT COUNT(UM_USER_NAME) FROM " + + "UM_USER_ROLE, UM_ROLE, UM_USER WHERE 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_WITH_ID = "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 " + diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java index 18b469c8f7b..9515e090b2f 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/JDBCUserStoreManager.java @@ -1,18 +1,19 @@ /* -z * Copyright 2005-2007 WSO2, Inc. (http://wso2.com) + * Copyright (c) 2005-2024, WSO2 LLC. (http://www.wso2.com). * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS"//also need to clear role authorization - userRealm.getAuthorizationManager().cle BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.user.core.jdbc; @@ -851,6 +852,13 @@ public String[] doGetUserListOfRole(String roleName, String filter) throws UserS return getUserListOfJDBCRole(roleContext, filter); } + @Override + public int doGetUserCountOfRole(String roleName) throws UserStoreException { + + RoleContext roleContext = createRoleContext(roleName); + return getUserCountByRole(roleContext); + } + /** * */ @@ -934,6 +942,33 @@ public String[] getUserListOfJDBCRole(RoleContext ctx, String filter, int maxIte return names; } + /** + * Return the count of users belong to the given role for the given {@link RoleContext}. + * + * @param ctx {@link RoleContext} corresponding to the role. + * @throws UserStoreException If an unexpected error occurs in user store. + */ + public int getUserCountByRole(RoleContext ctx) throws UserStoreException { + + String roleName = ctx.getRoleName(); + Connection dbConnection = null; + String sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE); + try { + dbConnection = getDBConnection(); + return DatabaseUtil.getIntegerValueFromDatabase( + dbConnection, sqlStmt, roleName, tenantId, tenantId, tenantId); + } 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); + } + } + /** * */ 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 16a26a6e0bf..f3155e6a394 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 @@ -1,7 +1,7 @@ /* - * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2019-2024, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, + * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at @@ -11,7 +11,7 @@ * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -352,11 +352,17 @@ public List doGetUserListOfRoleWithID(String roleName, String filter) thro } @Override - public int doGetUserCountOfRole(String roleName, String filter) - throws UserStoreException { + public int doGetUserCountOfRole(String roleName) throws UserStoreException { - RoleContext roleContext = createRoleContext(roleName); - return getUserCountByRole(roleContext, filter); + throw new UserStoreException("Operation is not supported."); + } + + @Override + public int doGetUserCountOfRoleWithID(String roleName) + throws UserStoreException { + + RoleContext roleContext = createRoleContext(roleName); + return getUserCountByRole(roleContext); } public List getUserListOfJDBCRoleWithID(RoleContext ctx, String filter) throws UserStoreException { @@ -426,42 +432,20 @@ public List getUserListOfJDBCRoleWithID(RoleContext ctx, String filter, in } /** - * Return the count of users belong to the given role for the given {@link RoleContext} and filter. + * Return the count of users belong to the given role for the given {@link RoleContext}. * - * @param ctx {@link RoleContext} corresponding to the role. - * @param filter String filter for the users. + * @param ctx {@link RoleContext} corresponding to the role. + * @throws UserStoreException If an unexpected error occurs while accessing user store. */ - public int getUserCountByRole(RoleContext ctx, String filter) throws UserStoreException { + public int getUserCountByRole(RoleContext ctx) 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); + String roleId = getRoleIdByName(ctx.getRoleName(), tenantId); Connection dbConnection = null; PreparedStatement prepStmt = null; ResultSet rs = null; - int count = 0 ; - String sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE); + int count = 0; + String sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE_WITH_ID); try { dbConnection = getDBConnection(); prepStmt = dbConnection.prepareStatement(sqlStmt); 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 7603468c133..148aae7931a 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 @@ -1,17 +1,19 @@ /* - * Copyright 2009-2010 WSO2, Inc. (http://wso2.com) + * Copyright (c) 2009-2024, WSO2 LLC. (http://www.wso2.com). * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.user.core.util; @@ -536,7 +538,13 @@ public static Map getSQL(Map properties) { } 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); + properties.put(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE, + JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE_SQL); + } + + if (!properties.containsKey(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE_WITH_ID)) { + properties.put(JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE_WITH_ID, + JDBCRealmConstants.GET_USERS_COUNT_WITH_FILTER_ROLE_SQL_WITH_ID); } if (!properties.containsKey(JDBCRealmConstants.GET_ROLE_ID_BY_NAME)) { diff --git a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmPrimaryUserStoreTest.java b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmPrimaryUserStoreTest.java index 5803bb9356d..9a0d608769e 100644 --- a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmPrimaryUserStoreTest.java +++ b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmPrimaryUserStoreTest.java @@ -644,4 +644,45 @@ public void test203DeleteUserWithID() throws UserStoreException { assertFalse(admin.getUserListOfRoleWithID(role).stream().map(User::getUserID).collect(Collectors.toList()).contains(userId1)); } } + + public void test204GetUserListOfGroupWithID() throws UserStoreException { + + // Add new groups + admin.addRoleWithID("userListTestGroup1", null, null, false); + admin.addRoleWithID("userListTestGroup2", null, null, false); + admin.addRoleWithID("userListTestGroup3", null, null, false); + + // Add 10 users for "userListTestGroup1" group + for (int i = 1; i <= 10; i++) { + User user = admin.addUserWithID("testUser1WithID" + i, "pass1", + new String[]{"userListTestGroup1"}, null, null); + assertNotNull(user); + } + + // Add 20 users for "userListTestGroup2" group + for (int i = 1; i <= 20; i++) { + User user = admin.addUserWithID("testUser2WithID" + i, "pass1", + new String[]{"userListTestGroup2"}, null, null); + assertNotNull(user); + } + + // getUserListOfGroup() method should return users of the given group + assertEquals(10, admin.getUserListOfGroupWithID("userListTestGroup1").size()); + assertEquals(20, admin.getUserListOfGroupWithID("userListTestGroup2").size()); + } + + public void test205GetUserCountForGroup() throws UserStoreException { + + // Add a new group + admin.addRole("userCountTestGroup", null, null); + + // Add users more than max users per page (100) + for (int i = 1; i <= 150; i++) { + admin.addUser("testUser" + i, "pass1", new String[]{"userCountTestGroup"}, + null, null, false); + } + + // getUserCountForGroup() method should return the total number of users of the given group + assertEquals(150, admin.getUserCountForGroup("userCountTestGroup")); + } } diff --git a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmSecondaryUserStoreTest.java b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmSecondaryUserStoreTest.java index 51bbdc5a2c6..a9b0ebe003f 100644 --- a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmSecondaryUserStoreTest.java +++ b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/JDBCRealmSecondaryUserStoreTest.java @@ -436,6 +436,47 @@ public void test183AuthenticateWithID() throws UserStoreException { "pass2").getAuthenticationStatus()); } + public void test184GetUserListOfGroupWithID() throws UserStoreException { + + // Add new groups + admin.addRoleWithID("SECONDARYJDBC/userListTestGroup1", null, null, false); + admin.addRoleWithID("SECONDARYJDBC/userListTestGroup2", null, null, false); + admin.addRoleWithID("SECONDARYJDBC/userListTestGroup3", null, null, false); + + // Add 10 users for "userListTestGroup1" group + for (int i = 1; i <= 10; i++) { + User user = admin.addUserWithID("SECONDARYJDBC/testUser1WithID" + i, "pass1", + new String[]{"SECONDARYJDBC/userListTestGroup1"}, null, null); + assertNotNull(user); + } + + // Add 20 users for "userListTestGroup2" group + for (int i = 1; i <= 20; i++) { + User user = admin.addUserWithID("SECONDARYJDBC/testUser2WithID" + i, "pass1", + new String[]{"SECONDARYJDBC/userListTestGroup2"}, null, null); + assertNotNull(user); + } + + // getUserListOfGroup() method should return users of the given group + assertEquals(10, admin.getUserListOfGroupWithID("SECONDARYJDBC/userListTestGroup1").size()); + assertEquals(20, admin.getUserListOfGroupWithID("SECONDARYJDBC/userListTestGroup2").size()); + } + + public void test185GetUserCountForGroup() throws UserStoreException { + + // Add a new group + admin.addRole("SECONDARYJDBC/userCountTestGroup", null, null); + + // Add users more than max users per page (100) + for (int i = 1; i <= 150; i++) { + admin.addUser("SECONDARYJDBC/testUser" + i, "pass1", + new String[]{"SECONDARYJDBC/userCountTestGroup"}, null, null, false); + } + + // getUserCountForGroup() method should return the total number of users of the given group + assertEquals(150, admin.getUserCountForGroup("SECONDARYJDBC/userCountTestGroup")); + } + private void addSecondaryUserStoreManager(RealmConfiguration primaryRealm, AbstractUserStoreManager userStoreManager, UserRealm userRealm) throws Exception { diff --git a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmPrimaryUserStoreTest.java b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmPrimaryUserStoreTest.java index ed5ce499f14..c7338466b23 100644 --- a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmPrimaryUserStoreTest.java +++ b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmPrimaryUserStoreTest.java @@ -702,6 +702,47 @@ public void test205GetDisplayNameOfUser() throws UserStoreException { assertEquals("user6WithID$_USERNAME_SEPARATOR_$usergivenname2withId", username); } + public void test206GetUserListOfGroupWithID() throws UserStoreException { + + // Add new groups + admin.addRoleWithID("userListTestGroup1", null, null, false); + admin.addRoleWithID("userListTestGroup2", null, null, false); + admin.addRoleWithID("userListTestGroup3", null, null, false); + + // Add 10 users for "userListTestGroup1" group + for (int i = 1; i <= 10; i++) { + User user = admin.addUserWithID("testUser1WithID" + i, "pass1", + new String[]{"userListTestGroup1"}, null, null); + assertNotNull(user); + } + + // Add 20 users for "userListTestGroup2" group + for (int i = 1; i <= 20; i++) { + User user = admin.addUserWithID("testUser2WithID" + i, "pass1", + new String[]{"userListTestGroup2"}, null, null); + assertNotNull(user); + } + + // getUserListOfGroup() method should return users of the given group + assertEquals(10, admin.getUserListOfGroupWithID("userListTestGroup1").size()); + assertEquals(20, admin.getUserListOfGroupWithID("userListTestGroup2").size()); + } + + public void test207GetUserCountForGroup() throws UserStoreException { + + // Add a new group + admin.addRole("userCountTestGroup", null, null); + + // Add users more than max users per page (100) + for (int i = 1; i <= 150; i++) { + admin.addUser("testUser" + i, "pass1", new String[]{"userCountTestGroup"}, + null, null, false); + } + + // getUserCountForGroup() method should return the total number of users of the given group + assertEquals(150, admin.getUserCountForGroup("userCountTestGroup")); + } + private void clearUserIdResolverCache() { UserIdResolverCache.getInstance() diff --git a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmSecondaryUserStoreTest.java b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmSecondaryUserStoreTest.java index 851b139799d..8697ee61521 100644 --- a/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmSecondaryUserStoreTest.java +++ b/core/org.wso2.carbon.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCRealmSecondaryUserStoreTest.java @@ -727,6 +727,47 @@ public void test204GetDisplayNameOfUser() throws UserStoreException { assertEquals("SECONDARY/user6WithID$_USERNAME_SEPARATOR_$SECONDARY/usergivenname2withId", username); } + public void test205GetUserListOfGroupWithID() throws UserStoreException { + + // Add new groups + admin.addRoleWithID("SECONDARY/userListTestGroup1", null, null, false); + admin.addRoleWithID("SECONDARY/userListTestGroup2", null, null, false); + admin.addRoleWithID("SECONDARY/userListTestGroup3", null, null, false); + + // Add 10 users for "userListTestGroup1" group + for (int i = 1; i <= 10; i++) { + User user = admin.addUserWithID("SECONDARY/testUser1WithID" + i, "pass1", + new String[]{"SECONDARY/userListTestGroup1"}, null, null); + assertNotNull(user); + } + + // Add 20 users for "userListTestGroup2" group + for (int i = 1; i <= 20; i++) { + User user = admin.addUserWithID("SECONDARY/testUser2WithID" + i, "pass1", + new String[]{"SECONDARY/userListTestGroup2"}, null, null); + assertNotNull(user); + } + + // getUserListOfGroup() method should return users of the given group + assertEquals(10, admin.getUserListOfGroupWithID("SECONDARY/userListTestGroup1").size()); + assertEquals(20, admin.getUserListOfGroupWithID("SECONDARY/userListTestGroup2").size()); + } + + public void test206GetUserCountForGroup() throws UserStoreException { + + // Add a new group + admin.addRole("SECONDARY/userCountTestGroup", null, null); + + // Add users more than max users per page (100) + for (int i = 1; i <= 150; i++) { + admin.addUser("SECONDARY/testUser" + i, "pass1", + new String[]{"SECONDARY/userCountTestGroup"}, null, null, false); + } + + // getUserCountForGroup() method should return the total number of users of the given group + assertEquals(150, admin.getUserCountForGroup("SECONDARY/userCountTestGroup")); + } + private void addSecondaryUserStoreManager(RealmConfiguration primaryRealm, AbstractUserStoreManager userStoreManager, UserRealm userRealm, String dbUrl, String configFilePath,