|
| 1 | +## Language Bindings for `casbin-cpp` |
| 2 | + |
| 3 | +At present, `casbin-cpp` provides language bindings for Python, we named it `pycasbin`. |
| 4 | + |
| 5 | +## Python Bindings |
| 6 | + |
| 7 | +### Use `pip` install the `pycasbin` module |
| 8 | + |
| 9 | +It is assumed you have `CMake >=v3.19` and `Python >= 3.6` installed. Current `pycasbin` only support `pip` install in local machine. |
| 10 | + |
| 11 | +1. Clone/download the project: |
| 12 | + ```bash |
| 13 | + git clone https://github.com/casbin/casbin-cpp.git |
| 14 | + ``` |
| 15 | + |
| 16 | +2. Update `wheel setuptools`: |
| 17 | + ```bash |
| 18 | + python -m pip install --upgrade wheel setuptools |
| 19 | + ``` |
| 20 | + |
| 21 | +3. Install the `pycasbin` module: |
| 22 | + ```bash |
| 23 | + cd casbin-cpp && pip install --verbose . |
| 24 | + ``` |
| 25 | + |
| 26 | +Now, you're ready to go! |
| 27 | +
|
| 28 | +### Usage |
| 29 | +
|
| 30 | +It is assumed that you have `pycasbin` module correctly installed on your system. |
| 31 | +
|
| 32 | +First, we import the `pycasbin` module to a python source file: |
| 33 | +
|
| 34 | +```python |
| 35 | +import pycasbin as casbin |
| 36 | +``` |
| 37 | +
|
| 38 | +Suppose we want a function to check authorization of a request: |
| 39 | +
|
| 40 | +```python |
| 41 | +def isAuthorized(req): |
| 42 | + result = True |
| 43 | + if result: |
| 44 | + print('Authorized') |
| 45 | + else |
| 46 | + print('Not authorized!') |
| 47 | +``` |
| 48 | +
|
| 49 | +Here, the request can be a list or a dictionary in the forms: |
| 50 | +
|
| 51 | +```python |
| 52 | +req = ['subject1', 'object1', 'action1'] # and so on.. |
| 53 | +
|
| 54 | +req = { |
| 55 | + "sub": "subject1", |
| 56 | + "obj": "object1", |
| 57 | + "act": "action1" # ... and so on |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +We can Enforce this request (or compute the `result` of this request) through `casbin.Enforce()`. |
| 62 | +For that, we need to create a `casbin.Enforcer`: |
| 63 | +
|
| 64 | +```python |
| 65 | +e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv') |
| 66 | +``` |
| 67 | +Make sure that the paths are relative to the current python source file or an absolute path. |
| 68 | +
|
| 69 | +Apart from the regular `Enforcer`, you may also use `CachedEnforcer` |
| 70 | +depending on your use case. |
| 71 | +
|
| 72 | +Incorporating the `Enforcer` in our example gives us: |
| 73 | +
|
| 74 | +```python |
| 75 | +def isAuthorized(req): |
| 76 | + result = e.Enforce(req) |
| 77 | + if result: |
| 78 | + print('Authorized') |
| 79 | + else |
| 80 | + print('Not authorized!') |
| 81 | +``` |
| 82 | +
|
| 83 | +Rest of the method's name is on par with `casbin-cpp`. |
| 84 | + |
| 85 | +#### Summary |
| 86 | + |
| 87 | +This sums up the basic usage of `pycasbin` module: |
| 88 | + |
| 89 | +```python |
| 90 | +import pycasbin as casbin |
| 91 | +
|
| 92 | +e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv') |
| 93 | +
|
| 94 | +def isAuthorized(req): |
| 95 | + result = e.Enforce(req) |
| 96 | + if result: |
| 97 | + print('Authorized') |
| 98 | + else |
| 99 | + print('Not authorized!') |
| 100 | +
|
| 101 | +isAuthorized(['subject1', 'object1', 'action1']) |
| 102 | +isAuthorized(['subject2', 'object2', 'action2']) |
| 103 | +# ... and so on |
| 104 | +``` |
| 105 | +
|
| 106 | +If you've done everything right, you'll see your output |
| 107 | +without any errors. |
0 commit comments