Skip to content

Commit

Permalink
get totalResults by user count
Browse files Browse the repository at this point in the history
  • Loading branch information
asha15 committed Feb 20, 2024
1 parent 998419e commit ab32527
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,13 @@ private UsersGetResponse filterUsersBySingleAttribute(ExpressionNode node, Map<S
maxLimit = Math.max(maxLimit, limit);
}
// Get total users based on the filter query without depending on pagination params.
totalResults += filterUsers(node, 1, maxLimit, sortBy, sortOrder, domainName).size();
if(SCIMCommonUtils.isRetrieveTotalResultsByUserCountEnabled()) {
//Get the total user count by the filter query.
totalResults += getUserCountByAttribute(node, 1, maxLimit, sortBy, sortOrder, domainName);
} else {
totalResults += filterUsers(node, 1, maxLimit, sortBy, sortOrder, domainName).size();
}

} else {
totalResults += users.size();
}
Expand All @@ -1451,6 +1457,29 @@ private UsersGetResponse filterUsersBySingleAttribute(ExpressionNode node, Map<S
return getDetailedUsers(filteredUsers, totalResults);
}

/**
* method to get user count by filtering parameter.
*
* @param node Expression node for single attribute filtering
* @param offset Starting index of the count
* @param limit Counting value
* @param sortBy SortBy
* @param sortOrder Sorting order
* @param domainName Domain to run the filter
* @return User count
* @throws BadRequestException
* @throws CharonException
*/
private int getUserCountByAttribute(Node node, int offset, int limit, String sortBy,
String sortOrder, String domainName) throws BadRequestException, CharonException {

if (SCIMConstants.UserSchemaConstants.GROUP_URI.equals(((ExpressionNode) node).getAttributeValue())) {
getUserCountByGroup(node, domainName);
}

return filterUsers(node, 1, limit, sortBy, sortOrder, domainName).size();
}

/**
* Method to check whether the all configured user stores are JDBC.
*
Expand Down Expand Up @@ -1688,6 +1717,54 @@ private Set<org.wso2.carbon.user.core.common.User> filterUsers(Node node, int of
}
}

/**
* Method to get User Count by Group filter
*
* @param node Expression or Operation node.
* @param domainName Domain name.
* @return User count for the filtered group.
* @throws CharonException
* @throws BadRequestException
*/
private int getUserCountByGroup(Node node, String domainName)
throws CharonException, BadRequestException {

int count = 0;
// Set filter values.
String attributeName = ((ExpressionNode) node).getAttributeValue();
String filterOperation = ((ExpressionNode) node).getOperation();
String attributeValue = ((ExpressionNode) node).getValue();

/*
If there is a domain and if the domain separator is not found in the attribute value, append the domain
with the domain separator in front of the new attribute value.
*/
attributeValue = UserCoreUtil.addDomainToName(((ExpressionNode) node).getValue(), domainName);

try {
List<String> roleNames = getRoleNames(attributeName, filterOperation, attributeValue);
count = getUserCountForRole(roleNames);
return count;
} catch (UserStoreException e) {
String errorMessage = String.format("Error while filtering the users for filter with attribute name: "
+ "%s, filter operation: %s and attribute value: %s.", attributeName, filterOperation,
attributeValue);
throw resolveError(e, errorMessage);
}
}

private int getUserCountForRole(List<String> roleNames) throws
org.wso2.carbon.user.core.UserStoreException {

int count = 0;
if (roleNames != null) {
for (String roleName : roleNames) {
count += carbonUM.getUserCountForRole(roleName);
}
}
return count;
}

/**
* Method to perform a multiple domain search when the domain is not specified in the request. The same function
* can be used to listing users by passing a condition for conditionForListingUsers parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public class SCIMCommonConstants {
"SCIM2.ConsiderMaxLimitForTotalResult";
public static final String SCIM_ENABLE_CONSIDER_TOTAL_RECORDS_FOR_TOTAL_RESULT_OF_LDAP =
"SCIM2.ConsiderTotalRecordsForTotalResultOfLDAP";
public static final String SCIM_ENABLE_RETRIEVE_TOTAL_RESULTS_BY_User_Count =
"SCIM2.EnableRetrieveTotalResultsByUserCount";
public static final String SCIM_ENABLE_MANDATE_DOMAIN_FOR_GROUPNAMES_IN_GROUPS_RESPONSE =
"SCIM2.MandateDomainForGroupNamesInGroupsResponse";
public static final String SCIM_ENABLE_MANDATE_DOMAIN_FOR_USERNAMES_AND_GROUPNAMES_IN_RESPONSE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,17 @@ public static boolean isEnterpriseUserExtensionEnabled() {
.getProperty(SCIMCommonConstants.ENTERPRISE_USER_EXTENSION_ENABLED));
}

/**
* Checks whether the identity.xml config is available to retrieve totalResults by user count.
*
* @return whether 'RetrieveTotalResultsByUserCount' property is enabled in identity.xml.
*/
public static boolean isRetrieveTotalResultsByUserCountEnabled() {

return Boolean.parseBoolean(IdentityUtil.getProperty(
SCIMCommonConstants.SCIM_ENABLE_RETRIEVE_TOTAL_RESULTS_BY_User_Count));
}

/**
* Checks whether the identity.xml config is available to notify userstore availability.
*
Expand Down

0 comments on commit ab32527

Please sign in to comment.