Skip to content

Commit

Permalink
Resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
sadilchamishka committed Nov 7, 2024
1 parent 69b133b commit eab796e
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ private UsersGetResponse filterUsersBySingleAttribute(ExpressionNode node, Map<S
// This is only implemented for JDBC userstores.
totalResults += getUserCountByAttribute(node, 1, maxLimit, sortBy, sortOrder, domainName);
} else {
totalResults += filterUsers(node, 1, maxLimit, sortBy, sortOrder, domainName).size();
totalResults += getFilteredUsersCount(node, 1, maxLimit, domainName);
}
} else {
totalResults += users.size();
Expand Down Expand Up @@ -1478,7 +1478,7 @@ private int getUserCountByAttribute(Node node, int offset, int limit, String sor
return getUserCountByGroup(node, domainName);
}

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

/**
Expand Down Expand Up @@ -1722,6 +1722,31 @@ private Set<org.wso2.carbon.user.core.common.User> filterUsers(Node node, int of
}
}

/**
* This method retrieves the count of users based on the filter query without depending on pagination params.
*
* @param node Filter condition tree.
* @param offset Starting index of the count.
* @param maxLimit The maximum number of users to be counted.
* @param domainName Domain that the filter should perform. If empty, filtering is applied to all available domains.
* @return The count of users that match the filtering conditions.
* @throws CharonException Error while filtering the users.
* @throws BadRequestException Domain miss match in domain parameter and attribute value.
*/
private int getFilteredUsersCount(Node node, int offset, int maxLimit, String domainName)
throws CharonException, BadRequestException {

if (SCIMConstants.UserSchemaConstants.GROUP_URI.equals(((ExpressionNode) node).getAttributeValue())) {
return filterUsersByGroup(node, offset, maxLimit, domainName).size();
}
if (StringUtils.isNotEmpty(domainName)) {
Condition condition = createConditionForSingleAttributeFilter(domainName, node);
return getFilteredUserCountFromSingleDomain(condition, offset, maxLimit, domainName);
} else {
return getFilteredUserCountFromMultipleDomains(node, offset, maxLimit, null);
}
}

/**
* Method to get User Count by Group filter
*
Expand Down Expand Up @@ -1850,6 +1875,58 @@ private Set<org.wso2.carbon.user.core.common.User> filterUsersFromMultipleDomain
return filteredUsernames;
}

/**
* Retrieves the count of users from multiple domains based on the specified filtering conditions, limit, and offset.
*
* @param node Expression or Operation node.
* @param offset The starting index for counting users.
* @param limit The maximum number of users to consider.
* @param conditionForListingUsers Condition for listing users when the function is used to list users
* except for filtering. For filtering this value should be set to NULL.
* @return The total count of users that match the filtering conditions across all specified domains.
* @throws CharonException Error while filtering the users.
* @throws BadRequestException Domain miss match in domain parameter and attribute value.
*/
private int getFilteredUserCountFromMultipleDomains(Node node, int offset, int limit,
Condition conditionForListingUsers)
throws CharonException, BadRequestException {

// Filter users when the domain is not set in the request. Then filter through multiple domains.
String[] userStoreDomainNames = getDomainNames();
int filteredUsersCount = 0;

Condition condition;
for (String userStoreDomainName : userStoreDomainNames) {

// Check for a user listing scenario. (For filtering this value will be set to NULL)
if (conditionForListingUsers == null) {

if (isLoginIdentifiersEnabled() && SCIMConstants.UserSchemaConstants.USER_NAME_URI
.equals(((ExpressionNode) node).getAttributeValue())) {
try {
((ExpressionNode) node).setAttributeValue(getScimUriForPrimaryLoginIdentifier(node));
} catch (org.wso2.carbon.user.core.UserStoreException e) {
throw new CharonException("Error in retrieving scim to local mappings.", e);
}
}
// Create filter condition for each domain for single attribute filter.
condition = createConditionForSingleAttributeFilter(userStoreDomainName, node);
} else {
condition = conditionForListingUsers;
}

// Filter users for given condition and domain.
try {
filteredUsersCount +=
getFilteredUserCountFromSingleDomain(condition, offset, limit, userStoreDomainName);
} catch (CharonException e) {
log.error("Error occurred while getting the filtered users count for domain: " + userStoreDomainName,
e);
}
}
return filteredUsersCount;
}

/**
* Method to update the count(limit) when iterating a filter across all domains.
*
Expand Down Expand Up @@ -1964,6 +2041,45 @@ private Set<org.wso2.carbon.user.core.common.User> filterUsernames(Condition con
}
}

/**
* Retrieves the count of users from a single domain based on the specified filtering condition, limit, and offset.
*
* @param condition The filtering condition to be applied.
* @param offset The starting index of the count.
* @param limit The maximum number of users to consider.
* @param domainName The domain in which to search for users.
* @return The count of users that match the filtering conditions within the specified domain.
* @throws CharonException Error while filtering the users.
* @throws BadRequestException Domain miss match in domain parameter and attribute value.
*/
private int getFilteredUserCountFromSingleDomain(Condition condition, int offset, int limit, String domainName)
throws CharonException, BadRequestException {

if (log.isDebugEnabled()) {
log.debug(String.format("Getting the filtered users count in domain : %s with limit: %d and offset: %d.",
domainName, limit, offset));
}
try {
return carbonUM.getUsersCount(condition, domainName, UserCoreConstants.DEFAULT_PROFILE, limit, offset,
removeDuplicateUsersInUsersResponseEnabled);
} catch (UserStoreException e) {
// Sometimes client exceptions are wrapped in the super class.
// Therefore checking for possible client exception.
Throwable rootCause = ExceptionUtils.getRootCause(e);
String errorMessage = String.format(
"Error while retrieving filtered users count for the domain: %s with limit: %d and offset: %d. %s",
domainName, limit, offset, rootCause != null ? rootCause.getMessage() : e.getMessage());
if (log.isDebugEnabled()) {
log.debug(errorMessage, e);
}
if (e instanceof UserStoreClientException || rootCause instanceof UserStoreClientException) {
throw new BadRequestException(errorMessage, ResponseCodeConstants.INVALID_VALUE);
} else {
throw new CharonException(errorMessage, e);
}
}
}

/**
* Method is to create a condition for a single attribute filter when the node and the domain name is passed.
*
Expand Down Expand Up @@ -2133,6 +2249,7 @@ private UsersGetResponse getMultiAttributeFilteredUsers(Node node, Map<String, B
// Get total users based on the filter query.
totalResults += getMultiAttributeFilteredUsersWithMaxLimit(node, 1, sortBy,
sortOrder, domainName, maxLimit).size();
totalResults += getMultiAttributeFilteredUsersCount(node, 1, domainName, maxLimit);
} else {
totalResults += filteredUsers.size();
if (totalResults == 0 && filteredUsers.size() > 1) {
Expand Down Expand Up @@ -2169,6 +2286,69 @@ private Set<org.wso2.carbon.user.core.common.User> getMultiAttributeFilteredUser
return users;
}
}

/**
* This method retrieves the count of users based on multi-attribute filtering.
*
* @param node Filter condition tree.
* @param offset Starting index of the count.
* @param domainName Domain that the filter should perform. If empty, filtering is applied to all available domains.
* @param maxLimit The maximum number of users to be counted.
* @return The count of users that match the filtering conditions.
* @throws CharonException Error while filtering the users.
* @throws BadRequestException Domain miss match in domain parameter and attribute value.
*/
private int getMultiAttributeFilteredUsersCount(Node node, int offset, String domainName, int maxLimit)
throws CharonException, BadRequestException {

int filteredUsersCount = 0;
if (StringUtils.isNotEmpty(domainName)) {
filteredUsersCount +=
getMultiAttributeFilteredUsersCountFromSingleDomain(node, offset, domainName, maxLimit);
} else {
AbstractUserStoreManager tempCarbonUM = carbonUM;
// If domain name are not given, then perform filtering on all available user stores.
while (tempCarbonUM != null && maxLimit > 0) {
// If carbonUM is not an instance of Abstract User Store Manger we can't get the domain name.
if (tempCarbonUM instanceof AbstractUserStoreManager) {
domainName =
tempCarbonUM.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.
PROPERTY_DOMAIN_NAME);
filteredUsersCount +=
getMultiAttributeFilteredUsersCountFromSingleDomain(node, offset, domainName, maxLimit);
}
// If secondary user store manager assigned to carbonUM then global variable carbonUM will contains the
// secondary user store manager.
tempCarbonUM = (AbstractUserStoreManager) tempCarbonUM.getSecondaryUserStoreManager();
}
}
return filteredUsersCount;
}

/**
* This method retrieves the count of users based on multi-attribute filtering.
*
* @param node Filter condition tree.
* @param offset Starting index of the count.
* @param domainName Domain that the filter should perform.
* @param limit The number of users to be counted.
* @return The count of users that match the filtering conditions.
* @throws CharonException Error while filtering the users.
* @throws BadRequestException Domain miss match in domain parameter and attribute value.
*/
private int getMultiAttributeFilteredUsersCountFromSingleDomain(Node node, int offset, String domainName, int limit)
throws CharonException, BadRequestException {

if (log.isDebugEnabled()) {
log.debug(String.format(
"Getting users count filtered by multi attributes in domain : %s with limit: %d and offset: %d.",
domainName, limit, offset));
}
Map<String, String> attributes = getAllAttributes(domainName);
Condition condition = getCondition(node, attributes);
return getFilteredUserCountFromSingleDomain(condition, offset, limit, domainName);
}

/**
* Get maximum user limit to retrieve.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,14 @@ public void testFilteringUsersWithGETWithPagination(List<org.wso2.carbon.user.co

when(mockedUserStoreManager.getUserListWithID(any(Condition.class), anyString(), anyString(), eq(Integer.MAX_VALUE),
anyInt(), nullable(String.class), nullable(String.class))).thenReturn(filteredUsersWithoutPagination);
when(mockedUserStoreManager.getUsersCount(any(Condition.class), anyString(), anyString(), eq(count),
anyInt(), anyBoolean())).thenReturn(filteredUsersWithPagination.size());

when(mockedUserStoreManager.getUsersCount(any(Condition.class), anyString(), anyString(), eq(configuredMaxLimit),
anyInt(), anyBoolean())).thenReturn(filteredUsersWithoutPagination.size());

when(mockedUserStoreManager.getUsersCount(any(Condition.class), anyString(), anyString(), eq(users.size()),
anyInt(), anyBoolean())).thenReturn(filteredUsersWithoutPagination.size());

when(mockedUserStoreManager.getRoleListOfUserWithID(anyString())).thenReturn(new ArrayList<>());

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,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.16</carbon.kernel.version>
<carbon.kernel.version>4.10.17</carbon.kernel.version>
<identity.framework.version>7.5.109</identity.framework.version>
<junit.version>4.13.1</junit.version>
<commons.lang.version>20030203.000129</commons.lang.version>
Expand Down

0 comments on commit eab796e

Please sign in to comment.