From 8e86da118e01e30e46bfdc4e126aaf733475b5c0 Mon Sep 17 00:00:00 2001 From: Momchil Ignatov Date: Wed, 7 Aug 2024 12:26:50 +0300 Subject: [PATCH] feat: improve policy comma handling (#74) * Policy rule comma handling * Policy rule comma handling --------- Co-authored-by: Momchil Ignatov --- .../org/casbin/adapter/JDBCBaseAdapter.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java b/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java index f5769bb..d6e59e4 100644 --- a/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java +++ b/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java @@ -179,6 +179,7 @@ protected void migrate() throws SQLException { } protected void loadPolicyLine(CasbinRule line, Model model) { + escapeCasbinRule(line); String lineText = line.ptype; if (!"".equals(line.v0)) { lineText += ", " + line.v0; @@ -540,4 +541,20 @@ protected void retry(ExecutionContext ctx) throws SQLException { protected String renderActualSql(String sql) { return sql.replace(DEFAULT_TABLE_NAME, tableName); } + + private void escapeCasbinRule(CasbinRule line) { + line.v0 = escapeSingleRule(line.v0); + line.v1 = escapeSingleRule(line.v1); + line.v2 = escapeSingleRule(line.v2); + line.v3 = escapeSingleRule(line.v3); + line.v4 = escapeSingleRule(line.v4); + line.v5 = escapeSingleRule(line.v5); + } + + private String escapeSingleRule(String rule) { + if (rule.isEmpty() || (rule.startsWith("\"") && rule.endsWith("\""))) { + return rule; + } + return String.format("\"%s\"", rule); + } }