RBAC with Domains API
A more user-friendly API for RBAC with domains. This API is a subset of the Management API. RBAC users can use this API to simplify their code.
Reference
The global variable e
represents the Enforcer instance.
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e, err := NewEnforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv")
const e = await newEnforcer('examples/rbac_with_domains_model.conf', 'examples/rbac_with_domains_policy.csv')
$e = new Enforcer('examples/rbac_with_domains_model.conf', 'examples/rbac_with_domains_policy.csv');
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv")
var e = new Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv");
let mut e = Enforcer::new("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv").await?;
Enforcer e = new Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv");
GetUsersForRoleInDomain()
The GetUsersForRoleInDomain()
function retrieves the users that have a role within a domain.
For example:
- Go
- Node.js
- Python
res := e.GetUsersForRoleInDomain("admin", "domain1")
const res = e.getUsersForRoleInDomain("admin", "domain1")
res = e.get_users_for_role_in_domain("admin", "domain1")
GetRolesForUserInDomain()
The GetRolesForUserInDomain()
function retrieves the roles that a user has within a domain.
For example:
- Go
- Node.js
- Python
- Java
res := e.GetRolesForUserInDomain("admin", "domain1")
const res = e.getRolesForUserInDomain("alice", "domain1")
res = e.get_roles_for_user_in_domain("alice", "domain1")
List<String> res = e.getRolesForUserInDomain("admin", "domain1");
GetPermissionsForUserInDomain()
The GetPermissionsForUserInDomain()
function retrieves the permissions for a user or role within a domain.
For example:
- Go
- Java
res := e.GetPermissionsForUserInDomain("alice", "domain1")
List<List<String>> res = e.getPermissionsForUserInDomain("alice", "domain1");
AddRoleForUserInDomain()
The AddRoleForUserInDomain()
function adds a role for a user within a domain. It returns false
if the user already has the role (no changes made).
For example:
- Go
- Python
- Java
ok, err := e.AddRoleForUserInDomain("alice", "admin", "domain1")
ok = e.add_role_for_user_in_domain("alice", "admin", "domain1")
boolean ok = e.addRoleForUserInDomain("alice", "admin", "domain1");
DeleteRoleForUserInDomain()
The DeleteRoleForUserInDomain()
function removes a role for a user within a domain. It returns false
if the user does not have the role (no changes made).
For example:
- Go
- Java
ok, err := e.DeleteRoleForUserInDomain("alice", "admin", "domain1")
boolean ok = e.deleteRoleForUserInDomain("alice", "admin", "domain1");
DeleteRolesForUserInDomain()
The DeleteRolesForUserInDomain()
function removes all roles for a user within a domain. It returns false
if the user does not have any roles (no changes made).
For example:
- Go
ok, err := e.DeleteRolesForUserInDomain("alice", "domain1")
GetAllUsersByDomain()
The GetAllUsersByDomain()
function retrieves all users associated with the given domain. It returns an empty string array if no domain is defined in the model.
For example:
- Go
res := e.GetAllUsersByDomain("domain1")
DeleteAllUsersByDomain()
The DeleteAllUsersByDomain()
function deletes all users associated with the given domain. It returns false
if no domain is defined in the model.
For example:
- Go
ok, err := e.DeleteAllUsersByDomain("domain1")
DeleteDomains()
DeleteDomains would delete all associated users and roles. It would delete all domains if parameter is not provided.
For example:
- Go
ok, err := e.DeleteDomains("domain1", "domain2")
GetAllDomains()
GetAllDomains would get all domains.
For example:
- Go
res, _ := e.GetAllDomains()
If you are handling a domain like name::domain
, it may lead to unexpected behavior. In Casbin, ::
is a reserved keyword, just like for
, if
in a programming language, we should never put ::
in a domain.
GetAllRolesByDomain()
GetAllRolesByDomain would get all roles associated with the domain.
For example:
- Go
res := e.GetAllRolesByDomain("domain1")
This method does not apply to domains that have an inheritance relationship, also known as implicit roles.
GetImplicitUsersForResourceByDomain()
GetImplicitUsersForResourceByDomain return implicit user based on resource and domain.
For example:
p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write
g, alice, admin, domain1
g, bob, admin, domain2
GetImplicitUsersForResourceByDomain("data1", "domain1") will return [["alice", "domain1", "data1", "read"],["alice", "domain1", "data1", "write"]], nil
- Go
ImplicitUsers, err := e.GetImplicitUsersForResourceByDomain("data1", "domain1")
Only users will be returned, roles (2nd arg in "g") will be excluded.