주요 콘텐츠로 건너뛰기

Policy Storage

Casbin implements policy storage through adapters.

.CSV 파일에서 정책 로드

Loading from a CSV file is the standard approach. This method is straightforward for beginners and convenient when seeking help from the Casbin team.

Here's an example CSV file 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
노트

When CSV fields contain commas, wrap them 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)

When fields contain both commas and double quotes, wrap the field in double quotes and escape embedded 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 "")

관련 이슈: casbin#886

어댑터 API

메소드타입설명
LoadPolicy()기본저장소에서 모든 정책 규칙을 로드합니다
SavePolicy()기본모든 정책 규칙을 저장소에 저장합니다
AddPolicy()선택적정책 규칙을 저장소에 추가합니다
RemovePolicy()선택적정책 규칙을 저장소에서 제거합니다
RemoveFilteredPolicy()선택적필터와 일치하는 정책 규칙을 저장소에서 제거합니다

데이터베이스 저장 형식

귀하의 정책 파일

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

해당 데이터베이스 구조 (예: MySQL)

idptypev0v1v2v3v4v5
1pdata2_admindata2read
2pdata2_admindata2write
3galiceadmin

각 열의 의미

  • id: Database primary key, not part of the Casbin policy. Generation method depends on the adapter implementation.

  • ptype: Corresponds to p, g, g2, etc.

  • v0-v5: Generic column names that map to policy CSV values from left to right. The number of columns depends on your policy definition. Most adapters implement 6 columns, though theoretically unlimited columns are possible. Submit an issue to the adapter repository if you need more columns.

어댑터 세부 정보

For more information about the adapter API and database structure design, see /docs/adapters.