Casbin
Api

RoleManager API

The RoleManager API 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.

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
    await e.addNamedMatchingFunc('g', Util.keyMatchFunc);
    await e.addGroupingPolicies([['*', 'admin', 'domain1']]);
    await e.getRoleManager().hasLink('bob', 'admin', 'domain1');

Example usage:

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

typescript const e = await newEnforcer('path/to/model', 'path/to/policy'); await e.addNamedMatchingFunc('g', Util.keyMatchFunc);

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)

typescript const e = await newEnforcer('path/to/model', 'path/to/policy'); await e.addNamedDomainMatchingFunc('g', Util.keyMatchFunc);

GetRoleManager()

GetRoleManager retrieves the current role manager for g.

Example usage:

    rm := e.GetRoleManager()

typescript const rm = await e.getRoleManager();

    rm = e.get_role_manager()

GetNamedRoleManager()

GetNamedRoleManager retrieves the role manager by named policy type.

Example usage:

    rm := e.GetNamedRoleManager("g2")

typescript const rm = await e.getNamedRoleManager("g2");

    rm = e.get_named_role_manager("g2")

SetRoleManager()

SetRoleManager assigns the current role manager for g.

Example usage:

    e.SetRoleManager(rm)

typescript e.setRoleManager(rm);

    rm = e.set_role_manager(rm)

SetNamedRoleManager()

SetNamedRoleManager assigns the role manager by named policy type.

Example usage:

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

Clear()

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

Example usage:

    rm.Clear()

typescript await rm.clear();

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

typescript await rm.addLink('u1', 'g1', 'domain1');

    rm.add_link("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")

typescript await rm.deleteLink('u1', 'g1', 'domain1');

    rm.delete_link("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")

typescript await rm.hasLink('u1', 'g1', 'domain1');

    rm.has_link("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")

typescript await rm.getRoles('u1', 'domain1');

    rm.get_roles("u1", "domain")

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

typescript await rm.getUsers('g1');

    rm.get_users("g1")

PrintRoles()

PrintRoles outputs all roles to the log.

Example usage:

    rm.PrintRoles()

typescript await rm.printRoles();

    rm.print_roles()

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)

On this page