Skip to content

Commit

Permalink
[SYNCOPE-1826] ensuring to correctly handle plus in fiql and make sea…
Browse files Browse the repository at this point in the history
…rch with four digits work (#804)

* [SYNCOPE-1826] ensuring to correctly handle plus in fiql and make search with four digits work
  • Loading branch information
andrea-patricelli committed Aug 6, 2024
1 parent 3267a1b commit 318db33
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.cxf.jaxrs.ext.search.ConditionType;
import org.apache.cxf.jaxrs.ext.search.SearchBean;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
Expand Down Expand Up @@ -52,8 +51,6 @@
*/
public class SearchCondVisitor extends AbstractSearchConditionVisitor<SearchBean, SearchCond> {

protected static final Pattern TIMEZONE = Pattern.compile(".* [0-9]{4}$");

protected static final ThreadLocal<String> REALM = new ThreadLocal<>();

protected static final ThreadLocal<SearchCond> SEARCH_COND = new ThreadLocal<>();
Expand All @@ -75,18 +72,12 @@ protected static AttrCond createAttrCond(final String schema) {
}

protected static String getValue(final SearchCondition<SearchBean> sc) {
String value = SearchUtils.toSqlWildcardString(
URLDecoder.decode(sc.getStatement().getValue().toString(), StandardCharsets.UTF_8), false);
String value = SearchUtils.toSqlWildcardString(URLDecoder.decode(sc.getStatement().getValue().toString()
.replace("+", "%2B"), StandardCharsets.UTF_8), false);
if (value.indexOf('%') == -1) {
value = value.replaceAll("\\\\_", "_");
}

if (TIMEZONE.matcher(value).matches()) {
char[] valueAsArray = value.toCharArray();
valueAsArray[valueAsArray.length - 5] = '+';
value = new String(valueAsArray);
}

return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,24 @@ public void issueSYNCOPE1779() {

assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
}

@Test
public void issueSYNCOPE1826() {
String fiql = new UserFiqlSearchConditionBuilder().is("username").equalToIgnoreCase("sh test app 0722").query();
assertEquals("username=~sh test app 0722", fiql);

AnyCond anyCond = new AnyCond(AttrCond.Type.IEQ);
anyCond.setSchema("username");
anyCond.setExpression("sh test app 0722");

assertEquals(SearchCond.getLeaf(anyCond), SearchCondConverter.convert(VISITOR, fiql));

fiql = "lastLoginDate==2016-03-02T15:21:22%2B0300";

AnyCond lastLoginDateCond = new AnyCond(AttrCond.Type.EQ);
lastLoginDateCond.setSchema("lastLoginDate");
lastLoginDateCond.setExpression("2016-03-02T15:21:22+0300");

assertEquals(SearchCond.getLeaf(lastLoginDateCond), SearchCondConverter.convert(VISITOR, fiql));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ under the License.
password="5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8" cipherAlgorithm="SHA1"
realm_id="e4c28e7a-9dbf-4ee7-9441-93812a0d4a28"
username="bellini" creator="admin" lastModifier="admin"
creationDate="2010-10-20 11:00:00" lastChangeDate="2010-10-20 11:00:00" suspended="0"
creationDate="2010-10-20 11:00:00" lastChangeDate="2010-10-20 11:00:00"
lastLoginDate="2016-03-03 15:21:22" suspended="0"
plainAttrs='[{"values":[{"stringValue":"Vincenzo"}],"schema":"firstname"},{"values":[{"stringValue":"Bellini"}],"schema":"surname"},{"values":[{"dateValue":"2009-06-24T00:00:00+02:00"}],"schema":"loginDate"},{"values":[{"booleanValue":true}],"schema":"cool"},{"values":[{"stringValue":"M"}],"schema":"gender"},{"uniqueValue":{"stringValue":"Vincenzo Bellini"},"schema":"fullname"},{"uniqueValue":{"stringValue":"bellini@apache.org"},"schema":"userId"}]'/>
<SyncopeUser_SyncopeRole user_id="c9b2dec2-00a7-4855-97c0-d854842b4b24" role_id="User reviewer"/>
<SyncopeUser_SyncopeRole user_id="c9b2dec2-00a7-4855-97c0-d854842b4b24" role_id="User manager"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ under the License.
password="5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8" cipherAlgorithm="SHA1"
realm_id="e4c28e7a-9dbf-4ee7-9441-93812a0d4a28"
username="bellini" creator="admin" lastModifier="admin"
creationDate="2010-10-20 11:00:00" lastChangeDate="2010-10-20 11:00:00" suspended="0"/>
creationDate="2010-10-20 11:00:00" lastLoginDate="2016-03-03 15:21:22" lastChangeDate="2010-10-20 11:00:00" suspended="0"/>
<SyncopeUser_SyncopeRole user_id="c9b2dec2-00a7-4855-97c0-d854842b4b24" role_id="User reviewer"/>
<SyncopeUser_SyncopeRole user_id="c9b2dec2-00a7-4855-97c0-d854842b4b24" role_id="User manager"/>
<SyncopeUser mustChangePassword="0" id="823074dc-d280-436d-a7dd-07399fae48ec" status="active"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public void searchByDate() {
PagedResult<UserTO> issueSYNCOPE1321 = USER_SERVICE.search(new AnyQuery.Builder().
realm(SyncopeConstants.ROOT_REALM).
fiql(SyncopeClient.getUserSearchConditionBuilder().
is("creationDate").lexicalNotBefore("2009-03-02 15:21:22").
is("lastLoginDate").lexicalNotBefore("2016-03-02T15:21:22%2B0300").
and("username").equalTo("bellini").query()).
build());
assertEquals(users, issueSYNCOPE1321);
Expand Down Expand Up @@ -1002,4 +1002,52 @@ public void issueSYNCOPE1800() {
assertEquals(1, users.getResult().size());
assertEquals(user.getKey(), users.getResult().get(0).getKey());
}

@Test
void issueSYNCOPE1826() {
UserCR userCR = UserITCase.getUniqueSample("issueSearch1@syncope.apache.org");
userCR.setUsername("user test 1826");
createUser(userCR);

AnyObjectTO anotherPrinter = createAnyObject(new AnyObjectCR.Builder(SyncopeConstants.ROOT_REALM,
PRINTER,
"obj test 1826").build()).getEntity();

userCR = UserITCase.getUniqueSample("issueSearch2@syncope.apache.org");
userCR.setUsername("user 1826 test");
createUser(userCR);

userCR = UserITCase.getUniqueSample("issueSearch3@syncope.apache.org");
userCR.setUsername("user test 182");
createUser(userCR);

if (IS_EXT_SEARCH_ENABLED) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore
}
}

try {
assertFalse(USER_SERVICE.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).details(false)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("username")
.equalToIgnoreCase("user test 1826").query()).build()).getResult().isEmpty());
assertFalse(ANY_OBJECT_SERVICE.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM)
.details(false).fiql(SyncopeClient.getAnyObjectSearchConditionBuilder(PRINTER).is("name")
.equalToIgnoreCase("obj test 1826").query()).build()).getResult().isEmpty());
assertFalse(USER_SERVICE.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).details(false)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("username")
.equalToIgnoreCase("user 1826 test").query()).build()).getResult().isEmpty());
assertFalse(USER_SERVICE.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).details(false)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("username")
.equalToIgnoreCase("user test 182").query()).build()).getResult().isEmpty());
} finally {
deleteUser("user test 1826");
deleteAnyObject(anotherPrinter.getKey());
deleteUser("user 1826 test");
deleteUser("user test 182");
}
}

}

0 comments on commit 318db33

Please sign in to comment.