Skip to content

Commit 514fa2a

Browse files
committed
feat: Add Get-Started section.
Signed-off-by: DivyPatel9881 <[email protected]>
1 parent 5f80bb5 commit 514fa2a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ You can also use the online editor (https://casbin.org/editor/) to write your Ca
100100

101101
https://casbin.org/docs/en/tutorials
102102

103+
## Get started
104+
105+
1. New a Casbin enforcer with a model file and a policy file:
106+
107+
```c++
108+
Enforcer* e = Enforcer :: NewEnforcer("<path to model.conf>", "<path to policy.csv>");
109+
```
110+
111+
Note: you can also initialize an enforcer with policy in DB instead of file, see [Policy-persistence](#policy-persistence) section for details.
112+
113+
2. Add an enforcement hook into your code right before the access happens:
114+
115+
```c++
116+
string sub = "alice"; // the user that wants to access a resource.
117+
string obj = "data1"; // the resource that is going to be accessed.
118+
string act = "read"; // the operation that the user performs on the resource.
119+
120+
if(e->Enforce({ sub, obj, act })) {
121+
// permit alice to read data1
122+
} else {
123+
// deny the request, show an error
124+
}
125+
```
126+
127+
3. Besides the static policy file, Casbin also provides API for permission management at run-time. For example, You can get all the roles assigned to a user as below:
128+
129+
```c++
130+
vector<string> roles( e->GetImplicitRolesForUser(sub) );
131+
```
132+
133+
103134
## Policy management
104135

105136
Casbin provides two sets of APIs to manage permissions:

0 commit comments

Comments
 (0)