メインコンテンツにスキップ

RBAC

Role Definition

The [role_definition] is used to define the RBAC role inheritance relations. Casbin supports multiple instances of RBAC systems, where users can have roles and their inheritance relations, and resources can have roles and their inheritance relations too. These two RBAC systems won't interfere with each other.

このセクションは任意です。 モデルでRBACロールを使用しない場合は、このセクションを省略してください。

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

The above role definition shows that g is an RBAC system, and g2 is another RBAC system. _,_ means there are two parties involved in an inheritance relation. In the most common case, you usually use g alone if you only need roles for users. You can also use both g and g2 when you need roles (or groups) for both users and resources. 例については rbac_model.conf および rbac_model_with_resource_roles.conf を参照してください。

Casbin stores the actual user-role mapping (or resource-role mapping if you are using roles on resources) in the policy. For example:

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

It means that alice inherits/is a member of the role data2_admin. Here, alice can be a user, a resource, or a role. Casbinは文字列としてのみ認識します。

Then, in a matcher, you should check the role as shown below:

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

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

note
  1. Casbin にはユーザーのロールマッピングのみが保存されます。
  2. Casbin doesn't verify whether a user is a valid user or a role is a valid role. それは認証によって処理されるべきである。
  3. Do not use the same name for a user and a role inside an RBAC system, because Casbin recognizes users and roles as strings, and there's no way for Casbin to know whether you are specifying user alice or role alice. role_alice を使用するだけで解決できます。
  4. If A has role B, and B has role C, then A has role C. この遷移は今のところ無限です。
Token Name Convention

Conventionally, the subject token name in the policy definition is sub and placed at the beginning. Now, Golang Casbin supports customized token names and places. If the subject token name is sub, the subject token can be placed at an arbitrary place without any extra action needed. If the subject token name is not sub, e.SetFieldIndex() for constant.SubjectIndex should be called after the enforcer is initialized, regardless of its position.

# `subject` here is for sub
[policy_definition]
p = obj, act, subject
e.SetFieldIndex("p", constant.SubjectIndex, 2) // index starts from 0
ok, err := e.DeleteUser("alice") // without SetFieldIndex, it will raise an error

Role Hierarchy

Casbin's RBAC supports RBAC1's role hierarchy feature, which means that if alice has role1, and role1 has role2, then alice will also have role2 and inherit its permissions.

Here, we have a concept called a hierarchy level. So, in this example, the hierarchy level is 2. For the built-in role manager in Casbin, you can specify the maximum hierarchy level. デフォルト値は 10 です。 This means that an end user like alice can only inherit 10 levels of roles.

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

return &rm
}

How to Distinguish Role from User?

Casbin doesn't distinguish between roles and users in its RBAC. They are both treated as strings. If you only use a single-level RBAC (where a role will never be a member of another role), you can use e.GetAllSubjects() to get all users and e.GetAllRoles() to get all roles. They will list all u and all r, respectively, in all g, u, r rules.

But if you are using a 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 a user and a role with the same name, you can add a prefix to the role like role::admin before passing it to Casbin. This way, you will know if it's a role by checking this prefix.

How to Query Implicit Roles or Permissions?

When a user inherits a role or permission via RBAC hierarchy instead of being directly assigned them in a policy rule, we call this type of assignment "implicit". To query such implicit relations, you need to use these two APIs: GetImplicitRolesForUser() and GetImplicitPermissionsForUser() instead of GetRolesForUser() and GetPermissionsForUser(). 詳細については、 この GitHub Issue を参照してください。

Using Pattern Matching in RBAC

パターン付きRBAC を参照

Role Manager

See the Role Managers section for details.