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 #533

Closed
wants to merge 4 commits into from
Closed
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 @@ -1447,7 +1447,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 @@ -1458,6 +1464,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())) {
return 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 @@ -1695,6 +1724,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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
<cxf-bundle.version>3.3.7</cxf-bundle.version>
<inbound.auth.oauth.version>6.5.3</inbound.auth.oauth.version>
<commons-collections.version>3.2.0.wso2v1</commons-collections.version>
<carbon.kernel.version>4.10.2</carbon.kernel.version>
<carbon.kernel.version>4.10.10</carbon.kernel.version>
<identity.framework.version>7.0.89</identity.framework.version>
<junit.version>4.13.1</junit.version>
<commons.lang.version>20030203.000129</commons.lang.version>
Expand Down