跳转至主要内容

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.

  • Go
  • Node.js
    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:

  • Go
  • Node.js
    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).

  • Go
  • Node.js
  • Python
    rm := e.GetRoleManager()

GetNamedRoleManager

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

  • Go
  • Node.js
  • Python
    rm := e.GetNamedRoleManager("g2")

SetRoleManager

Sets the role manager for the default grouping policy type.

  • Go
  • Node.js
  • Python
    e.SetRoleManager(rm)

SetNamedRoleManager

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

  • Go
  • Python
    rm := e.SetNamedRoleManager("g2", rm)

Clear

Clears all role/link data in the role manager.

  • Go
  • Node.js
  • Python
    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).

  • Go
  • Node.js
  • Python
    rm.AddLink("u1", "g1", "domain1")

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

  • Go
  • Node.js
  • Python
    rm.DeleteLink("u1", "g1", "domain1")

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

  • Go
  • Node.js
  • Python
    rm.HasLink("u1", "g1", "domain1")

GetRoles

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

  • Go
  • Node.js
  • Python
    rm.GetRoles("u1", "domain1")

GetUsers

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

  • Go
  • Node.js
  • Python
    rm.GetUsers("g1")

PrintRoles

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

  • Go
  • Node.js
  • Python
    rm.PrintRoles()

SetLogger

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

  • Go
    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.)

  • Go
    result, err := rm.GetDomains(name)

Morty Proxy This is a proxified and sanitized view of the page, visit original site.