RBAC API
一个更友好的RBAC API。 此API是管理API的一个子集。 RBAC用户可以使用此API简化代码。
参考
全局变量e
是Enforcer实例。
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e, err := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv')
$e = new Enforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');
e = casbin.Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
var e = new Enforcer("path/to/model.conf", "path/to/policy.csv");
let mut e = Enforcer::new("examples/rbac_model.conf", "examples/rbac_policy.csv").await?;
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
GetRolesForUser()
GetRolesForUser获取用户拥有的角色。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
res := e.GetRolesForUser("alice")
const res = await e.getRolesForUser('alice')
$res = $e->getRolesForUser("alice");
roles = e.get_roles_for_user("alice")
var res = e.GetRolesForUser("alice");
let roles = e.get_roles_for_user("alice", None); // No domain
List<String> res = e.getRolesForUser("alice");
GetUsersForRole()
GetUsersForRole获取拥有某个角色的用户。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
res := e.GetUsersForRole("data1_admin")
const res = await e.getUsersForRole('data1_admin')
$res = $e->getUsersForRole("data1_admin");
users = e.get_users_for_role("data1_admin")
var res = e.GetUsersForRole("data1_admin");
let users = e.get_users_for_role("data1_admin", None); // No domain
List<String> res = e.getUsersForRole("data1_admin");
HasRoleForUser()
HasRoleForUser确定用户是否拥有某个角色。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
res := e.HasRoleForUser("alice", "data1_admin")
const res = await e.hasRoleForUser('alice', 'data1_admin')
$res = $e->hasRoleForUser("alice", "data1_admin");
has = e.has_role_for_user("alice", "data1_admin")
var res = e.HasRoleForUser("alice", "data1_admin");
let has = e.has_role_for_user("alice", "data1_admin", None); // No domain
boolean res = e.hasRoleForUser("alice", "data1_admin");
AddRoleForUser()
AddRoleForUser为用户添加一个角色。 如果用户已经拥有该角色(即没有影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.AddRoleForUser("alice", "data2_admin")
await e.addRoleForUser('alice', 'data2_admin')
$e->addRoleForUser("alice", "data2_admin");
e.add_role_for_user("alice", "data2_admin")
var added = e.AddRoleForUser("alice", "data2_admin");
or
var added = await e.AddRoleForUserAsync("alice", "data2_admin");
let added = e.add_role_for_user("alice", "data2_admin", None).await?; // No domain
boolean added = e.addRoleForUser("alice", "data2_admin");
AddRolesForUser()
AddRolesForUser为用户添加多个角色。 如果用户已经拥有这些角色中的一个(即没有影响),则返回false。
例如:
- Go
- Node.js
- Rust
var roles = []string{"data2_admin", "data1_admin"}
e.AddRolesForUser("alice", roles)
const roles = ["data1_admin", "data2_admin"];
roles.map((role) => e.addRoleForUser("alice", role));
let roles = vec!["data1_admin".to_owned(), "data2_admin".to_owned()];
let all_added = e.add_roles_for_user("alice", roles, None).await?; // No domain
DeleteRoleForUser()
DeleteRoleForUser删除用户的一个角色。 如果用户没有该角色(即没有影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeleteRoleForUser("alice", "data1_admin")
await e.deleteRoleForUser('alice', 'data1_admin')
$e->deleteRoleForUser("alice", "data1_admin");
e.delete_role_for_user("alice", "data1_admin")
var deleted = e.DeleteRoleForUser("alice", "data1_admin");
or
var deleted = await e.DeleteRoleForUser("alice", "data1_admin");
let deleted = e.delete_role_for_user("alice", "data1_admin", None).await?; // No domain
boolean deleted = e.deleteRoleForUser("alice", "data1_admin");
DeleteRolesForUser()
DeleteRolesForUser删除用户的所有角色。 如果用户没有任何角色(即没有影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeleteRolesForUser("alice")
await e.deleteRolesForUser('alice')
$e->deleteRolesForUser("alice");
e.delete_roles_for_user("alice")
var deletedAtLeastOne = e.DeleteRolesForUser("alice");
or
var deletedAtLeastOne = await e.DeleteRolesForUserAsync("alice");
let deleted_at_least_one = e.delete_roles_for_user("alice", None).await?; // No domain
boolean deletedAtLeastOne = e.deleteRolesForUser("alice");
DeleteUser()
DeleteUser删除一个用户。 如果用户不存在(即没有影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeleteUser("alice")
await e.deleteUser('alice')
$e->deleteUser("alice");
e.delete_user("alice")
var deleted = e.DeleteUser("alice");
or
var deleted = await e.DeleteUserAsync("alice");
let deleted = e.delete_user("alice").await?;
boolean deleted = e.deleteUser("alice");
DeleteRole()
DeleteRole删除一个角色。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeleteRole("data2_admin")
await e.deleteRole("data2_admin")
$e->deleteRole("data2_admin");
e.delete_role("data2_admin")
var deleted = e.DeleteRole("data2_admin");
or
var deleted = await e.DeleteRoleAsync("data2_admin");
let deleted = e.delete_role("data2_admin").await?;
e.deleteRole("data2_admin");
DeletePermission()
DeletePermission删除一个权限。 如果权限不存在(即未受影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeletePermission("read")
await e.deletePermission('read')
$e->deletePermission("read");
e.delete_permission("read")
var deleted = e.DeletePermission("read");
or
var deleted = await e.DeletePermissionAsync("read");
let deleted = e.delete_permission(vec!["read".to_owned()]).await?;
boolean deleted = e.deletePermission("read");
AddPermissionForUser()
AddPermissionForUser为用户或角色添加一个权限。 如果用户或角色已经拥有该权限(即未受影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.AddPermissionForUser("bob", "read")
await e.addPermissionForUser('bob', 'read')
$e->addPermissionForUser("bob", "read");
e.add_permission_for_user("bob", "read")
var added = e.AddPermissionForUser("bob", "read");
or
var added = await e.AddPermissionForUserAsync("bob", "read");
let added = e.add_permission_for_user("bob", vec!["read".to_owned()]).await?;
boolean added = e.addPermissionForUser("bob", "read");
AddPermissionsForUser()
AddPermissionsForUser为用户或角色添加多个权限。 如果用户或角色已经拥有其中一个权限(即未受影响),则返回false。
例如:
- Go
- Node.js
- Rust
var permissions = [][]string{{"data1", "read"},{"data2","write"}}
for i := 0; i < len(permissions); i++ {
e.AddPermissionsForUser("alice", permissions[i])
}
const permissions = [
["data1", "read"],
["data2", "write"],
];
permissions.map((permission) => e.addPermissionForUser("bob", ...permission));
let permissions = vec![
vec!["data1".to_owned(), "read".to_owned()],
vec!["data2".to_owned(), "write".to_owned()],
];
let all_added = e.add_permissions_for_user("bob", permissions).await?;
DeletePermissionForUser()
DeletePermissionForUser删除用户或角色的一个权限。 如果用户或角色没有该权限(即未受影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeletePermissionForUser("bob", "read")
await e.deletePermissionForUser("bob", "read")
$e->deletePermissionForUser("bob", "read");
e.delete_permission_for_user("bob", "read")
var deleted = e.DeletePermissionForUser("bob", "read");
or
var deleted = await e.DeletePermissionForUserAsync("bob", "read");
let deleted = e.delete_permission_for_user("bob", vec!["read".to_owned()]).await?;
boolean deleted = e.deletePermissionForUser("bob", "read");
DeletePermissionsForUser()
DeletePermissionsForUser删除用户或角色的权限。 如果用户或角色没有任何权限(即未受影响),则返回false。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.DeletePermissionsForUser("bob")
await e.deletePermissionsForUser('bob')
$e->deletePermissionsForUser("bob");
e.delete_permissions_for_user("bob")
var deletedAtLeastOne = e.DeletePermissionsForUser("bob");
or
var deletedAtLeastOne = await e.DeletePermissionsForUserAsync("bob");
let deleted_at_least_one = e.delete_permissions_for_user("bob").await?;
boolean deletedAtLeastOne = e.deletePermissionForUser("bob");
GetPermissionsForUser()
GetPermissionsForUser获取用户或角色的权限。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Java
e.GetPermissionsForUser("bob")
await e.getPermissionsForUser('bob')
$e->getPermissionsForUser("bob");
e.get_permissions_for_user("bob")
var permissions = e.GetPermissionsForUser("bob");
List<List<String>> permissions = e.getPermissionsForUser("bob");
HasPermissionForUser()
HasPermissionForUser确定用户是否拥有一个权限。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.HasPermissionForUser("alice", []string{"read"})
await e.hasPermissionForUser('alice', 'read')
$e->hasPermissionForUser("alice", []string{"read"});
has = e.has_permission_for_user("alice", "read")
var has = e.HasPermissionForUser("bob", "read");
let has = e.has_permission_for_user("alice", vec!["data1".to_owned(), "read".to_owned()]);
boolean has = e.hasPermissionForUser("alice", "read");
GetImplicitRolesForUser()
GetImplicitRolesForUser获取用户拥有的隐式角色。 与GetRolesForUser()相比,此函数除了直接角色外,还检索间接角色。
例如:
g, alice, role:admin
g, role:admin, role:user
GetRolesForUser("alice")只能获取:["role:admin"]。\ 但是GetImplicitRolesForUser("alice")将获取:["role:admin", "role:user"]。
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.GetImplicitRolesForUser("alice")
await e.getImplicitRolesForUser("alice")
$e->getImplicitRolesForUser("alice");
e.get_implicit_roles_for_user("alice")
var implicitRoles = e.GetImplicitRolesForUser("alice");
e.get_implicit_roles_for_user("alice", None); // No domain
List<String> implicitRoles = e.getImplicitRolesForUser("alice");
GetImplicitUsersForRole()
GetImplicitUsersForRole获取继承该角色的所有用户。 与GetUsersForRole()相比,此函数检索间接用户。
例如:
g, alice, role:admin
g, role:admin, role:user
GetUsersForRole("role:user")只能获取:["role:admin"].\ 但是GetImplicitUesrsForRole("role:user")将获取:["role:admin", "alice"].
例如:
- Go
- Node.js
- Java
users := e.GetImplicitUsersForRole("role:user")
const users = e.getImplicitUsersForRole("role:user");
List<String> users = e.getImplicitUsersForRole("role:user");
GetImplicitPermissionsForUser()
GetImplicitPermissionsForUser获取用户或角色的隐式权限。\ 与GetPermissionsForUser()相比,此函数检索继承角色的权限。
例如:
p, admin, data1, read
p, alice, data2, read
g, alice, admin
GetPermissionsForUser("alice")只能获取:[["alice", "data2", "read"]].\ 但是GetImplicitPermissionsForUser("alice")将获取:[["admin", "data1", "read"], ["alice", "data2", "read"]].
例如:
- Go
- Node.js
- PHP
- Python
- .NET
- Rust
- Java
e.GetImplicitPermissionsForUser("alice")
await e.getImplicitPermissionsForUser("alice")
$e->getImplicitPermissionsForUser("alice");
e.get_implicit_permissions_for_user("alice")
var implicitPermissions = e.GetImplicitPermissionsForUser("alice");
e.get_implicit_permissions_for_user("alice", None); // No domain
List<List<String>> implicitPermissions = e.getImplicitPermissionsForUser("alice");
GetNamedImplicitPermissionsForUser()
GetNamedImplicitPermissionsForUser通过命名策略获取用户或角色的隐式权限。与GetImplicitPermissionsForUser()相比,此函数允许您指定策略名称。
例如:
p, admin, data1, read
p2, admin, create
g, alice, admin
GetImplicitPermissionsForUser("alice")只能获取:[["admin", "data1", "read"]],其策略默认为"p"
但是你可以通过GetNamedImplicitPermissionsForUser("p2","alice")指定策略为"p2"来获取:[["admin", "create"]]
例如:
- Go
- Python
e.GetNamedImplicitPermissionsForUser("p2","alice")
e.get_named_implicit_permissions_for_user("p2", "alice")
GetDomainsForUser()
GetDomainsForUser获取用户拥有的所有域。
例如:
p, admin, domain1, data1, read
p, admin, domain2, data2, read
p, admin, domain2, data2, write
g, alice, admin, domain1
g, alice, admin, domain2
GetDomainsForUser("alice")可以获取["domain1", "domain2"]
例如:
- Go
result, err := e.GetDomainsForUser("alice")
GetImplicitResourcesForUser()
GetImplicitResourcesForUser返回用户应为真的所有策略。
例如:
p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
GetImplicitResourcesForUser("alice")将返回[[alice data1 read] [alice data2 read] [alice data2 write]]
- Go
resources, err := e.GetImplicitResourcesForUser("alice")
GetImplicitUsersForPermission()
GetImplicitUsersForPermission获取权限的隐式用户。
例如:
p, admin, data1, read
p, bob, data1, read
g, alice, admin
GetImplicitUsersForPermission("data1", "read")将返回:["alice", "bob"]
.
注意:只会返回用户,角色("g"中的第二个参数)将被排除。
- Go
users, err := e.GetImplicitUsersForPermission("data1", "read")
GetAllowedObjectConditions()
GetAllowedObjectConditions返回用户可以访问的对象条件的字符串数组。
例如:
p, alice, r.obj.price < 25, read
p, admin, r.obj.category_id = 2, read
p, bob, r.obj.author = bob, write
g, alice, admin
e.GetAllowedObjectConditions("alice", "read", "r.obj.")
将返回["price < 25", "category_id = 2"], nil
注意:
前缀:您可以自定义对象条件的前缀,通常使用"r.obj."作为前缀。 移除前缀后,剩下的部分就是对象的条件。 如果有一个obj策略不满足前缀要求,将返回
errors.ERR_OBJ_CONDITION
。如果'objectConditions'数组为空,返回
errors.ERR_EMPTY_CONDITION
。这个错误是因为一些数据适配器的ORM在接收到空条件时默认返回全表数据,这往往与预期行为相反(例如GORM)。如果您使用的适配器不是这样的,您可以选择忽略这个错误。
- Go
conditions, err := e.GetAllowedObjectConditions("alice", "read", "r.obj.")
GetImplicitUsersForResource()
GetImplicitUsersForResource基于资源返回隐式用户。
例如:
p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
GetImplicitUsersForResource("data2")将返回[["bob", "data2", "write"], ["alice", "data2", "read"] ["alice", "data2", "write"]], nil
。
GetImplicitUsersForResource("data1")将返回[["alice", "data1", "read"]], nil
。
- Go
ImplicitUsers, err := e.GetImplicitUsersForResource("data2")
只会返回用户,角色("g"中的第二个参数)将被排除。