Ana içeriğe atla

RoleManager API

RoleManager

The RoleManager provides an interface for managing role operations. Adding a matching function to the RoleManager enables the use of wildcards in role names and domains.

AddNamedMatchingFunc()

AddNamedMatchingFunc registers a MatchingFunc for a specific policy type in the RoleManager. The MatchingFunc is applied during role matching.

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

Example usage:

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

AddNamedDomainMatchingFunc()

AddNamedDomainMatchingFunc registers a MatchingFunc for a specific policy type in the RoleManager. The DomainMatchingFunc works similarly to MatchingFunc.

Example usage:

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

GetRoleManager()

GetRoleManager retrieves the current role manager for g.

Example usage:

    rm := e.GetRoleManager()

GetNamedRoleManager()

GetNamedRoleManager retrieves the role manager by named policy type.

Example usage:

    rm := e.GetNamedRoleManager("g2")

SetRoleManager()

SetRoleManager assigns the current role manager for g.

Example usage:

    e.SetRoleManager(rm)

SetNamedRoleManager()

SetNamedRoleManager assigns the role manager by named policy type.

Example usage:

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

Clear()

Clear removes all stored data and resets the role manager to its initial state.

Example usage:

    rm.Clear()

AddLink creates an inheritance link between two roles: name1 and name2. The domain parameter serves as a prefix to the roles (can also be used for other purposes).

Example usage:

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

DeleteLink removes the inheritance link between two roles: name1 and name2. The domain parameter serves as a prefix to the roles (can also be used for other purposes).

Example usage:

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

HasLink checks whether a link exists between two roles, where name1 inherits from name2. The domain parameter serves as a prefix to the roles (can also be used for other purposes).

Example usage:

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

GetRoles()

GetRoles retrieves the roles that a user inherits. The domain parameter serves as a prefix to the roles (can also be used for other purposes).

Example usage:

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

GetUsers()

GetUsers retrieves the users that inherit a role. The domain parameter serves as a prefix to the users (can also be used for other purposes).

Example usage:

    rm.GetUsers("g1")

PrintRoles()

PrintRoles outputs all roles to the log.

Example usage:

    rm.PrintRoles()

SetLogger()

SetLogger assigns a logger to the role manager.

Example usage:

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

GetDomains()

GetDomains retrieves the domains that a user belongs to.

Example usage:

    result, err := rm.GetDomains(name)