Richtlinien-Speicher
In Casbin wird der Richtlinienspeicher als -Adapter implementiert.
Loading policy from a .CSV file
Dies ist die häufigste Art Casbin zu benutzen. 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 "")
Ähnliches Problem: casbin#886
Adapter-API
Methode | Typ | Beschreibung |
---|---|---|
LoadPolicy() | einfach | Lade alle Regeln aus dem Speicher |
SavePolicy() | einfach | Alle Richtlinien-Regeln im Speicher speichern |
AddPolicy() | optional | Richtlinien-Regel zum Speicher hinzufügen |
RemovePolicy() | optional | Eine Richtlinien-Regel vom Speicher entfernen |
FilteredPolicy() entfernen | optional | Regeln, die dem Filter entsprechen, vom Speicher entfernen |
Datenbankspeicherformat
Your policy file
p, data2_admin, data2, lesen
p, data2_admin, data2, schreiben
g, alice, admin
Corresponding database structure (such as MySQL)
id | ptype | v0 | v1 | v2 | v3 | v4 | v5 |
---|---|---|---|---|---|---|---|
1 | p | data2_admin | data2 | gelesen | |||
2 | p | data2_admin | data2 | schreiben | |||
3 | g | alice | admin |
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
: Es entsprichtp
,g
,g2
, etc.v0-v5
: The column names have no specific meaning and correspond to the values in thepolicy csv
from left to right. Die Anzahl der Spalten hängt davon ab, wie viele Sie selbst definieren. 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.
Adapter-Details
For more details about the use of the adapter API and database table structure design, please visit: /docs/adapters