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

Improve getUserCountByRole() Method #3951

Merged
merged 13 commits into from
Apr 5, 2024
Merged
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
*/
Expand Down Expand Up @@ -158,6 +158,25 @@ AuthenticationResult authenticateWithID(List<LoginIdentifier> loginIdentifiers,
*/
List<User> 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.
*/
List<User> getUserListOfGroupWithID(String groupName) throws UserStoreException;
dhaura marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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.
dhaura marked this conversation as resolved.
Show resolved Hide resolved
*/
List<User> getUserListOfGroupWithID(String groupName, String filter, int maxItemLimit) throws UserStoreException;
dhaura marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get user claim value in the profile.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -9479,14 +9479,13 @@ protected List<User> 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 {
dhaura marked this conversation as resolved.
Show resolved Hide resolved

if (log.isDebugEnabled()) {
log.debug("doGetUserCountOfRole operation is not implemented in: " + this.getClass());
Expand All @@ -9495,6 +9494,22 @@ protected int doGetUserCountOfRole(String roleName, String filter) throws UserSt
"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(
"doGetUserCountOfRoleWithID 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 @@ -12721,6 +12736,74 @@ public final List<User> getUserListOfRoleWithID(String roleName, String filter,
return users;
}

@Override
public final List<User> getUserListOfGroupWithID(String groupName) throws UserStoreException {

if (!isSecureCall.get()) {
Class argTypes[] = new Class[] { String.class };
Object object = callSecure("getUserListOfGroupWithID", new Object[] { groupName }, argTypes);
return (List<User>) object;
}

return getUserListOfGroupWithID(groupName, QUERY_FILTER_STRING_ANY, QUERY_MAX_ITEM_LIMIT_ANY);
}

@Override
public final List<User> 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<User>) object;
}

List<User> 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());
}
dhaura marked this conversation as resolved.
Show resolved Hide resolved
}

// #################### 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 {
Expand Down Expand Up @@ -19198,33 +19281,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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 " +
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

/**
*
*/
Expand Down Expand Up @@ -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 {
dhaura marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}

/**
*
*/
Expand Down
Loading