Skip to content

Commit 0cd1fc5

Browse files
authored
chore: Updated docs for build instructions (#113)
* docs: Updated docs Signed-off-by: Yash Pandey (YP) <[email protected]> * fix: minor errors Signed-off-by: Yash Pandey (YP) <[email protected]> * docs: Added summary Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 6f6a4e8 commit 0cd1fc5

File tree

3 files changed

+77
-34
lines changed

3 files changed

+77
-34
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ jobs:
9494
- name: Cleanup
9595
id: clean-up
9696
run: |
97-
rm -r build lib
97+
rm -r build

README.md

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -111,48 +111,72 @@ https://casbin.org/docs/en/tutorials
111111

112112
## Installation and Set-Up
113113

114-
#### Windows (Microsoft Visual Studio 2019)
115-
- `Clone` the repository in your target client project
116-
- Open the project solution and `build` the solution
117-
- To use the `casbin-cpp` project in your client project add the path to the static library `casbin.lib` file to your client project properties under `VC++ Directories` > `Library Directories` and also add the path of `casbin` directory to your client project properties under `VC++ Directories` > `Include Directories`
118-
- Add the static library file name `casbin.lib` to the properties under `Linker` > `Input` > `Additional Dependencies`
119-
120-
#### Unix
121-
- `Clone` the repository in your target client project
122-
- Change the current working directory to the `casbin-cpp` directory and build the library through following commands:
123-
- ***Make***
124-
```shell
125-
$ make
126-
$ make library
114+
### Build instructions for all platforms
115+
116+
(Assuming you have CMake v3.19 or later installed)
117+
118+
1. Clone/checkout to [`casbin/casbin-cpp:master`](https://github.com/EmperorYP7/casbin-cpp/tree/ctest-setup)
119+
```bash
120+
git clone https://github.com/casbin/casbin-cpp.git
127121
```
128-
- ***CMake***
129-
```shell
130-
$ make build
122+
123+
2. Open terminal/cmd in the root directory of the project:
124+
125+
**Note:** On Windows, this command will also create Visual Studio project files in the `/build` directory.
126+
127+
```bash
128+
mkdir build
129+
cd build
130+
cmake ..
131+
```
132+
133+
3. After the project is configured successfully, build it:
134+
135+
```bash
136+
cmake --build .
131137
```
132-
- To get rid of intermediate files generated during building of library:
133-
```shell
134-
$ make clean
138+
139+
4. To install casbin library to your machine run:
140+
141+
```bash
142+
cmake --build . --target install
135143
```
136-
- Now, you can use the file present in `lib` directory, created through `archiver`, as a static library
137144

138-
## Get started
145+
- For **Windows**, this will install `casbin.lib` to `C:/Program Files/casbin-cpp/lib`
146+
and the headers to `C:/Program Files/casbin-cpp/include`.
147+
- For Unix based OS i.e. **Linux and macOS**, this will install `casbin.a` to `usr/local/lib`
148+
and the headers to `usr/local/include`.
139149

140-
1. New a Casbin enforcer with a model file and a policy file:
150+
You can add the respective include and lib paths
151+
to the PATH environment variable to use casbin.
141152

142-
```c++
143-
Enforcer* e = Enforcer :: NewEnforcer("<path to model.conf>", "<path to policy.csv>");
153+
5. (OPTIONAL) To run the tests, issue the following command from `/build`:
154+
155+
```bash
156+
ctest
144157
```
145158

146-
Note: you can also initialize an enforcer with policy in DB instead of file, see [Policy-persistence](#policy-persistence) section for details.
159+
## Get started
160+
161+
1. Add the include directory of the project to the PATH Environment variable.
162+
```cpp
163+
#include <casbin/casbin.h>
164+
```
165+
166+
2. New a Casbin enforcer with a model file and a policy file:
167+
168+
```cpp
169+
casbin::Enforcer e("./path/to/model.conf", "./path/to/policy.csv");
170+
```
147171

148172
2. Add an enforcement hook into your code right before the access happens:
149173

150-
```c++
151-
string sub = "alice"; // the user that wants to access a resource.
152-
string obj = "data1"; // the resource that is going to be accessed.
153-
string act = "read"; // the operation that the user performs on the resource.
174+
```cpp
175+
std::string sub = "alice"; // the user that wants to access a resource.
176+
std::string obj = "data1"; // the resource that is going to be accessed.
177+
std::string act = "read"; // the operation that the user performs on the resource.
154178
155-
if(e->Enforce({ sub, obj, act })) {
179+
if(e.Enforce({ sub, obj, act })) {
156180
// permit alice to read data1
157181
} else {
158182
// deny the request, show an error
@@ -161,10 +185,29 @@ Note: you can also initialize an enforcer with policy in DB instead of file, see
161185

162186
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:
163187

164-
```c++
165-
vector<string> roles( e->GetImplicitRolesForUser(sub) );
188+
```cpp
189+
std::vector<std::string> roles( e.GetImplicitRolesForUser(sub) );
166190
```
167191

192+
Here's the summary:
193+
```cpp
194+
#include <casbin/casbin.h>
195+
196+
void IsAuthorized() {
197+
casbin::Enforcer e("./path/to/model.conf", "./path/to/policy.csv");
198+
199+
std::string sub = "alice"; // the user that wants to access a resource.
200+
std::string obj = "data1"; // the resource that is going to be accessed.
201+
std::string act = "read"; // the operation that the user performs on the resource.
202+
203+
if(e.Enforce({ sub, obj, act })) {
204+
// permit alice to read data1
205+
} else {
206+
// deny the request, show an error
207+
}
208+
}
209+
```
210+
168211
## Policy management
169212
170213
Casbin provides two sets of APIs to manage permissions:

casbin/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ install(
3535

3636
install(
3737
DIRECTORY ${CMAKE_SOURCE_DIR}/casbin
38-
DESTINATION ${CMAKE_SOURCE_DIR}/lib
38+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
3939
FILES_MATCHING PATTERN "*.h"
4040
)

0 commit comments

Comments
 (0)