Policy Storage
Policy storage in Casbin is handled by adapters. The enforcer loads and (optionally) saves policy through the adapter API.
Loading policy from a CSV file
Using a CSV file is the simplest option and works well for development and examples.
Example: examples/rbac_policy.csv
p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
หมายเหตุ
If a field contains commas, wrap it in double quotes:
p, alice, "data1,data2", read --correct
p, alice, data1,data2, read --incorrect (the whole phrase "data1,data2" should be wrapped in double quotes)
If a field contains both commas and double quotes, wrap it in double quotes and escape internal quotes by doubling them:
p, alice, data, "r.act in (""get"", ""post"")" --correct
p, alice, data, "r.act in ("get", "post")" --incorrect (you should use "" to escape "")
See casbin#886.
Adapter API
| วิธีการ | Type | Description |
|---|---|---|
| LoadPolicy() | basic | โหลดกฎนโยบายทั้งหมดจากที่เก็บข้อมูล |
| SavePolicy() | basic | บันทึกกฎนโยบายทั้งหมดไปยังที่เก็บข้อมูล |
| AddPolicy() | ไม่บังคับ | เพิ่มกฎนโยบายไปยังที่เก็บข้อมูล |
| RemovePolicy() | ไม่บังคับ | ลบกฎนโยบายออกจากที่เก็บข้อมูล |
| RemoveFilteredPolicy() | ไม่บังคับ | ลบกฎนโยบายที่ตรงกับตัวกรองออกจากที่เก็บข้อมูล |
รูปแบบการจัดเก็บข้อมูลในฐานข้อมูล
Example policy (CSV)
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, admin
Typical database layout (e.g. MySQL)
| id | ptype | v0 | v1 | v2 | v3 | v4 | v5 |
|---|---|---|---|---|---|---|---|
| 1 | p | data2_admin | data2 | read | |||
| 2 | p | data2_admin | data2 | write | |||
| 3 | g | alice | admin |
Columns
- id — Primary key (adapter-specific; not part of Casbin policy).
- ptype — Policy type:
p,g,g2, etc. - v0–v5 — Policy fields in order (left to right in CSV). Most adapters use 6 columns; some support more. Check the adapter docs if you need extra fields.
For full adapter API and design details, see Adapters.
Comments are powered by giscus and GitHub Discussions. They load from giscus.app and GitHub only after you click the button below.