Хранилище политики
В Касбине хранилище политики реализовано как адаптер.
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)
id | ptype | х0 | в1 | в2 | v3 | v4 | v5 |
---|---|---|---|---|---|---|---|
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 thecasbin 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 thepolicy 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