Skip to content
Merged
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
13 changes: 11 additions & 2 deletions src/main/java/org/casbin/jcasbin/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Util {
public static boolean enableLog = true;
private static Pattern evalReg = Pattern.compile("\\beval\\(([^),]*)\\)");

private static final Pattern IN_SYNTAX_PATTERN = Pattern.compile("([a-zA-Z0-9_.()\"]*) +in +([a-zA-Z0-9_.()\"]*)");
private static final Pattern IN_SYNTAX_PATTERN = Pattern.compile("([^\\s]+?) +in +(\\([^)]*\\)|\\S+)");

private static Pattern escapeAssertionRegex = Pattern.compile("\\b(r|p)[0-9]*\\.");

Expand Down Expand Up @@ -181,7 +181,16 @@ public static String convertInSyntax(String expString) {
private static String replaceInSyntaxWithInclude(Matcher matcher) {
StringBuffer sb = new StringBuffer();
do {
matcher.appendReplacement(sb, "include($2, $1)");
String leftOperand = matcher.group(1);
String rightOperand = matcher.group(2);

// If right operand is a parenthesized expression with commas (tuple literal),
// convert to tuple() function. Otherwise, keep parentheses as-is for grouping.
if (rightOperand.startsWith("(") && rightOperand.endsWith(")") && rightOperand.contains(",")) {
rightOperand = "tuple" + rightOperand;
}

matcher.appendReplacement(sb, Matcher.quoteReplacement("include(" + rightOperand + ", " + leftOperand + ")"));
} while (matcher.find());
matcher.appendTail(sb);
return sb.toString();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,28 @@ public void testEscapeAssertion(){

@Test
public void testConvertInSyntax(){
// Basic cases
assertEquals("include(r_obj, r_sub)", Util.convertInSyntax("r_sub in r_obj"));
assertEquals("include(r_obj, r_sub.name)", Util.convertInSyntax("r_sub.name in r_obj"));
assertEquals("include(r_obj.name, r_sub.name)", Util.convertInSyntax("r_sub.name in r_obj.name"));
assertEquals("include(r_obj, r_sub) && r.obj == p.obj", Util.convertInSyntax("r_sub in r_obj && r.obj == p.obj"));

// Tuple/array with single quotes - should convert to tuple() function
assertEquals("include(tuple('data2', 'data3'), r.obj)", Util.convertInSyntax("r.obj in ('data2', 'data3')"));

// Tuple/array with double quotes - should convert to tuple() function
assertEquals("include(tuple(\"data2\", \"data3\"), r.obj)", Util.convertInSyntax("r.obj in (\"data2\", \"data3\")"));

// Tuple/array without quotes - should convert to tuple() function
assertEquals("include(tuple(data2, data3), r.obj)", Util.convertInSyntax("r.obj in (data2, data3)"));

// Complex expression with 'in' operator and tuple (the original issue scenario)
assertEquals("g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act || include(tuple('data2', 'data3'), r.obj)",
Util.convertInSyntax("g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act || r.obj in ('data2', 'data3')"));

// Multiple 'in' operators
assertEquals("include(r.obj.admins, r.sub.name) && include(r.obj.admins, (\"bob\"))",
Util.convertInSyntax("r.sub.name in r.obj.admins && (\"bob\") in r.obj.admins"));
}

@Test
Expand Down