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

BACKLOG-16810: Return empty query result on incompatible criteria #33

Merged
merged 3 commits into from
Feb 18, 2022
Merged
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 @@ -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