Skip to content

Commit 6d8baae

Browse files
authored
feat: Added bindings for CachedEnforcer (#129)
* feat: Added binding for CachedEnforcer Signed-off-by: Yash Pandey (YP) <[email protected]> * feat: Added bindings for CachedEnforcer Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent cf72f4b commit 6d8baae

File tree

7 files changed

+138
-22
lines changed

7 files changed

+138
-22
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CMAKE_MODULE_PATH
2020
)
2121

2222
set(CMAKE_WARN_DEPRECATED ON)
23+
set(PY_CASBIN_VERSION 1.0)
2324

2425
if(APPLE AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
2526
# The value of this variable should be set prior to the first project() command invocation

bindings/python/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414

1515
set(SOURCES
1616
main.cpp
17+
py_cached_enforcer.cpp
1718
py_enforcer.cpp
1819
)
1920

20-
add_library(pycasbin MODULE ${SOURCES})
21+
set(HEADERS
22+
py_casbin.h
23+
)
24+
25+
add_library(pycasbin MODULE ${SOURCES} ${HEADERS})
2126

2227
target_include_directories(pycasbin PUBLIC ${CMAKE_SOURCE_DIR})
2328

@@ -26,6 +31,8 @@ set_target_properties(pycasbin PROPERTIES
2631
CXX_STANDARD 17
2732
)
2833

34+
add_definitions(-DPY_CASBIN_VERSION=${PY_CASBIN_VERSION})
35+
2936
if(WIN32)
3037
# Windows uses .pyd extension for python modules
3138
set_target_properties(pycasbin PROPERTIES

bindings/python/main.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,25 @@
1717
*/
1818

1919
#include <pybind11/pybind11.h>
20-
21-
#define STRINGIFY(x) #x
22-
#define MACRO_STRINGIFY(x) STRINGIFY(x)
23-
24-
int add(int i, int j) {
25-
return i + j;
26-
}
20+
#include "py_casbin.h"
2721

2822
namespace py = pybind11;
2923

30-
// PYBIND11_MODULE(pycasbin, m) {
31-
// m.doc() = R"pbdoc(
32-
// Pybind11 example plugin
33-
// -----------------------
24+
PYBIND11_MODULE(pycasbin, m) {
25+
m.doc() = R"pbdoc(
26+
Casbin Authorization Library
27+
-----------------------
3428
35-
// .. currentmodule:: pycasbin
29+
.. currentmodule:: pycasbin
3630
37-
// .. autosummary::
38-
// :toctree: _generate
31+
.. autosummary::
32+
:toctree: _generate
3933
40-
// Enforcer
41-
// )pbdoc";
34+
Enforcer
35+
)pbdoc";
4236

43-
// m.attr("__version__") = "dev";
44-
// }
37+
bindPyEnforcer(m);
38+
bindPyCachedEnforcer(m);
39+
40+
m.attr("__version__") = PY_CASBIN_VERSION;
41+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <pybind11/pybind11.h>
18+
#include <pybind11/stl.h>
19+
#include <casbin/casbin.h>
20+
21+
namespace py = pybind11;
22+
23+
void bindPyCachedEnforcer(py::module &m) {
24+
py::class_<casbin::CachedEnforcer, casbin::Enforcer>(m, "CachedEnforcer")
25+
.def(py::init<>(), "Enforcer is the default constructor.")
26+
.def(py::init<const std::string &, const std::string &>(), R"doc(
27+
Enforcer initializes an enforcer with a model file and a policy file.
28+
@param model_path the path of the model file.
29+
@param policy_file the path of the policy file.
30+
)doc")
31+
.def(py::init<const std::string &, std::shared_ptr<casbin::Adapter>>(), R"doc(
32+
Enforcer initializes an enforcer with a database adapter.
33+
@param model_path the path of the model file.
34+
@param adapter the adapter.
35+
)doc")
36+
.def(py::init<std::shared_ptr<casbin::Model>, std::shared_ptr<casbin::Adapter>>(), R"doc(
37+
Enforcer initializes an enforcer with a model and a database adapter.
38+
@param m the model.
39+
@param adapter the adapter.
40+
)doc")
41+
.def(py::init<std::shared_ptr<casbin::Model>>(), R"doc(
42+
Enforcer initializes an enforcer with a model.
43+
@param m the model.
44+
)doc")
45+
.def(py::init<const std::string &>(), R"doc(
46+
Enforcer initializes an enforcer with a model file.
47+
@param model_path the path of the model file.
48+
)doc")
49+
.def(py::init<const std::string &, const std::string &, bool>(), R"doc(
50+
Enforcer initializes an enforcer with a model file, a policy file and an enable log flag.
51+
@param model_path the path of the model file.
52+
@param policy_file the path of the policy file.
53+
@param enable_log whether to enable Casbin's log.
54+
)doc")
55+
56+
// .def("Enforce", py::overload_cast<casbin::Scope>(&casbin::CachedEnforcer::Enforce), R"doc(
57+
// Enforce with a vector param,decides whether a "subject" can access a
58+
// "object" with the operation "action", input parameters are usually: (sub,
59+
// obj, act).
60+
// )doc")
61+
.def("Enforce", py::overload_cast<const casbin::DataList &>(&casbin::CachedEnforcer::Enforce), R"doc(
62+
Enforce with a map param,decides whether a "subject" can access a "object"
63+
with the operation "action", input parameters are usually: (sub, obj, act).
64+
)doc")
65+
.def("Enforce", py::overload_cast<const casbin::DataMap &>(&casbin::CachedEnforcer::Enforce), R"doc(
66+
Enforce with a map param,decides whether a "subject" can access a "object"
67+
with the operation "action", input parameters are usually: (sub, obj, act).
68+
)doc")
69+
// .def("EnforceWithMatcher", py::overload_cast<const std::string &, casbin::Scope>(&casbin::CachedEnforcer::EnforceWithMatcher), R"doc(
70+
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can
71+
// access a "object" with the operation "action", input parameters are
72+
// usually: (matcher, sub, obj, act), use model matcher by default when
73+
// matcher is "".
74+
// )doc")
75+
.def("EnforceWithMatcher", py::overload_cast<const std::string &, const casbin::DataList &>(&casbin::CachedEnforcer::EnforceWithMatcher), R"doc(
76+
EnforceWithMatcher use a custom matcher to decides whether a "subject" can
77+
access a "object" with the operation "action", input parameters are
78+
usually: (matcher, sub, obj, act), use model matcher by default when
79+
matcher is "".
80+
)doc")
81+
.def("EnforceWithMatcher", py::overload_cast<const std::string &, const casbin::DataMap &>(&casbin::CachedEnforcer::EnforceWithMatcher), R"doc(
82+
EnforceWithMatcher use a custom matcher to decides whether a "subject" can
83+
access a "object" with the operation "action", input parameters are
84+
usually: (matcher, sub, obj, act), use model matcher by default when
85+
matcher is "".
86+
)doc");
87+
}

bindings/python/py_casbin.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <pybind11/pybind11.h>
18+
19+
namespace py = pybind11;
20+
21+
void bindPyEnforcer(py::module &m);
22+
void bindPyCachedEnforcer(py::module &m);

bindings/python/py_enforcer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
#include <pybind11/stl.h>
1919
#include <casbin/casbin.h>
2020

21+
#include "py_casbin.h"
22+
2123
namespace py = pybind11;
2224

23-
PYBIND11_MODULE(pycasbin, m) {
25+
void bindPyEnforcer(py::module& m) {
2426
py::class_<casbin::Enforcer>(m, "Enforcer")
2527
.def(py::init<>())
2628
.def(py::init<const std::string &, const std::string &>())

casbin/enforcer_cached.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class CachedEnforcer : public Enforcer {
3838
void InvalidateCache();
3939

4040
public:
41-
/**
41+
/**
4242
* Enforcer is the default constructor.
43-
*/
43+
*/
4444
CachedEnforcer();
4545
/**
4646
* Enforcer initializes an enforcer with a model file and a policy file.

0 commit comments

Comments
 (0)