You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,6 +100,37 @@ You can also use the online editor (https://casbin.org/editor/) to write your Ca
100
100
101
101
https://casbin.org/docs/en/tutorials
102
102
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:
0 commit comments