RoleManager API

RoleManager

RoleManager provides interface to define the operations for managing roles. Adding matching function to rolemanager allows using wildcards in role name and domain.

AddNamedMatchingFunc()

AddNamedMatchingFunc add MatchingFunc by ptype RoleManager. MatchingFunc将在操作角色匹配时工作。

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

例如:

Go

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

AddNamedDomainMatchingFunc 通过 ptype 把MatchingFunc 添加到 RoleManager中。 DomainMatchingFunc 类似于上面列出的 MatchingFunc

例如:

Go

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

GetRoleManager 获取现存的 g 的role manager。

例如:

Go

rm := e.GetRoleManager()
Clear()

Clear清除所有存储的数据并将角色管理器重置到初始状态。

例如:

Go

rm.Clear()
AddLink()

AddLink添加了两个角色之间的继承链接。 角色: 名称1 和 角色: 名称2 Domain is a prefix to the roles (can be used for other purposes).

例如:

Go

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

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

例如:

Go

rm := DeleteLink("u1", "g1", "domain1")
HasLink()

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

例如:

Go

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

例如:

Go

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

例如:

Go

rm.GetUsers("g1")
PrintRoles()

PrintRoles prints all the roles to log.

例如:

Go

rm.PrintRoles()
SetLogger()

SetLogger设置角色管理器的日志。

例如:

Go

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

GetDomains gets domains that a user has

For example:

Go

result, err := rm.GetDomains(name)
最后编辑: kuteng  文档更新时间: 2021-06-22 19:21   作者:kuteng