Passer au contenu principal

Stockage de la politique

En Casbin, le stockage des politiques est implémenté comme un adaptateur.

Loading policy from a .CSV file

C'est la façon la plus courante d'utiliser 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
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 "")

Problème lié : casbin#886

API de la carte

MéthodeType de texteDescription
format@@0 LoadPolicy()basiqueCharger toutes les règles de politique du stockage
EnregistrerPolicy()basiqueEnregistrer toutes les règles de politique sur le stockage
AddPolicy()optionnelAjouter une règle de politique au stockage
RemovePolicy()optionnelSupprimer une règle de politique du stockage
RemoveFilteredPolicy()optionnelSupprimer les règles de politique qui correspondent au filtre du stockage

Format de stockage de la base de données

Your policy file

p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, admin

Corresponding database structure (such as MySQL)

idptypeV0V1V2V3V4contre 5
1padministre_donnéesdonnées2Lu
2padministre_donnéesdonnées2écrire
3galiceAdministrateur

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: Il correspond à p, g, g2, etc.

  • v0-v5: The column names have no specific meaning and correspond to the values in the policy csv from left to right. Le nombre de colonnes dépend du nombre que vous définissez vous-même. 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.

Détails de l'adaptateur

For more details about the use of the adapter API and database table structure design, please visit: /docs/adapters