Skip to content

Commit

Permalink
feat: support batchAdapter (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
imp2002 authored Nov 16, 2022
1 parent 472fa35 commit 564b800
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<dependency>
<groupId>org.casbin</groupId>
<artifactId>jcasbin</artifactId>
<version>1.1.0</version>
<version>1.31.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
Expand All @@ -165,6 +165,11 @@
<version>8.2.2.jre8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>


Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/casbin/adapter/CasbinRuleDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ public interface CasbinRuleDao {
@Insert("INSERT INTO casbin_rule (ptype,v0,v1,v2,v3,v4,v5) VALUES (#{ptype},#{v0},#{v1},#{v2},#{v3},#{v4},#{v5})")
void insertData(CasbinRule line);

@Insert({"<script>",
"INSERT INTO casbin_rule (ptype, v0, v1, v2, v3, v4, v5) VALUES ",
"<foreach collection='list' item='item' separator=','>",
"(#{item.ptype}, #{item.v0}, #{item.v1}, #{item.v2}, #{item.v3}, #{item.v4}, #{item.v5})",
"</foreach>",
"</script>"
})
void insertDataBatch(@Param("list") List<CasbinRule> rules);

@Insert("<script>" +
"DELETE FROM casbin_rule WHERE ptype = #{ptype}" +
"<foreach collection=\"list\" item=\"item1\" index=\"index\" separator=\" \">" +
" AND v#{index} = #{item1}" +
"</foreach>" +
"</script>")
void deleteData(@Param("ptype") String ptype, @Param("list") List<String> rules);

}
44 changes: 42 additions & 2 deletions src/main/java/org/casbin/adapter/MybatisAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
import org.casbin.jcasbin.model.Assertion;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.Adapter;
import org.casbin.jcasbin.persist.BatchAdapter;
import org.casbin.jcasbin.persist.Helper;

import javax.sql.DataSource;
import java.util.*;
import java.util.stream.Collectors;


/**
* MybatisAdapter is the Mybatis adapter for jCasbin.
* It can load policy from Mybatis supported database or save policy to it.
*/
public class MybatisAdapter implements Adapter {
public class MybatisAdapter implements Adapter, BatchAdapter {
private String driver;
private String url;
private String username;
Expand Down Expand Up @@ -104,7 +106,7 @@ private String getUrl(String url){
private void createDatabase(){
SqlSession sqlSession = factory.openSession(true);
CasbinRuleDao casbinRuleDao = sqlSession.getMapper(CasbinRuleDao.class);

switch (driver) {
case "com.mysql.cj.jdbc.Driver":
casbinRuleDao.createMysqlDatabase("casbin");
Expand Down Expand Up @@ -285,6 +287,30 @@ public void addPolicy(String sec, String ptype, List<String> rule) {
}


/**
* addPolicies adds policy rules to the storage.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policy rules.
*/
@Override
public void addPolicies(String sec, String ptype, List<List<String>> rules) {
if(CollectionUtils.isEmpty(rules)) {
return;
}
List<CasbinRule> casbinRules = rules.stream()
.map(rule -> savePolicyLine(sec, rule))
.distinct()
.collect(Collectors.toList());

SqlSession sqlSession = factory.openSession(true);
CasbinRuleDao casbinRuleDao = sqlSession.getMapper(CasbinRuleDao.class);
casbinRuleDao.insertDataBatch(casbinRules);
sqlSession.close();
}


/**
* removePolicy removes a policy rule from the storage.
*/
Expand All @@ -294,6 +320,20 @@ public void removePolicy(String sec, String ptype, List<String> rule) {
removeFilteredPolicy(sec, ptype, 0, rule.toArray(new String[0]));
}

/**
* removePolicies removes some policy rules from the storage.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policy rules.
*/
@Override
public void removePolicies(String sec, String ptype, List<List<String>> rules) {
rules.forEach(rule -> {
removePolicy(sec, ptype, rule);
});
}

/**
* removeFilteredPolicy removes policy rules that match the filter from the storage.
*/
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/org/casbin/adapter/MybatisAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,45 @@ public void testAddAndRemovePolicy() {
a.loadPolicy(e.getModel());
testEnforce(e, "cathy", "data1", "read", false);
}

@Test
public void testAddAndRemovePolicyBatch() {
MybatisAdapter a = new MybatisAdapter(DRIVER, URL, USERNAME, PASSWORD);
Enforcer e = new Enforcer("examples/rbac_model.conf", a);

// test addPolicies()
e.clearPolicy();
e.addPolicies(asList(
asList("alice", "data1", "read"),
asList("bob", "data2", "write"),
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")
));
e.clearPolicy();
a.loadPolicy(e.getModel());
testEnforce(e, "alice", "data1", "read", true);
testEnforce(e, "bob", "data2", "write", true);
testEnforce(e, "data2_admin", "data2", "read", true);
testEnforce(e, "data2_admin", "data2", "write", true);

// test removePolicies()
e.clearPolicy();
a.savePolicy(e.getModel());
e.addPolicies(asList(
asList("alice", "data1", "read"),
asList("bob", "data2", "write"),
asList("data2_admin", "data2", "read"),
asList("data2_admin", "data2", "write")
));
e.removePolicies(asList(
asList("alice", "data1", "read"),
asList("bob", "data2", "write")
));
e.clearPolicy();
a.loadPolicy(e.getModel());
testEnforce(e, "alice", "data1", "read", false);
testEnforce(e, "bob", "data2", "write", false);
testEnforce(e, "data2_admin", "data2", "read", true);
testEnforce(e, "data2_admin", "data2", "write", true);
}
}

0 comments on commit 564b800

Please sign in to comment.