Class DefaultRoleManager

java.lang.Object
org.casbin.jcasbin.rbac.DefaultRoleManager
All Implemented Interfaces:
RoleManager
Direct Known Subclasses:
ConditionalRoleManager, GroupRoleManager

public class DefaultRoleManager extends Object implements RoleManager
  • Field Details

  • Constructor Details

    • DefaultRoleManager

      public DefaultRoleManager(int maxHierarchyLevel)
      DefaultRoleManager is the constructor for creating an instance of the default RoleManager implementation.
      Parameters:
      maxHierarchyLevel - the maximized allowed RBAC hierarchy level.
    • DefaultRoleManager

      public DefaultRoleManager(int maxHierarchyLevel, BiPredicate<String,String> matchingFunc, BiPredicate<String,String> domainMatchingFunc)
      In order to use a specific role name matching function, set explicitly the role manager on the Enforcer and rebuild role links (you can optimize by using minimal enforcer constructor).
       final Enforcer e = new Enforcer("model.conf");
       e.setAdapter(new FileAdapter("policies.csv"));
       e.setRoleManager(new DefaultRoleManager(10, BuiltInFunctions::domainMatch));
       e.loadPolicy();
       
      Parameters:
      maxHierarchyLevel - the maximized allowed RBAC hierarchy level.
      matchingFunc - a matcher for supporting pattern in g
      domainMatchingFunc - a matcher for supporting domain pattern in g
  • Method Details

    • addMatchingFunc

      public void addMatchingFunc(String name, BiPredicate<String,String> matchingFunc)
      addMatchingFunc support use pattern in g.
      Parameters:
      name - the name of the matching function.
      matchingFunc - the matching function.
    • addDomainMatchingFunc

      public void addDomainMatchingFunc(String name, BiPredicate<String,String> domainMatchingFunc)
      addDomainMatchingFunc support use domain pattern in g
      Parameters:
      name - the name of the domain matching function.
      domainMatchingFunc - the domain matching function.
    • rebuild

      private void rebuild()
    • match

      boolean match(String str, String pattern)
    • getRole

      Role getRole(String name)
    • removeRole

      void removeRole(String name)
    • copyFrom

      void copyFrom(DefaultRoleManager other)
    • clear

      public void clear()
      clear clears all stored data and resets the role manager to the initial state.
      Specified by:
      clear in interface RoleManager
    • addLink

      public void addLink(String name1, String name2, String... domain)
      addLink adds the inheritance link between role: name1 and role: name2. aka role: name1 inherits role: name2. domain is a prefix to the roles.
      Specified by:
      addLink in interface RoleManager
      Parameters:
      name1 - the first role (or user).
      name2 - the second role.
      domain - the domain the roles belong to.
    • deleteLink

      public void deleteLink(String name1, String name2, String... domain)
      deleteLink deletes the inheritance link between role: name1 and role: name2. aka role: name1 does not inherit role: name2 any more. domain is a prefix to the roles.
      Specified by:
      deleteLink in interface RoleManager
      Parameters:
      name1 - the first role (or user).
      name2 - the second role.
      domain - the domain the roles belong to.
    • hasLink

      public boolean hasLink(String name1, String name2, String... domain)
      hasLink determines whether role: name1 inherits role: name2. domain is a prefix to the roles.
      Specified by:
      hasLink in interface RoleManager
      Parameters:
      name1 - the first role (or a user).
      name2 - the second role.
      domain - the domain the roles belong to.
      Returns:
      whether name1 inherits name2 (name1 has role name2).
    • hasLinkHelper

      private boolean hasLinkHelper(String targetName, Map<String,Role> roles, int level)
    • getRoles

      public List<String> getRoles(String name, String... domain)
      getRoles gets the roles that a subject inherits. domain is a prefix to the roles.
      Specified by:
      getRoles in interface RoleManager
      Parameters:
      name - the user (or a role).
      domain - the domain the roles belong to.
      Returns:
      the roles.
    • getUsers

      public List<String> getUsers(String name, String... domain)
      getUsers gets the users that inherits a subject.
      Specified by:
      getUsers in interface RoleManager
      Parameters:
      name - the role.
      domain - is a prefix to the users (can be used for other purposes).
      Returns:
      the users.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • printRoles

      public void printRoles()
      printRoles prints all the roles to log.
      Specified by:
      printRoles in interface RoleManager