Zum Hauptinhalt springen

Role Manager API

The RoleManager interface defines how role hierarchy and user–role links are stored and queried. You can attach matching functions so that wildcards (e.g. *) in role or domain names are supported. Below, e is an Enforcer and rm is a RoleManager.

RoleManager API

AddNamedMatchingFunc

Registers a matching function for a policy type (e.g. g). Used when evaluating role links so that patterns like * match multiple names.

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

AddNamedDomainMatchingFunc

Registers a matching function for the domain field in role links (e.g. in g with three arguments). Enables wildcards in domain names.

Example:

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

GetRoleManager

Returns the role manager used for the default grouping policy type (e.g. g).

    rm := e.GetRoleManager()

GetNamedRoleManager

Returns the role manager for a specific policy type (e.g. g2).

    rm := e.GetNamedRoleManager("g2")

SetRoleManager

Sets the role manager for the default grouping policy type.

    e.SetRoleManager(rm)

SetNamedRoleManager

Sets the role manager for a specific policy type (e.g. g2).

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

Clear

Clears all role/link data in the role manager.

    rm.Clear()

Adds a link so that name1 has role name2 in the given domain (or with the domain as a prefix, depending on the model).

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

Removes the link between name1 and name2 in the given domain.

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

Returns whether name1 has role name2 (directly or by inheritance) in the given domain.

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

GetRoles

Returns all roles the user has in the given domain (including inherited).

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

GetUsers

Returns all users that have the given role (in the default domain, if applicable).

    rm.GetUsers("g1")

PrintRoles

Prints all roles to the role manager’s logger (for debugging).

    rm.PrintRoles()

SetLogger

Sets the logger used by the role manager (e.g. for PrintRoles).

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

GetDomains

Returns the domains in which the user has at least one role. (Go only; signature may vary.)

    result, err := rm.GetDomains(name)