Skip to content

Commit d3e66e8

Browse files
authored
feat: implement UpdatableAdapter and add tests (#73)
1 parent 37f90e8 commit d3e66e8

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/main/java/org/casbin/adapter/JDBCBaseAdapter.java

+43-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.casbin.jcasbin.persist.Adapter;
2525
import org.casbin.jcasbin.persist.BatchAdapter;
2626
import org.casbin.jcasbin.persist.Helper;
27+
import org.casbin.jcasbin.persist.UpdatableAdapter;
2728

2829
import javax.sql.DataSource;
2930
import java.sql.*;
@@ -49,7 +50,7 @@ public String[] toStringArray() {
4950
* JDBCAdapter is the JDBC adapter for jCasbin.
5051
* It can load policy from JDBC supported database or save policy to it.
5152
*/
52-
abstract class JDBCBaseAdapter implements Adapter, BatchAdapter {
53+
abstract class JDBCBaseAdapter implements Adapter, BatchAdapter, UpdatableAdapter {
5354
protected static final String DEFAULT_TABLE_NAME = "casbin_rule";
5455
protected static final boolean DEFAULT_REMOVE_POLICY_FAILED = false;
5556
protected static final boolean DEFAULT_AUTO_CREATE_TABLE = true;
@@ -480,6 +481,47 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
480481
});
481482
}
482483

484+
/**
485+
* updatePolicy updates a policy rule from the current policy.
486+
*/
487+
@Override
488+
public void updatePolicy(String sec, String ptype, List<String> oldRule, List<String> newRule) {
489+
if (CollectionUtils.isEmpty(oldRule) || CollectionUtils.isEmpty(newRule)) {
490+
return;
491+
}
492+
493+
String sql = renderActualSql("INSERT INTO casbin_rule (ptype,v0,v1,v2,v3,v4,v5) VALUES(?,?,?,?,?,?,?)");
494+
495+
496+
Failsafe.with(retryPolicy).run(ctx -> {
497+
if (ctx.isRetry()) {
498+
retry(ctx);
499+
}
500+
conn.setAutoCommit(false);
501+
removePolicy(sec, ptype, oldRule);
502+
try (PreparedStatement ps = conn.prepareStatement(sql)) {
503+
CasbinRule line = this.savePolicyLine(ptype, newRule);
504+
505+
ps.setString(1, line.ptype);
506+
ps.setString(2, line.v0);
507+
ps.setString(3, line.v1);
508+
ps.setString(4, line.v2);
509+
ps.setString(5, line.v3);
510+
ps.setString(6, line.v4);
511+
ps.setString(7, line.v5);
512+
ps.executeUpdate();
513+
conn.commit();
514+
} catch (SQLException e) {
515+
conn.rollback();
516+
517+
e.printStackTrace();
518+
throw e;
519+
} finally {
520+
conn.setAutoCommit(true);
521+
}
522+
});
523+
}
524+
483525
/**
484526
* Close the Connection.
485527
*/

src/test/java/org/casbin/adapter/JDBCAdapterTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,37 @@ public void testRemovePolicy() throws Exception {
257257
asList("data2_admin", "data2", "read"),
258258
asList("data2_admin", "data2", "write")));
259259
}
260+
261+
@Test
262+
public void testUpdatePolicy() throws Exception {
263+
JDBCAdapter adapter = new MySQLAdapterCreator().create();
264+
265+
// Because the DB is empty at first,
266+
// so we need to load the policy from the file adapter (.CSV) first.
267+
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
268+
269+
// This is a trick to save the current policy to the DB.
270+
// We can't call e.savePolicy() because the adapter in the enforcer is still the file adapter.
271+
// The current policy means the policy in the jCasbin enforcer (aka in memory).
272+
adapter.savePolicy(e.getModel());
273+
274+
e.clearPolicy();
275+
testGetPolicy(e, asList());
276+
277+
e = new Enforcer("examples/rbac_model.conf", adapter);
278+
testGetPolicy(e, asList(
279+
asList("alice", "data1", "read"),
280+
asList("bob", "data2", "write"),
281+
asList("data2_admin", "data2", "read"),
282+
asList("data2_admin", "data2", "write")));
283+
284+
adapter.updatePolicy("p", "p", asList("bob", "data2", "write"), asList("alice", "data2", "read"));
285+
e = new Enforcer("examples/rbac_model.conf", adapter);
286+
testGetPolicy(e, asList(
287+
asList("alice", "data1", "read"),
288+
asList("data2_admin", "data2", "read"),
289+
asList("data2_admin", "data2", "write"),
290+
asList("alice", "data2", "read")));
291+
292+
}
260293
}

0 commit comments

Comments
 (0)