Skip to content

Commit

Permalink
Implement updatableAdapter for JDBCBaseAdapter and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Muluo-cyan committed May 10, 2024
1 parent 37f90e8 commit fdcbf8e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
45 changes: 44 additions & 1 deletion src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.casbin.jcasbin.persist.Adapter;
import org.casbin.jcasbin.persist.BatchAdapter;
import org.casbin.jcasbin.persist.Helper;
import org.casbin.jcasbin.persist.UpdatableAdapter;

import javax.sql.DataSource;
import java.sql.*;
Expand All @@ -49,7 +50,7 @@ public String[] toStringArray() {
* JDBCAdapter is the JDBC adapter for jCasbin.
* It can load policy from JDBC supported database or save policy to it.
*/
abstract class JDBCBaseAdapter implements Adapter, BatchAdapter {
abstract class JDBCBaseAdapter implements Adapter, BatchAdapter, UpdatableAdapter {
protected static final String DEFAULT_TABLE_NAME = "casbin_rule";
protected static final boolean DEFAULT_REMOVE_POLICY_FAILED = false;
protected static final boolean DEFAULT_AUTO_CREATE_TABLE = true;
Expand Down Expand Up @@ -480,6 +481,48 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
});
}

/**
* updatePolicy updates a policy rule from the current policy.
*/
@Override
public void updatePolicy(String sec, String ptype, List<String> oldRule, List<String> newRule) {
if (CollectionUtils.isEmpty(oldRule) || CollectionUtils.isEmpty(newRule)) {
return;
}

String sql = renderActualSql("INSERT INTO casbin_rule (ptype,v0,v1,v2,v3,v4,v5) VALUES(?,?,?,?,?,?,?)");


Failsafe.with(retryPolicy).run(ctx -> {
if (ctx.isRetry()) {
retry(ctx);
}
conn.setAutoCommit(false);
removePolicy(sec, ptype, oldRule);
try (PreparedStatement ps = conn.prepareStatement(sql)) {
CasbinRule line = this.savePolicyLine(ptype, newRule);

ps.setString(1, line.ptype);
ps.setString(2, line.v0);
ps.setString(3, line.v1);
ps.setString(4, line.v2);
ps.setString(5, line.v3);
ps.setString(6, line.v4);
ps.setString(7, line.v5);
ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();

e.printStackTrace();
throw e;
} finally {
conn.setAutoCommit(true);
}
});
}


/**
* Close the Connection.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/casbin/adapter/AdapterCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public interface AdapterCreator {
}

class MySQLAdapterCreator implements AdapterCreator {
private String url = "jdbc:mysql://127.0.0.1:3306/casbin?serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true";
private String username = "casbin_test";
private String password = "TEST_casbin";
private String url = "jdbc:mysql://127.0.0.1:3306/jcasbin?serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true";
private String username = "root";
private String password = "Wodemima0";
private String driver = "com.mysql.cj.jdbc.Driver";

@Override
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/casbin/adapter/JDBCAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,16 @@ public void testRemovePolicy() throws Exception {
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")));
}

@Test
public void testUpdatePolicy() throws Exception {
JDBCAdapter adapter = new MySQLAdapterCreator().create();
Enforcer e = new Enforcer("examples/rbac_model.conf", adapter);
testGetPolicy(e, asList(
asList("bob", "data2", "write")));
adapter.updatePolicy("p", "p", asList("bob", "data2", "write"), asList("alice", "data2", "read"));
e = new Enforcer("examples/rbac_model.conf", adapter);
testGetPolicy(e, asList(
asList("alice", "data2", "read")));
}
}

0 comments on commit fdcbf8e

Please sign in to comment.