Casbin

Casbin

  • 文档
  • API
  • 编辑器
  • JetBrains 插件
  • Dashboard
  • 论坛
  • OA
  • 趋势
  • 云服务
  • 帮助
  • 博客
  • Languages icon中文
    • English
    • 한국어
    • 参与翻译
  • GitHub

›Model

基础知识

  • 概述
  • 开始使用
  • 工作原理
  • 教程

Model

  • 支持的Models
  • Model语法
  • 函数
  • 基于角色的访问控制
  • 域内RBAC
  • ABAC

存储

  • Model存储
  • Policy存储
  • 政策子集加载

扩充功能

  • 适配器
  • 观察者
  • 调度器
  • 角色管理器
  • 中间件

API

  • 管理 API
  • RBAC API

高级用法。

  • 多线程
  • 基准测试

管理

  • 管理员门户
  • Casbin 服务
  • 日志 & 错误处理
  • 在线编辑器
  • Frontend Usage

更多

  • 本项目使用者
  • 隐私政策
  • 服务条款
Translate

基于角色的访问控制

角色定义

[role_definition] 是RBAC角色继承关系的定义。 Casbin 支持 RBAC 系统的多个实例, 例如, 用户可以具有角色及其继承关系, 资源也可以具有角色及其继承关系。 这两个 RBAC 系统不会互相干扰。

此部分是可选的。 如果在模型中不使用 RBAC 角色, 则省略此部分。

[role_definition]
g = _, _
g2 = _, _

上述角色定义表明, g 是一个 RBAC系统, g2 是另一个 RBAC 系统。 _, _表示角色继承关系的前项和后项,即前项继承后项角色的权限。 一般来讲,如果您需要进行角色和用户的绑定,直接使用g 即可。 You can also use g and g2 when you need roles (or groups) on both users and resources. 请参见 rbac_model 和 rbac_model_with_resource_roles 的示例。

在Casbin里,我们以policy表示中实际的用户角色映射关系 (或是资源-角色映射关系),例如:

p, data2_admin, data2, read
g, alice, data2_admin

这意味着 alice 是角色 data2_admin的一个成员。 alice 在这里可以是用户、资源或角色。 Cabin 只是将其识别为一个字符串。

接下来在matcher中,应该像下面的例子一样检查角色信息:

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

It means sub in the request should have the role sub in the policy.

note
  1. Casbin 只存储用户角色的映射关系。
  2. Cabin 没有验证用户是否是有效的用户,或者角色是一个有效的角色。 这应该通过认证来解决。
  3. RBAC 系统中的用户名称和角色名称不应相同。因为Casbin将用户名和角色识别为字符串, 所以当前语境下Casbin无法得出这个字面量到底指代用户 alice 还是角色 alice。 这时,使用明确的 role_alice ,问题便可迎刃而解。
  4. 假设A具有角色 B,B 具有角色 C,并且 A 有角色 C。 这种传递性在当前版本会造成死循环。

角色层次

Casbin 的 RBAC 支持 RBAC1 的角色层次结构功能,如果 alice具有role1, role1具有role2,则 alice 也将拥有 role2 并继承其权限。

下面是一个称为层次结构级别的概念。 因此, 此示例的层次结构级别为2。 对于Casbin中的内置角色管理器, 可以指定最大层次结构级别。 默认值为10。 这意味着终端用户 alice 只能继承10个级别的角色。

// NewRoleManager is the constructor for creating an instance of the
// default RoleManager implementation.
func NewRoleManager(maxHierarchyLevel int) rbac.RoleManager {
    rm := RoleManager{}
    rm.allRoles = &sync.Map{}
    rm.maxHierarchyLevel = maxHierarchyLevel
    rm.hasPattern = false

    return &rm
}

如何区分用户和角色?

在RBAC中,Casbin不对用户和角色进行区分。 它们都被视为字符串。 如果你只使用单层的RBAC模型(角色不会成为另一个角色的成员)。 可以使用 e.GetAllSubjects() 获取所有用户,e.GetAllRoles() 获取所有角色。 它们会为规则 g, u, r 分别列出所有的 u 和 r。

But if you are using multi-level RBAC (with role hierarchy), and your application doesn't record whether a name (string) is a user or a role, or you have user and role with same name. 可以给角色加上像 role::admin 的前缀再传递到Casbin中。 由此可以通过查看前缀来区分用户和角色。

如何查询隐性角色或权限?

当用户通过RBAC层次结构继承角色或权限,而不是直接在策略规则中分配它们时,我们将这种类型的分配称为 implicit。 要查询这种隐式关系,需要使用以下两个api: GetImplicitRolesForUser()以及 GetImplicitPermissionsForUser 替代GetRolesForUser() 以及 GetPermissionsForUser. 有关详情,请参阅 this GitHub issue。

在 RBAC 中使用模式匹配

Sometimes, you want some subjects, object or domains/tenants with the specific pattern to be automatically granted to a role. RBAC中的模式匹配函数可以帮助做到这一点。 模式匹配函数与前一个函数共享相同的参数和返回值:matcher function。

The pattern matching function supports each parameter of g.

We know that normally RBAC is expressed as g(r.sub, p.sub) in matcher. Then we will use policy like:

p, alice, book_group, read
g, /book/1, book_group
g, /book/2, book_group

So alice can read all books including book 1 and book 2. But there can be thousands of books and it's very tedious to add each book to the book role (or group) with one g policy rule.

But with pattern matching functions, you can write the policy with only one line:

g, /book/:id, book_group

Casbin will automatically match /book/1 and /book/2 into pattern /book/:id for you. You only need to register the function with the enforcer like:

r := e.GetRoleManager()
r.(*defaultrolemanager.RoleManager).AddMatchingFunc("KeyMatch2",util.KeyMatch2)

When Using a pattern matching function in domains/tenants, You need to register the function to enforcer and model.

register keyMatch2 to enforcer:

r := e.GetRoleManager()
r.(*defaultrolemanager.RoleManager).AddDomainMatchingFunc("KeyMatch2",util.KeyMatch2)

register keyMatch2 to model:

m = g(r.sub, p.sub, r.dom) && keyMatch2(r.dom, p.dom) && r.obj == p.obj && r.act == p.act

You can see the full example here.

角色管理器

The role manager is used to manage the RBAC role hierarchy (user-role mapping) in Casbin. A role manager can retrieve the role data from Casbin policy rules or external sources such as LDAP, Okta, Auth0, Azure AD, etc. We support different implementations of a role manager. To keep light-weight, we don't put role manager code in the main library (except the default role manager). A complete list of Casbin role managers is provided as below. Any 3rd-party contribution on a new role manager is welcomed, please inform us and I will put it in this list:)

角色管理器作者描述
Default Role Manager (built-in)Casbin支持存储在Casbin策略中的角色层次结构
Session Role ManagerEDOMO Systems支持存储在Casbin策略中的角色层次结构,以及基于时间范围的会话
Okta Role ManagerCasbin支持存储在Okta中的角色层次结构
Auth0 Role ManagerCasbin支持存储在 Auth0's Authorization Extension 授权扩展名中的角色层次结构

For developers: all role managers must implement the RoleManager interface. Session Role Manager can be used as a reference implementation.

← 函数域内RBAC →
  • 角色定义
  • 角色层次
  • 如何区分用户和角色?
  • 如何查询隐性角色或权限?
  • 在 RBAC 中使用模式匹配
  • 角色管理器
Casbin
Docs
Getting StartedManagement APIRBAC APIMiddlewares
Community
Who's using Casbin?ForumStack OverflowProject Chat
Casbin          jCasbin
Node-Casbin   PHP-CasbinPyCasbin          Casbin.NETCasbin-CPP        Casbin-RS
Follow @CasbinNews
Copyright © 2021 Casbin contributors.