Перейти к основному контенту

Хранилище политики

В Касбине хранилище политики реализовано как адаптер.

Loading policy from a .CSV file

Это самый распространенный способ использования Касбина. 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()базовыйЗагрузить все правила политики из хранилища
Сохранение политики ()базовыйСохранить все правила политики в хранилище
AddPolicy()опциональноДобавить правило политики в хранилище
Удалить политику()опциональноУдалить правило политики из хранилища
УдалитьFilteredPolicy()опциональноУдалить правила политики, соответствующие фильтру

Формат хранилища данных

Your policy file

p, data2_admin, data2, чтение
p, data2_admin, data2, запись
g, alice, admin

Corresponding database structure (such as MySQL)

idptypeх0в1в2v3v4v5
1пdata2_adminданные2читать
2пdata2_adminданные2писать
3гальцеадмин

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