Casbin

Casbin

  • 文档
  • API
  • 编辑器
  • IDE 插件
  • Single Sign-On (SSO)
  • 论坛
  • 帮助
  • 博客
  • Pricing
  • Contact Sales
  • Languages icon中文
    • English
    • 한국어
    • Русский
    • Français
    • Deutsch
    • 日本語
    • 参与翻译
  • GitHub

›Model

基础知识

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

Model

  • 支持的Models
  • Model语法
  • 效果器
  • 函数
  • 基于角色的访问控制
  • RBAC with Pattern
  • 域内RBAC
  • Casbin RBAC v.s. RBAC96
  • ABAC
  • 优先级模型
  • 超级管理员

存储

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

扩充功能

  • Enforcers
  • 适配器
  • 观察者
  • 调度器
  • 角色管理器
  • 中间件
  • Graphql 中间件
  • 云端原生中间值

API

  • API 概述
  • 管理 API
  • RBAC API
  • 域内基于角色的访问控制 API
  • RoleManager API
  • 数据权限

高级用法

  • 多线程
  • 基准测试
  • 性能优化
  • Kubernetes的授权
  • 通过特使核准服务网格。

管理

  • 管理员门户
  • Casbin 服务
  • 日志 & 错误处理
  • 前端使用

编辑器

  • 在线编辑器
  • IDE 插件

更多

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

RBAC with Pattern

Quick Start

  • use pattern in g(_,_)

    e, _ := NewEnforcer("./example.conf", "./example.csv")
    e.AddNamedMatchingFunc("g","KeyMatch2",util.KeyMatch2)
    
  • use pattern with domain

    e.AddNamedDomainMatchingFunc("g","KeyMatch2",util.KeyMatch2)
    
  • use all pattern

    just combined use of two APIs

As shown above, after you create the enforcer instance, you need to activate pattern matching via AddNamedMatchingFunc and AddNamedMatchingFunc API, which determine how the pattern matches.

note

If you use the online editor, it specifies the pattern matching function in the lower left corner. editor-tips

Use pattern matching in RBAC

Sometimes, you want some subjects, object or domains/tenants with the specific pattern to be automatically granted to a role. Pattern matching functions in RBAC can help you do that. A pattern matching function shares the same parameters and return value as the previous 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:

Go
Node.js
e.AddNamedMatchingFunc("g","KeyMatch2",util.KeyMatch2)
await e.addNamedMatchingFunc('g', Util.keyMatch2Func);

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

Go
Node.js
e.AddNamedDomainMatchingFunc("g","KeyMatch2",util.KeyMatch2)
await e.addNamedDomainMatchingFunc('g', Util.keyMatch2Func);

If you don't understand what g(r.sub, p.sub, r.dom) means, please read rbac-with-domains. In short, g(r.sub, p.sub, r.dom) will check whether the user r.sub has a role p.sub in the domain r.dom. So this is how the matcher work. You can see the full example here.

Apart from the pattern matching syntax above, we can also use pure domain pattern.

For example, if we want sub to have access in different domains, domain1 and domain2, we can use the pure domain pattern:

p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write

g, alice, admin, *
g, bob, admin, domain2

In this example, we want alice to read and write data in domain1 and domain2, pattern matching * in g makes alice have the access to two domains.

By using pattern matching, especially in the scenarios which is more complicated and there are a lot of domains or objects we need to take into consideration, we can implement the policy_definition more elegant and effective.

← 基于角色的访问控制域内RBAC →
  • Quick Start
  • Use pattern matching in RBAC
Casbin
Docs
Getting StartedManagement APIRBAC APIMiddlewares
Community
Who's using Casbin?Stack Overflow
Casbin          jCasbin
Node-Casbin   PHP-CasbinPyCasbin          Casbin.NETCasbin-CPP        Casbin-RS
Follow @casbinHQ
Copyright © 2022 Casbin Organization