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

ポリシーストレージ

Casbin では、ポリシーストレージは アダプター として実装されています。

Loading policy from a .CSV file

これはCasbinを使用する最も一般的な方法です。 It is easy to understand for beginners and convenient for sharing when you ask the Casbin team for help.

The content of the .CSV file examples/rbac_policy.csv is as follows:

p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
note

If your file contains commas, you should wrap them in double quotes. For example:

p, alice, "data1,data2", read    --correct
p, alice, data1,data2, read --incorrect (the whole phrase "data1,data2" should be wrapped in double quotes)

If your file contains commas and double quotes, you should enclose the field in double quotes and double any embedded double quotes.

p, alice, data, "r.act in (""get"", ""post"")"        --correct
p, alice, data, "r.act in ("get", "post")" --incorrect (you should use "" to escape "")

関連する問題: casbin#886

アダプター API

方法タイプ説明
LoadPolicy()基本ストレージからすべてのポリシールールを読み込む
SavePolicy()基本すべてのポリシールールをストレージに保存
AddPolicy()省略可能ストレージにポリシールールを追加
削除ポリシー()省略可能ストレージからポリシールールを削除
RemoveFilterPolicy()省略可能ストレージからフィルタに一致するポリシールールを削除

データベースストレージフォーマット

Your policy file

p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, admin

Corresponding database structure (such as MySQL)

idptypev0v1v2v3v4v5
1pdata2_admindata2既読にする
2pdata2_admindata2書き込み
3galice管理者

Meaning of each column

  • id: The primary key in the database. It does not exist as part of the casbin policy. The way it is generated depends on the specific adapter.

  • ptype: p, g, g2等に対応する。

  • v0-v5: The column names have no specific meaning and correspond to the values in the policy csv from left to right. 列の数は、自分で定義する数によって決まります。 In theory, there can be an infinite number of columns, but generally only 6 columns are implemented in the adapter. If this is not enough for you, please submit an issue to the corresponding adapter repository.

アダプターの詳細

For more details about the use of the adapter API and database table structure design, please visit: /docs/adapters