Skip to content

Commit

Permalink
BACKLOG-16810: Return empty query result on incompatible criteria (#33)
Browse files Browse the repository at this point in the history
* Revert "QA-13649 Break the loop, return empty properties set to prevent NPE (#30) (#31)"

This reverts commit d4b76cf.

* BACKLOG-16810: Return empty query result on incompatible criteria

* Fix npe check
  • Loading branch information
gflores-jahia committed Feb 18, 2022
1 parent 2a13a0d commit 07a7951
Showing 1 changed file with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1203,26 +1203,26 @@ private Properties attributesToJahiaProperties(Attributes attributes, boolean is
}

/**
* build a user query, that use the searchCriteria from jahia forms
* Build a user query, that use the searchCriteria from jahia forms
*
* If any of the searchCriteria doesn't map to LDAP properties,
* then it returns an empty query (query that returns 0 results)
*
* @param searchCriteria
* @return
*/
private ContainerCriteria buildUserQuery(Properties searchCriteria) {
// transform jnt:user props to ldap props
Properties ldapfilters = mapJahiaPropertiesToLDAP(searchCriteria, userConfig.getAttributesMapper());

// if no jnt:user props map to ldap props, then return an empty query i.e. limit results to 0
int searchCountLimit = (ldapfilters == null) ? 0 : (int) userConfig.getSearchCountlimit();
List<String> attributesToRetrieve = getUserAttributes();
ContainerCriteria query = query().base(userConfig.getUidSearchName())
.attributes(attributesToRetrieve.toArray(new String[attributesToRetrieve.size()]))
.countLimit((int) userConfig.getSearchCountlimit())
.countLimit(searchCountLimit)
.where(OBJECTCLASS_ATTRIBUTE).is(StringUtils.defaultString(userConfig.getSearchObjectclass(), "*"));

// transform jnt:user props to ldap props
Properties ldapfilters = mapJahiaPropertiesToLDAP(searchCriteria, userConfig.getAttributesMapper());

if (ldapfilters == null) {
return null;
}

applyPredefinedUserFilter(query);

// define and / or operator
Expand Down Expand Up @@ -1302,16 +1302,18 @@ private ContainerCriteria buildGroupQuery(Properties searchCriteria, boolean isD
attributesToRetrieve.add(groupConfig.getDynamicMembersAttribute());
}

// transform jnt:group props to ldap props
Properties ldapfilters = mapJahiaPropertiesToLDAP(searchCriteria, groupConfig.getAttributesMapper());

// if no jnt:user props map to ldap props, then return an empty query i.e. limit results to 0
int searchCountLimit = (ldapfilters == null) ? 0 : (int) groupConfig.getSearchCountlimit();
ContainerCriteria query = query().base(groupConfig.getSearchName())
.attributes(attributesToRetrieve.toArray(new String[attributesToRetrieve.size()]))
.countLimit((int) groupConfig.getSearchCountlimit())
.countLimit(searchCountLimit)
.where(OBJECTCLASS_ATTRIBUTE).is(isDynamic ? groupConfig.getDynamicSearchObjectclass() : groupConfig.getSearchObjectclass());

applyPredefinedGroupFilter(query);

// transform jnt:user props to ldap props
Properties ldapfilters = mapJahiaPropertiesToLDAP(searchCriteria, groupConfig.getAttributesMapper());

// define and / or operator
boolean orOp = isOrOperator(ldapfilters, searchCriteria);

Expand All @@ -1326,7 +1328,7 @@ private ContainerCriteria buildGroupQuery(Properties searchCriteria, boolean isD
}

private static boolean isOrOperator(Properties ldapfilters, Properties searchCriteria) {
if (ldapfilters.size() > 1) {
if (ldapfilters != null && ldapfilters.size() > 1) {
if (searchCriteria.containsKey(JahiaUserManagerService.MULTI_CRITERIA_SEARCH_OPERATION)) {
if (((String) searchCriteria.get(JahiaUserManagerService.MULTI_CRITERIA_SEARCH_OPERATION)).trim().toLowerCase().equals("and")) {
return false;
Expand All @@ -1346,6 +1348,10 @@ private static boolean isOrOperator(Properties ldapfilters, Properties searchCri
*/
private ContainerCriteria getQueryFilters(Properties ldapfilters, AbstractConfig config, boolean isOrOperator) {
ContainerCriteria filterQuery = null;
if (ldapfilters == null) {
return filterQuery;
}

if (ldapfilters.containsKey("*")) {
// Search on all wildcards attributes
String filterValue = ldapfilters.getProperty("*");
Expand Down Expand Up @@ -1408,7 +1414,7 @@ private Properties mapJahiaPropertiesToLDAP(Properties searchCriteria, Map<Strin
if (configProperties.containsKey(entry.getKey())) {
p.setProperty(configProperties.get(entry.getKey()), (String) entry.getValue());
} else if (!entry.getKey().equals("*") && !entry.getKey().equals(JahiaUserManagerService.MULTI_CRITERIA_SEARCH_OPERATION)) {
break;
return null;
}
}

Expand Down

0 comments on commit 07a7951

Please sign in to comment.