Skip to content

Commit 4dd852d

Browse files
authored
docs: Added documentation for python bindings (#146)
* docs: Added documentation for python bindings Signed-off-by: Yash Pandey (YP) <[email protected]> * fix: error on ubuntu Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 0c42d80 commit 4dd852d

File tree

11 files changed

+315
-60
lines changed

11 files changed

+315
-60
lines changed

CMakeLists.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ option(CASBIN_BUILD_TEST "State whether to build test" ON)
5555
option(CASBIN_BUILD_BENCHMARK "State whether to build benchmarks" ON)
5656
option(CASBIN_BUILD_BINDINGS "State whether to build language bindings" ON)
5757
option(CASBIN_BUILD_PYTHON_BINDINGS "State whether to build python bindings" ON)
58+
option(CASBIN_INSTALL "State whether to install casbin targets on the current system" ON)
5859

60+
# Intrinsic directory paths
61+
set(CASBIN_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/casbin)
62+
set(CASBIN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
63+
set(CASBIN_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
5964

6065
# Do not output install messages.
6166
if(NOT DEFINED CMAKE_INSTALL_MESSAGE)
62-
set(CMAKE_INSTALL_MESSAGE "NEVER")
67+
set(CMAKE_INSTALL_MESSAGE "LAZY")
6368
endif()
6469

6570
if(CASBIN_BUILD_BINDINGS)
@@ -88,3 +93,24 @@ set(CMAKE_CXX_STANDARD 17)
8893
include(FindExtPackages)
8994

9095
add_subdirectory(casbin)
96+
97+
if(CASBIN_INSTALL)
98+
message(CHECK_START "[casbin]: Installing casbin ...")
99+
export(
100+
TARGETS casbin
101+
NAMESPACE casbin::
102+
FILE casbinTargets.cmake
103+
)
104+
105+
# Installing headers
106+
install(
107+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/casbin
108+
DESTINATION include
109+
)
110+
111+
set(CMAKE_EXPORT_PACKAGE_REGISTRY ON)
112+
export(PACKAGE casbin)
113+
114+
message(CHECK_PASS " The targets can now be imported with find_package(casbin)")
115+
message(STATUS "[casbin]: Build the \"install\" target and add \"${CMAKE_INSTALL_PREFIX}/include\" to you PATH for casbin to work")
116+
endif()

README.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ production-ready | production-ready | production-ready | production-ready
3333
[PyCasbin](https://github.com/casbin/pycasbin) | [Casbin.NET](https://github.com/casbin-net/Casbin.NET) | [Casbin-CPP](https://github.com/casbin/casbin-cpp) | [Casbin-RS](https://github.com/casbin/casbin-rs)
3434
production-ready | production-ready | beta-test | production-ready
3535

36+
**Note**: PyCasbin-on-CPP is available to use. Refer to the [documentation](./bindings/README.md) for installation and usage.
37+
3638
## Supported models
3739

3840
1. [**ACL (Access Control List)**](https://en.wikipedia.org/wiki/Access_control_list)
@@ -113,8 +115,10 @@ https://casbin.org/docs/en/tutorials
113115

114116
## Integrating Casbin to your project through CMake
115117

116-
Here is a [working project](https://github.com/EmperorYP7/casbin-CMake-setup) to demonstarte how to set up your CMake
117-
configurations to integrate casbin.
118+
### Without installing casbin locally
119+
120+
Here is a [working project](https://github.com/EmperorYP7/casbin-CMake-setup) to demonstarte how to set up
121+
your CMake configurations to integrate casbin without any prior installations.
118122

119123
You may integrate casbin into your CMake project through `find_package`. **It is assumed that you're using CMake >= v3.19.**
120124

@@ -161,6 +165,57 @@ target_include_directories(myexec PRIVATE ${myexec_INCLUDE_DIR})
161165

162166
Do remember to include `casbin_SOURCE_DIR/include` directory wherever casbin's functions are utilised.
163167

168+
### With local installation
169+
170+
You may integrate casbin into your CMake project through `find_package`.
171+
**It is assumed that you're using CMake >= v3.19**
172+
173+
1. Clone/checkout to [`casbin/casbin-cpp:master`](https://github.com/EmperorYP7/casbin-cpp/tree/ctest-setup)
174+
```bash
175+
git clone https://github.com/casbin/casbin-cpp.git
176+
```
177+
178+
2. Open terminal/cmd in the root directory of the project:
179+
180+
```bash
181+
mkdir build
182+
cd build
183+
cmake ..
184+
```
185+
186+
**Note:** Look up for the logs of this step. And add the path indicated by the log into your PATH/project include directory.
187+
The log message you're looking for should be something like this:
188+
```bash
189+
[casbin]: Installing casbin ...
190+
[casbin]: Installing casbin ... - The targets can now be imported with find_package(casbin)
191+
[casbin]: Build the "install" target and add "/usr/local/include" to you PATH for casbin to work
192+
```
193+
194+
3. After the project is configured successfully, build it:
195+
```bash
196+
cmake --build . --config Release
197+
```
198+
199+
4. Install casbin:
200+
201+
```bash
202+
cmake --build . --config Release --target install
203+
```
204+
Now, casbin has been installed and ready to go.
205+
206+
5. In your project's CMake file, add
207+
```cmake
208+
find_package(casbin REQUIRED)
209+
```
210+
This will import all the targets exported by casbin to your project
211+
212+
6. Link against casbin (Refer to Step 2's **Note** to get the value of `MY_INCLUDE_DIR` for your system):
213+
```cmake
214+
set(MY_INCLUDE_DIR "/usr/local/include")
215+
target_include_directories(MyTargetName PRIVATE ${MY_INCLUDE_DIR})
216+
target_link_libraries(MyTargetName PRIVATE casbin::casbin)
217+
```
218+
164219
## Installation and Set-Up
165220
166221
### Build instructions for all platforms
@@ -194,8 +249,8 @@ Do remember to include `casbin_SOURCE_DIR/include` directory wherever casbin's f
194249
cmake --build . --target install
195250
```
196251
197-
- For **Windows**, this will install `casbin.lib` to `C:/Program Files/casbin-cpp/lib`
198-
and the headers to `C:/Program Files/casbin-cpp/include`.
252+
- For **Windows**, this will install `casbin.lib` to `C:/Program Files/casbin/lib`
253+
and the headers to `C:/Program Files/casbin/include`.
199254
- For Unix based OS i.e. **Linux and macOS**, this will install `casbin.a` to `usr/local/lib`
200255
and the headers to `usr/local/include`.
201256

bindings/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
## Language Bindings for Casbin
2+
3+
Casbin-CPP provides language bindings to compound the advantages of different languages and to make
4+
authorization easier and faster.
5+
6+
At present, casbin-cpp provides language bindings for Python.
7+
8+
## Python Bindings
9+
10+
### Installing the PyCasbin module
11+
12+
It is assumed you have CMake >=v3.19 and Python >= 3.2 installed.
13+
14+
1. Clone/download the project:
15+
```bash
16+
git clone https://github.com/casbin/casbin-cpp.git
17+
```
18+
19+
2. Make a build directory and generate project files through CMake:
20+
```bash
21+
mkdir build
22+
cd build
23+
cmake ..
24+
```
25+
**Note:** Kindly look at the log message to find the directory you need to add in your `sys.path` (Step 5). The log may look like this:
26+
```bash
27+
[pycasbin]: Build "pycasbin" target for Python Bindings
28+
[pycasbin]: Add "lib/python3.9/site-packages" to your sys.path/USER_SITE variable if not already present
29+
```
30+
31+
3. Build the python bindings (`pycasbin` target):
32+
```bash
33+
cmake --build . --config Release --target pycasbin
34+
```
35+
36+
4. Install the `pycasbin` module:
37+
```bash
38+
cmake --build . --config Release --target install
39+
```
40+
This will install the module to:
41+
- `<prefix>/lib/site-packages` on Windows.
42+
- `<prefix>/lib/python3.x/site-packages` on UNIX.
43+
44+
**Note:** The actual install path can be deduced in the log output of Step 2.
45+
46+
5. Add the correct `site-packages` directory path to `sys.path` or `USER_SITE` of your current python configuration if not already present.
47+
48+
Now, you're ready to go!
49+
50+
### Usage
51+
52+
It is assumed that you have `pycasbin` module correctly installed on your system.
53+
54+
First, we import the pycasbin module to a python source file:
55+
56+
```python
57+
import pycasbin as casbin
58+
```
59+
60+
Suppose we want a function to check authorization of a request:
61+
62+
```python
63+
def isAuthorized(req):
64+
result = True
65+
if result:
66+
print('Authorized')
67+
else
68+
print('Not authorized!')
69+
```
70+
71+
Here, the request can be a list or a dictionary in the forms:
72+
73+
```python
74+
req = ['subject1', 'object1', 'action1'] # and so on..
75+
76+
req = {
77+
"sub": "subject1",
78+
"obj": "object1",
79+
"act": "action1" # ... and so on
80+
}
81+
```
82+
83+
We can Enforce this request (or compute the `result` of this request) through `casbin.Enforce()`.
84+
For that, we need to create a `casbin.Enforcer`:
85+
86+
```python
87+
e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv')
88+
```
89+
Make sure that the paths are relative to the current python source file or an absolute path.
90+
91+
Apart from the regular `Enforcer`, you may also use `CachedEnforcer`
92+
depending on your use case.
93+
94+
Incorporating the `Enforcer` in our example gives us:
95+
96+
```python
97+
def isAuthorized(req):
98+
result = e.Enforce(req)
99+
if result:
100+
print('Authorized')
101+
else
102+
print('Not authorized!')
103+
```
104+
105+
Rest of the method's name is on par with casbin-CPP.
106+
107+
#### Summary
108+
109+
This sums up the basic usage of pycasbin module:
110+
111+
```python
112+
import pycasbin as casbin
113+
114+
e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv')
115+
116+
def isAuthorized(req):
117+
result = e.Enforce(req)
118+
if result:
119+
print('Authorized')
120+
else
121+
print('Not authorized!')
122+
123+
isAuthorized(['subject1', 'object1', 'action1'])
124+
isAuthorized(['subject2', 'object2', 'action2'])
125+
# ... and so on
126+
```
127+
128+
If you've done everything right, you'll see your output
129+
without any errors.

bindings/python/CMakeLists.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ set(HEADERS
3030

3131
Python_add_library(pycasbin MODULE ${SOURCES} ${HEADERS})
3232

33-
target_include_directories(pycasbin PUBLIC ${CMAKE_SOURCE_DIR}/include)
33+
target_include_directories(pycasbin PUBLIC ${CASBIN_INCLUDE_DIR})
3434

3535
set_target_properties(pycasbin PROPERTIES
3636
PREFIX ""
@@ -78,16 +78,19 @@ else()
7878
set(Python_VARIANT_PATH lib${LIB_SUFFIX}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages)
7979
endif()
8080

81-
message("${Python_VARIANT_PATH}")
82-
8381
# For testing
8482
install(
8583
TARGETS pycasbin
8684
LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/tests/python
8785
)
8886

89-
# For actual installation
90-
install(
91-
TARGETS pycasbin
92-
LIBRARY DESTINATION ${Python_VARIANT_PATH}
93-
)
87+
if(CASBIN_INSTALL)
88+
# For actual installation
89+
install(
90+
TARGETS pycasbin
91+
LIBRARY DESTINATION ${Python_VARIANT_PATH}
92+
)
93+
94+
message(STATUS "[pycasbin]: Build \"pycasbin\" target for Python Bindings")
95+
message(STATUS "[pycasbin]: Add \"${Python_VARIANT_PATH}\" to your sys.path/USER_SITE variable if not already present")
96+
endif()

casbin/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
set(CASBIN_SOURCE
15+
set(CASBIN_SOURCE_FILES
1616
abac_data.cpp
1717
enforcer.cpp
1818
enforcer_cached.cpp
@@ -71,10 +71,10 @@ set(CASBIN_SOURCE
7171
# Setting to C++ standard to C++17
7272
set(CMAKE_CXX_STANDARD 17)
7373

74-
add_library(casbin STATIC ${CASBIN_SOURCE})
74+
add_library(casbin STATIC ${CASBIN_SOURCE_FILES})
7575

76-
target_precompile_headers(casbin PUBLIC pch.h)
77-
target_include_directories(casbin PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
76+
target_precompile_headers(casbin PRIVATE pch.h)
77+
target_include_directories(casbin PRIVATE ${CASBIN_SOURCE_DIR})
7878

7979
set_target_properties(casbin PROPERTIES
8080
PREFIX ""

casbin/abac_data.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef ABAC_H
18-
#define ABAC_H
17+
#ifndef CASBIN_ABAC_H
18+
#define CASBIN_ABAC_H
1919

2020
#include "attribute_types.h"
2121

@@ -109,4 +109,4 @@ typedef ABACData ABACData;
109109
const std::shared_ptr<ABACData> GetDataObject(const AttributeMap& attribs);
110110
}
111111

112-
#endif
112+
#endif // CASBIN_ABAC_H

casbin/casbin.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

include/casbin/casbin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#ifndef CASBIN_CPP_CASBIN_H
2020
#define CASBIN_CPP_CASBIN_H
2121

22+
#include "casbin_types.h"
2223
#include "casbin_enforcer.h"
2324
#include "casbin_helpers.h"
24-
#include "casbin_types.h"
2525
#include "duktape/duktape.h"
2626
#include "duktape/duk_config.h"
2727

0 commit comments

Comments
 (0)