Skip to content

Commit

Permalink
fix: modify the implement of removePolicy() (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: imp2002 <imp07@qq.com>
  • Loading branch information
imp2002 authored Jun 12, 2022
1 parent 7f1e7f8 commit 7bde0b2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 20 deletions.
66 changes: 46 additions & 20 deletions src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected void migrate() throws SQLException {
}

stmt.executeUpdate(sql);
if (productName.equals("Oracle")) {
if ("Oracle".equals(productName)) {
sql = renderActualSql("declare " +
"V_NUM number;" +
"BEGIN " +
Expand All @@ -152,16 +152,16 @@ protected void migrate() throws SQLException {
"if V_NUM > 0 then " +
"null;" +
"else " +
"execute immediate 'create trigger casbin_id_autoincrement before "+
" insert on CASBIN_RULE for each row "+
" when (new.id is null) "+
" begin "+
" select casbin_sequence.nextval into:new.id from dual;"+
"execute immediate 'create trigger casbin_id_autoincrement before " +
" insert on CASBIN_RULE for each row " +
" when (new.id is null) " +
" begin " +
" select casbin_sequence.nextval into:new.id from dual;" +
" end;';" +
"end if;" +
"END;");
stmt.executeUpdate(sql);
} else if (productName.equals("PostgreSQL")) {
} else if ("PostgreSQL".equals(productName)) {
sql = renderActualSql("CREATE TABLE IF NOT EXISTS casbin_rule(id int NOT NULL PRIMARY KEY default nextval('CASBIN_SEQUENCE'::regclass), ptype VARCHAR(100) NOT NULL, v0 VARCHAR(100), v1 VARCHAR(100), v2 VARCHAR(100), v3 VARCHAR(100), v4 VARCHAR(100), v5 VARCHAR(100))");
stmt.executeUpdate(sql);
}
Expand Down Expand Up @@ -206,12 +206,12 @@ public void loadPolicy(Model model) {
while (rSet.next()) {
CasbinRule line = new CasbinRule();
line.ptype = rSet.getObject(1) == null ? "" : (String) rSet.getObject(1);
line.v0 = rSet.getObject(2) == null ? "" : (String) rSet.getObject(2);
line.v1 = rSet.getObject(3) == null ? "" : (String) rSet.getObject(3);
line.v2 = rSet.getObject(4) == null ? "" : (String) rSet.getObject(4);
line.v3 = rSet.getObject(5) == null ? "" : (String) rSet.getObject(5);
line.v4 = rSet.getObject(6) == null ? "" : (String) rSet.getObject(6);
line.v5 = rSet.getObject(7) == null ? "" : (String) rSet.getObject(7);
line.v0 = rSet.getObject(2) == null ? "" : (String) rSet.getObject(2);
line.v1 = rSet.getObject(3) == null ? "" : (String) rSet.getObject(3);
line.v2 = rSet.getObject(4) == null ? "" : (String) rSet.getObject(4);
line.v3 = rSet.getObject(5) == null ? "" : (String) rSet.getObject(5);
line.v4 = rSet.getObject(6) == null ? "" : (String) rSet.getObject(6);
line.v5 = rSet.getObject(7) == null ? "" : (String) rSet.getObject(7);
loadPolicyLine(line, model);
}
}
Expand Down Expand Up @@ -310,7 +310,7 @@ public void savePolicy(Model model) {
}
}

if(count!=0){
if (count != 0) {
ps.executeBatch();
}

Expand All @@ -325,14 +325,15 @@ public void savePolicy(Model model) {
}
});
}

/**
* addPolicy adds a policy rule to the storage.
*/
@Override
public void addPolicy(String sec, String ptype, List<String> rule) {
List<List<String>> rules = new ArrayList<>();
rules.add(rule);
this.addPolicies(sec,ptype,rules);
this.addPolicies(sec, ptype, rules);
}

@Override
Expand All @@ -351,7 +352,7 @@ public void addPolicies(String sec, String ptype, List<List<String>> rules) {
conn.setAutoCommit(false);
int count = 0;
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for(List<String> rule:rules){
for (List<String> rule : rules) {
CasbinRule line = savePolicyLine(ptype, rule);

ps.setString(1, line.ptype);
Expand All @@ -363,12 +364,12 @@ public void addPolicies(String sec, String ptype, List<List<String>> rules) {
ps.setString(7, line.v5);
ps.addBatch();
if (++count == batchSize) {
count=0;
count = 0;
ps.executeBatch();
ps.clearBatch();
}
}
if(count!=0){
if (count != 0) {
ps.executeBatch();
}
conn.commit();
Expand All @@ -391,7 +392,32 @@ public void removePolicy(String sec, String ptype, List<String> rule) {
if (CollectionUtils.isEmpty(rule)) {
return;
}
removeFilteredPolicy(sec, ptype, 0, rule.toArray(new String[0]));

Failsafe.with(retryPolicy).run(ctx -> {
if (ctx.isRetry()) {
retry(ctx);
}
String sql = renderActualSql("DELETE FROM casbin_rule WHERE ptype = ?");
int columnIndex = 0;
for (int i = 0; i < rule.size(); i++) {
sql = String.format("%s%s%s%s", sql, " AND v", columnIndex, " = ?");
columnIndex++;
}
while (columnIndex <= 5) {
sql = String.format("%s%s%s%s", sql, " AND v", columnIndex, " IS NULL");
columnIndex++;
}
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, ptype);
for (int j = 0; j < rule.size(); j++) {
ps.setString(j + 2, rule.get(j));
}
int rows = ps.executeUpdate();
if (rows < 1 && removePolicyFailed) {
throw new CasbinAdapterException(String.format("Remove policy error, remove %d rows, expect least 1 rows", rows));
}
}
});
}

@Override
Expand All @@ -407,7 +433,7 @@ public void removePolicies(String sec, String ptype, List<List<String>> rules) {
conn.setAutoCommit(false);
try {
for (List<String> rule : rules) {
removeFilteredPolicy(sec, ptype, 0, rule.toArray(new String[0]));
removePolicy(sec, ptype, rule);
}
conn.commit();
} catch (SQLException e) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/casbin/adapter/JDBCAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,48 @@ public void testConstructorParams() throws Exception {
adapter.close();
adapterViaDataSource.close();
}

@Test
public void testRemovePolicy() throws Exception {
JDBCAdapter adapter = new MySQLAdapterCreator().create();

// Because the DB is empty at first,
// so we need to load the policy from the file adapter (.CSV) first.
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");

// This is a trick to save the current policy to the DB.
// We can't call e.savePolicy() because the adapter in the enforcer is still the file adapter.
// The current policy means the policy in the jCasbin enforcer (aka in memory).
adapter.savePolicy(e.getModel());

e.clearPolicy();
testGetPolicy(e, asList());

e = new Enforcer("examples/rbac_model.conf", adapter);
testGetPolicy(e, asList(
asList("alice", "data1", "read"),
asList("bob", "data2", "write"),
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")));

adapter.removePolicy("p", "p", Arrays.asList("alice", "data1", "read"));
e = new Enforcer("examples/rbac_model.conf", adapter);
testGetPolicy(e, asList(
asList("bob", "data2", "write"),
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")));

adapter.removePolicy("p", "p", Arrays.asList("bob", "data2"));
e = new Enforcer("examples/rbac_model.conf", adapter);
testGetPolicy(e, asList(
asList("bob", "data2", "write"),
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")));

adapter.removePolicy("p", "p", Arrays.asList("bob", "data2", "write"));
e = new Enforcer("examples/rbac_model.conf", adapter);
testGetPolicy(e, asList(
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")));
}
}

0 comments on commit 7bde0b2

Please sign in to comment.