跳转至主要内容

Policy的存储

在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
备注

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 "")

相关问题: issue#886

适配器 API

接口名类型描述
LoadPolicy()基本设置从持久层中加载policy规则
SavePolicy()基本设置将policy规则保存至持久层
AddPolicy()可选添加单条policy规则至持久层
RemovePolicy()可选从持久层删除单条policy规则
RemoveFilteredPolicy()可选从持久层删除符合筛选条件的policy规则

数据库存储格式

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可写
3galiceadmin

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