Skip to main content

RoleManager API

RoleManager

The RoleManager provides an interface for defining operations to manage roles. The addition of a matching function to the RoleManager allows the use of wildcards in role names and domains.

AddNamedMatchingFunc()

The AddNamedMatchingFunc function adds a MatchingFunc by Ptype to the RoleManager. The MatchingFunc will be used when performing role matching.

    e.AddNamedMatchingFunc("g", "", util.KeyMatch)
_, _ = e.AddGroupingPolicies([][]string{{"*", "admin", "domain1"}})
_, _ = e.GetRoleManager().HasLink("bob", "admin", "domain1") // -> true, nil

For example:

    e, _ := casbin.NewEnforcer("path/to/model", "path/to/policy")
e.AddNamedMatchingFunc("g", "", util.MatchKey)

AddNamedDomainMatchingFunc()

The AddNamedDomainMatchingFunc function adds a MatchingFunc by Ptype to the RoleManager. The DomainMatchingFunc is similar to the MatchingFunc listed above.

For example:

    e, _ := casbin.NewEnforcer("path/to/model", "path/to/policy")
e.AddNamedDomainMatchingFunc("g", "", util.MatchKey)

GetRoleManager()

The GetRoleManager function gets the current role manager for g.

For example:

    rm := e.GetRoleManager()

GetNamedRoleManager()

The GetNamedRoleManager function gets the role manager by named Ptype.

For example:

    rm := e.GetNamedRoleManager("g2")

SetRoleManager()

The SetRoleManager function sets the current role manager for g.

For example:

    e.SetRoleManager(rm)

SetNamedRoleManager()

The SetNamedRoleManager function sets the role manager by named Ptype.

For example:

    rm := e.SetNamedRoleManager("g2", rm)

Clear()

The Clear function clears all stored data and resets the role manager to its initial state.

For example:

    rm.Clear()

AddLink adds the inheritance link between two roles. role: name1 and role: name2. Domain is a prefix to the roles (can be used for other purposes).

For example:

    rm.AddLink("u1", "g1", "domain1")

DeleteLink deletes the inheritance link between two roles. role: name1 and role: name2. Domain is a prefix to the roles (can be used for other purposes).

For example:

    rm.DeleteLink("u1", "g1", "domain1")

HasLink determines whether a link exists between two roles. role: name1 inherits role: name2. Domain is a prefix to the roles (can be used for other purposes).

For example:

    rm.HasLink("u1", "g1", "domain1")

GetRoles()

GetRoles gets the roles that a user inherits. Domain is a prefix to the roles (can be used for other purposes).

For example:

    rm.GetRoles("u1", "domain1")

GetUsers()

GetUsers gets the users that inherits a role. Domain is a prefix to the users (can be used for other purposes).

For example:

    rm.GetUsers("g1")

PrintRoles()

PrintRoles prints all the roles to log.

For example:

    rm.PrintRoles()

SetLogger()

SetLogger sets role manager's logger.

For example:

    logger := log.DefaultLogger{}
logger.EnableLog(true)
rm.SetLogger(&logger)
_ = rm.PrintRoles()

GetDomains()

GetDomains gets domains that a user has

For example:

    result, err := rm.GetDomains(name)