use of org.casbin.jcasbin.rbac.DefaultRoleManager in project jcasbin by casbin.
the class CoreEnforcer method addNamedDomainMatchingFunc.
/**
* addNamedMatchingFunc add MatchingFunc by ptype RoleManager
*/
public boolean addNamedDomainMatchingFunc(String ptype, String name, BiPredicate<String, String> fn) {
if (rmMap.containsKey(ptype)) {
DefaultRoleManager rm = (DefaultRoleManager) rmMap.get(ptype);
rm.addDomainMatchingFunc(name, fn);
clearRmMap();
if (autoBuildRoleLinks) {
buildRoleLinks();
}
return true;
}
return false;
}
use of org.casbin.jcasbin.rbac.DefaultRoleManager in project jcasbin by casbin.
the class RbacAPIUnitTest method testRoleAPIWithRegex.
@Test
public void testRoleAPIWithRegex() {
Enforcer e = new Enforcer("examples/rbac_model.conf");
e.setAdapter(new FileAdapter("examples/rbac_with_pattern_regex_policy.csv"));
e.setRoleManager("g", new DefaultRoleManager(10, BuiltInFunctions::regexMatch, null));
e.loadPolicy();
testGetRoles(e, "root", asList("admin"));
testGetRoles(e, "^E\\d+$", asList("employee"));
testGetRoles(e, "E101", asList("^E\\d+$"));
assertEquals(e.getImplicitRolesForUser("E101"), asList("^E\\d+$", "employee"));
testEnforce(e, "E101", "data1", "read", true);
testEnforce(e, "E101", "data1", "write", false);
e.addRoleForUser("^E\\d+$", "admin");
testGetRoles(e, "^E\\d+$", asList("employee", "admin"));
testGetRoles(e, "E101", asList("^E\\d+$"));
assertEquals(e.getImplicitRolesForUser("E101"), asList("^E\\d+$", "employee", "admin"));
testEnforce(e, "E101", "data1", "read", true);
testEnforce(e, "E101", "data1", "write", true);
e.deleteRoleForUser("^E\\d+$", "admin");
testGetRoles(e, "^E\\d+$", asList("employee"));
testGetRoles(e, "E101", asList("^E\\d+$"));
assertEquals(e.getImplicitRolesForUser("E101"), asList("^E\\d+$", "employee"));
testEnforce(e, "E101", "data1", "read", true);
testEnforce(e, "E101", "data1", "write", false);
}
use of org.casbin.jcasbin.rbac.DefaultRoleManager in project jcasbin by casbin.
the class RbacAPIWithPatternMatchUnitTest method testImplicitPermissionAPIWithDomainMatch.
@Test
public void testImplicitPermissionAPIWithDomainMatch() {
final Enforcer e = new Enforcer("examples/rbac_with_domain_pattern_model.conf");
e.setAdapter(new FileAdapter("examples/rbac_with_domain_pattern_policy.csv"));
e.setRoleManager(new DefaultRoleManager(10, null, BuiltInFunctions::allMatch));
e.loadPolicy();
testGetImplicitPermissionsInDomain(e, "alice", "domain1", asList(asList("admin", "domain1", "data1", "read"), asList("admin", "domain1", "data1", "write")));
}
use of org.casbin.jcasbin.rbac.DefaultRoleManager in project jcasbin by casbin.
the class RbacAPIWithPatternMatchUnitTest method testRoleAPIWithDomainMatch.
@Test
public void testRoleAPIWithDomainMatch() {
final Enforcer e = new Enforcer("examples/rbac_with_domain_pattern_model.conf");
e.setAdapter(new FileAdapter("examples/rbac_with_domain_pattern_policy.csv"));
e.setRoleManager(new DefaultRoleManager(10, null, BuiltInFunctions::allMatch));
e.loadPolicy();
testGetRolesInDomain(e, "alice", "domain1", asList("admin"));
testGetRolesInDomain(e, "alice", "domain2", asList("admin"));
testGetRolesInDomain(e, "bob", "domain1", asList());
testGetRolesInDomain(e, "bob", "domain2", asList("admin"));
}
use of org.casbin.jcasbin.rbac.DefaultRoleManager in project jcasbin by casbin.
the class RbacAPIWithPatternMatchUnitTest method testEnforceAPIWithKeyMatch3Pattern.
@Test
public void testEnforceAPIWithKeyMatch3Pattern() {
final Enforcer e = new Enforcer("examples/rbac_with_pattern_model.conf");
e.setAdapter(new FileAdapter("examples/rbac_with_pattern_policy.csv"));
e.setRoleManager("g2", new DefaultRoleManager(10, BuiltInFunctions::keyMatch3, null));
e.loadPolicy();
testEnforce(e, "alice", "/book/1", "GET", true);
testEnforce(e, "alice", "/book/2", "GET", true);
testEnforce(e, "alice", "/pen/1", "GET", true);
testEnforce(e, "alice", "/pen/2", "GET", false);
testEnforce(e, "bob", "/book/1", "GET", false);
testEnforce(e, "bob", "/book/2", "GET", false);
testEnforce(e, "bob", "/pen/1", "GET", false);
testEnforce(e, "bob", "/pen/2", "GET", false);
testEnforce(e, "alice", "/book2/1", "GET", true);
testEnforce(e, "alice", "/book2/2", "GET", true);
testEnforce(e, "alice", "/pen2/1", "GET", true);
testEnforce(e, "alice", "/pen2/2", "GET", false);
testEnforce(e, "bob", "/book2/1", "GET", false);
testEnforce(e, "bob", "/book2/2", "GET", false);
testEnforce(e, "bob", "/pen2/1", "GET", true);
testEnforce(e, "bob", "/pen2/2", "GET", true);
}
Aggregations