Skip to content

Commit cf72f4b

Browse files
authored
Merge pull request #128 from EmperorYP7/enforcer-binding
feat: Added python bindings for `casbin::Enforcer`
2 parents 249d177 + d543655 commit cf72f4b

File tree

6 files changed

+203
-43
lines changed

6 files changed

+203
-43
lines changed

bindings/python/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414

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

1920
add_library(pycasbin MODULE ${SOURCES})
2021

22+
target_include_directories(pycasbin PUBLIC ${CMAKE_SOURCE_DIR})
23+
2124
set_target_properties(pycasbin PROPERTIES
2225
PREFIX ""
26+
CXX_STANDARD 17
2327
)
2428

2529
if(WIN32)
@@ -28,7 +32,7 @@ if(WIN32)
2832
SUFFIX ".pyd"
2933
)
3034
endif()
31-
35+
3236

3337
# NOTE: Depending of the compiler version pybind11 2.4.3 does not compile with C++17 so revert to c++11
3438

@@ -38,7 +42,9 @@ if(UNIX)
3842

3943
# If supported for the target machine, emit position-independent code
4044
# suitable for dynamic linking.
41-
set_property(TARGET pycasbin PROPERTY POSITION_INDEPENDENT_CODE ON)
45+
set_target_properties(pycasbin PROPERTIES
46+
POSITION_INDEPENDENT_CODE ON
47+
)
4248
endif()
4349

4450
# macOS demands that the linker resolve all symbols at build time
@@ -53,5 +59,6 @@ target_link_libraries(pycasbin
5359
PRIVATE
5460
# casbin
5561
pybind11::module
62+
casbin
5663
)
5764

bindings/python/main.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,18 @@ int add(int i, int j) {
2727

2828
namespace py = pybind11;
2929

30-
PYBIND11_MODULE(pycasbin, m) {
31-
m.doc() = R"pbdoc(
32-
Pybind11 example plugin
33-
-----------------------
30+
// PYBIND11_MODULE(pycasbin, m) {
31+
// m.doc() = R"pbdoc(
32+
// Pybind11 example plugin
33+
// -----------------------
3434

35-
.. currentmodule:: pycasbin
35+
// .. currentmodule:: pycasbin
3636

37-
.. autosummary::
38-
:toctree: _generate
37+
// .. autosummary::
38+
// :toctree: _generate
3939

40-
add
41-
subtract
42-
)pbdoc";
40+
// Enforcer
41+
// )pbdoc";
4342

44-
m.def("add", &add, R"pbdoc(
45-
Add two numbers
46-
47-
Some other explanation about the add function.
48-
)pbdoc");
49-
50-
m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
51-
Subtract two numbers
52-
53-
Some other explanation about the subtract function.
54-
)pbdoc");
55-
56-
m.attr("__version__") = "dev";
57-
}
43+
// m.attr("__version__") = "dev";
44+
// }

bindings/python/py_enforcer.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
PYBIND11_MODULE(pycasbin, m) {
24+
py::class_<casbin::Enforcer>(m, "Enforcer")
25+
.def(py::init<>())
26+
.def(py::init<const std::string &, const std::string &>())
27+
.def(py::init<const std::string &, std::shared_ptr<casbin::Adapter>>())
28+
.def(py::init<std::shared_ptr<casbin::Model>, std::shared_ptr<casbin::Adapter>>())
29+
.def(py::init<std::shared_ptr<casbin::Model>>())
30+
.def(py::init<const std::string &>())
31+
.def(py::init<const std::string &, const std::string &, bool>())
32+
.def("InitWithFile", &casbin::Enforcer::InitWithFile, "InitWithFile initializes an enforcer with a model file and a policy file.")
33+
.def("InitWithAdapter", &casbin::Enforcer::InitWithAdapter, "InitWithAdapter initializes an enforcer with a database adapter.")
34+
.def("InitWithModelAndAdapter", &casbin::Enforcer::InitWithModelAndAdapter, "InitWithModelAndAdapter initializes an enforcer with a model and a database adapter.")
35+
.def("Initialize", &casbin::Enforcer::Initialize)
36+
.def("LoadModel", &casbin::Enforcer::LoadModel, R"doc(
37+
LoadModel reloads the model from the model CONF file.
38+
Because the policy is attached to a model, so the policy is invalidated and
39+
needs to be reloaded by calling LoadPolicy().
40+
)doc")
41+
.def("GetModel", &casbin::Enforcer::GetModel, "GetModel gets the current model.")
42+
.def("SetModel", &casbin::Enforcer::SetModel, "SetModel sets the current model.")
43+
.def("GetAdapter", &casbin::Enforcer::GetAdapter, "GetAdapter gets the current adapter.")
44+
.def("SetAdapter", &casbin::Enforcer::SetAdapter, "SetAdapter sets the current adapter.")
45+
.def("SetWatcher", &casbin::Enforcer::SetWatcher, "SetWatcher sets the current watcher.")
46+
.def("GetRoleManager", &casbin::Enforcer::GetRoleManager, "GetRoleManager gets the current role manager.")
47+
.def("SetRoleManager", &casbin::Enforcer::SetRoleManager, "SetRoleManager sets the current role manager.")
48+
.def("SetEffector", &casbin::Enforcer::SetEffector, "SetEffector sets the current effector.")
49+
.def("ClearPolicy", &casbin::Enforcer::ClearPolicy, "ClearPolicy clears all policy.")
50+
.def("LoadPolicy", &casbin::Enforcer::LoadPolicy, "LoadPolicy reloads the policy from file/database.")
51+
// .def("LoadFilteredPolicy", &casbin::Enforcer::LoadFilteredPolicy, "LoadFilteredPolicy reloads a filtered policy from file/database.")
52+
.def("IsFiltered", &casbin::Enforcer::IsFiltered, "IsFiltered returns true if the loaded policy has been filtered.")
53+
.def("SavePolicy", &casbin::Enforcer::SavePolicy, "SavePolicy saves the current policy (usually after changed with Casbin API) back to file/database.")
54+
.def("EnableEnforce", &casbin::Enforcer::EnableEnforce, "EnableEnforce changes the enforcing state of Casbin, when Casbin is disabled, all access will be allowed by the Enforce() function.")
55+
.def("EnableLog", &casbin::Enforcer::EnableLog, "EnableLog changes whether Casbin will log messages to the Logger.")
56+
.def("EnableAutoNotifyWatcher", &casbin::Enforcer::EnableAutoNotifyWatcher, "EnableAutoNotifyWatcher controls whether to save a policy rule automatically notify the Watcher when it is added or removed.")
57+
.def("EnableAutoSave", &casbin::Enforcer::EnableAutoSave, "EnableAutoSave controls whether to save a policy rule automatically to the adapter when it is added or removed.")
58+
.def("EnableAutoBuildRoleLinks", &casbin::Enforcer::EnableAutoBuildRoleLinks, "EnableAutoBuildRoleLinks controls whether to rebuild the role inheritance relations when a role is added or deleted.")
59+
.def("BuildRoleLinks", &casbin::Enforcer::BuildRoleLinks, "BuildRoleLinks manually rebuild the role inheritance relations.")
60+
.def("BuildIncrementalRoleLinks", &casbin::Enforcer::BuildIncrementalRoleLinks, "BuildIncrementalRoleLinks provides incremental build the role inheritance relations.")
61+
// .def("Enforce", py::overload_cast<casbin::Scope>(&casbin::Enforcer::Enforce), "Enforce decides whether a \"subject\" can access a \"object\" with the operation \"action\", input parameters are usually: (sub, obj, act).")
62+
.def("Enforce", py::overload_cast<const casbin::DataList &>(&casbin::Enforcer::Enforce), "Enforce with a vector param,decides whether a \"subject\" can access a \"object\" with the operation \"action\", input parameters are usually: (sub, obj, act).")
63+
.def("Enforce", py::overload_cast<const casbin::DataMap &>(&casbin::Enforcer::Enforce), "Enforce with a map param, decides whether a \"subject\" can access a \"object\" with the operation \"action\", input parameters are usually: (sub, obj, act).")
64+
// .def("EnforceWithMatcher", py::overload_cast<const std::string &, casbin::Scope>(&casbin::Enforcer::EnforceWithMatcher), "EnforceWithMatcher use a custom matcher to decides whether a \"subject\" can access a \"object\" with the operation \"action\", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is \"\".")
65+
.def("EnforceWithMatcher", py::overload_cast<const std::string &, const casbin::DataList &>(&casbin::Enforcer::EnforceWithMatcher), "EnforceWithMatcher use a custom matcher to decides whether a \"subject\" can access a \"object\" with the operation \"action\", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is \"\".")
66+
.def("EnforceWithMatcher", py::overload_cast<const std::string &, const casbin::DataMap &>(&casbin::Enforcer::EnforceWithMatcher), "EnforceWithMatcher use a custom matcher to decides whether a \"subject\" can access a \"object\" with the operation \"action\", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is \"\".")
67+
.def("BatchEnforce", &casbin::Enforcer::BatchEnforce, "BatchEnforce enforce in batches")
68+
.def("BatchEnforceWithMatcher", &casbin::Enforcer::BatchEnforceWithMatcher, "BatchEnforceWithMatcher enforce with matcher in batches")
69+
70+
/* Management API member functions. */
71+
72+
.def("GetAllSubjects", &casbin::Enforcer::GetAllSubjects)
73+
.def("GetAllNamedSubjects", &casbin::Enforcer::GetAllNamedSubjects)
74+
.def("GetAllObjects", &casbin::Enforcer::GetAllObjects)
75+
.def("GetAllNamedObjects", &casbin::Enforcer::GetAllNamedObjects)
76+
.def("GetAllActions", &casbin::Enforcer::GetAllActions)
77+
.def("GetAllNamedActions", &casbin::Enforcer::GetAllNamedActions)
78+
.def("GetAllRoles", &casbin::Enforcer::GetAllRoles)
79+
.def("GetAllNamedRoles", &casbin::Enforcer::GetAllNamedRoles)
80+
.def("GetPolicy", &casbin::Enforcer::GetPolicy)
81+
.def("GetFilteredPolicy", &casbin::Enforcer::GetFilteredPolicy)
82+
.def("GetNamedPolicy", &casbin::Enforcer::GetNamedPolicy)
83+
.def("GetFilteredNamedPolicy", &casbin::Enforcer::GetFilteredNamedPolicy)
84+
.def("GetGroupingPolicy", &casbin::Enforcer::GetGroupingPolicy)
85+
.def("GetFilteredGroupingPolicy", &casbin::Enforcer::GetFilteredGroupingPolicy)
86+
.def("GetNamedGroupingPolicy", &casbin::Enforcer::GetNamedGroupingPolicy)
87+
.def("GetFilteredNamedGroupingPolicy", &casbin::Enforcer::GetFilteredNamedGroupingPolicy)
88+
89+
.def("HasPolicy", &casbin::Enforcer::HasPolicy)
90+
.def("HasNamedPolicy", &casbin::Enforcer::HasNamedPolicy)
91+
.def("AddPolicy", &casbin::Enforcer::AddPolicy)
92+
.def("AddNamedPolicy", &casbin::Enforcer::AddNamedPolicy)
93+
.def("AddNamedPolicies", &casbin::Enforcer::AddNamedPolicies)
94+
.def("RemovePolicy", &casbin::Enforcer::RemovePolicy)
95+
.def("RemovePolicies", &casbin::Enforcer::RemovePolicies)
96+
.def("RemoveFilteredPolicy", &casbin::Enforcer::RemoveFilteredPolicy)
97+
.def("RemoveNamedPolicies", &casbin::Enforcer::RemoveNamedPolicies)
98+
.def("RemoveFilteredNamedPolicy", &casbin::Enforcer::RemoveFilteredNamedPolicy)
99+
.def("HasNamedGroupingPolicy", &casbin::Enforcer::HasNamedGroupingPolicy)
100+
.def("AddGroupingPolicy", &casbin::Enforcer::AddGroupingPolicy)
101+
.def("AddGroupingPolicies", &casbin::Enforcer::AddGroupingPolicies)
102+
.def("AddNamedGroupingPolicy", &casbin::Enforcer::AddNamedGroupingPolicy)
103+
.def("AddNamedGroupingPolicies", &casbin::Enforcer::AddNamedGroupingPolicies)
104+
.def("RemoveGroupingPolicy", &casbin::Enforcer::RemoveGroupingPolicy)
105+
.def("RemoveGroupingPolicies", &casbin::Enforcer::RemoveGroupingPolicies)
106+
.def("RemoveFilteredGroupingPolicy", &casbin::Enforcer::RemoveFilteredGroupingPolicy)
107+
.def("RemoveNamedGroupingPolicy", &casbin::Enforcer::RemoveNamedGroupingPolicy)
108+
.def("RemoveNamedGroupingPolicies", &casbin::Enforcer::RemoveNamedGroupingPolicies)
109+
.def("RemoveFilteredNamedGroupingPolicy", &casbin::Enforcer::RemoveFilteredNamedGroupingPolicy)
110+
.def("AddFunction", &casbin::Enforcer::AddFunction)
111+
.def("UpdateGroupingPolicy", &casbin::Enforcer::UpdateGroupingPolicy)
112+
.def("UpdateNamedGroupingPolicy", &casbin::Enforcer::UpdateNamedGroupingPolicy)
113+
.def("UpdatePolicy", &casbin::Enforcer::UpdatePolicy)
114+
.def("UpdateNamedPolicy", &casbin::Enforcer::UpdateNamedPolicy)
115+
.def("UpdatePolicies", &casbin::Enforcer::UpdatePolicies)
116+
.def("UpdateNamedPolicies", &casbin::Enforcer::UpdateNamedPolicies)
117+
118+
/* RBAC API member functions. */
119+
120+
.def("GetRolesForUser", &casbin::Enforcer::GetRolesForUser)
121+
.def("GetUsersForRole", &casbin::Enforcer::GetUsersForRole)
122+
.def("HasRoleForUser", &casbin::Enforcer::HasRoleForUser)
123+
.def("AddRoleForUser", &casbin::Enforcer::AddRoleForUser)
124+
.def("AddRolesForUser", &casbin::Enforcer::AddRolesForUser)
125+
.def("AddPermissionForUser", &casbin::Enforcer::AddPermissionForUser)
126+
.def("DeletePermissionForUser", &casbin::Enforcer::DeletePermissionForUser)
127+
.def("DeletePermissionsForUser", &casbin::Enforcer::DeletePermissionsForUser)
128+
.def("GetPermissionsForUser", &casbin::Enforcer::GetPermissionsForUser)
129+
.def("HasPermissionForUser", &casbin::Enforcer::HasPermissionForUser)
130+
.def("GetImplicitRolesForUser", &casbin::Enforcer::GetImplicitRolesForUser)
131+
.def("GetImplicitPermissionsForUser", &casbin::Enforcer::GetImplicitPermissionsForUser)
132+
.def("GetImplicitUsersForPermission", &casbin::Enforcer::GetImplicitUsersForPermission)
133+
.def("DeleteRoleForUser", &casbin::Enforcer::DeleteRoleForUser)
134+
.def("DeleteRolesForUser", &casbin::Enforcer::DeleteRolesForUser)
135+
.def("DeleteUser", &casbin::Enforcer::DeleteUser)
136+
.def("DeleteRole", &casbin::Enforcer::DeleteRole)
137+
.def("DeletePermission", &casbin::Enforcer::DeletePermission)
138+
139+
/* Internal API member functions omitted */
140+
141+
/* RBAC API with domains.*/
142+
143+
.def("GetUsersForRoleInDomain", &casbin::Enforcer::GetUsersForRoleInDomain)
144+
.def("GetRolesForUserInDomain", &casbin::Enforcer::GetRolesForUserInDomain)
145+
.def("GetPermissionsForUserInDomain", &casbin::Enforcer::GetPermissionsForUserInDomain)
146+
.def("AddRoleForUserInDomain", &casbin::Enforcer::AddRoleForUserInDomain)
147+
.def("DeleteRoleForUserInDomain", &casbin::Enforcer::DeleteRoleForUserInDomain);
148+
}

casbin/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ target_precompile_headers(casbin PUBLIC "pch.h")
2424

2525
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})
2626

27-
set_target_properties(casbin PROPERTIES PREFIX "")
27+
set_target_properties(casbin PROPERTIES
28+
PREFIX ""
29+
)
30+
2831
if(WIN32 OR MSVC)
2932
set_target_properties(casbin PROPERTIES SUFFIX ".lib")
3033
elseif(UNIX)
31-
set_target_properties(casbin PROPERTIES SUFFIX ".a")
34+
set_target_properties(casbin PROPERTIES
35+
SUFFIX ".a"
36+
POSITION_INDEPENDENT_CODE ON
37+
)
3238
endif()
3339

3440
install(

tests/CMakeLists.txt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@
1515
if(CASBIN_BUILD_TEST)
1616
set(CMAKE_CXX_STANDARD 17)
1717

18-
add_executable(
19-
casbintest
20-
built_in_functions_test.cpp
21-
config_test.cpp
22-
enforcer_test.cpp
23-
enforcer_cached_test.cpp
24-
enforcer_synced_test.cpp
25-
management_api_test.cpp
26-
model_enforcer_test.cpp
27-
model_test.cpp
28-
rbac_api_with_domains_test.cpp
29-
rbac_api_test.cpp
30-
role_manager_test.cpp
31-
util_test.cpp
32-
)
18+
add_executable(
19+
casbintest
20+
built_in_functions_test.cpp
21+
config_test.cpp
22+
enforcer_test.cpp
23+
enforcer_cached_test.cpp
24+
enforcer_synced_test.cpp
25+
management_api_test.cpp
26+
model_enforcer_test.cpp
27+
model_test.cpp
28+
rbac_api_with_domains_test.cpp
29+
rbac_api_test.cpp
30+
role_manager_test.cpp
31+
util_test.cpp
32+
)
33+
34+
if(UNIX)
35+
set_target_properties(casbintest PROPERTIES
36+
POSITION_INDEPENDENT_CODE ON
37+
)
38+
endif()
3339

3440
target_include_directories(casbintest PUBLIC ${CMAKE_SOURCE_DIR})
3541

tests/benchmarks/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ add_executable(casbin_benchmark
2323

2424
target_include_directories(casbin_benchmark PUBLIC ${CMAKE_SOURCE_DIR})
2525

26+
if(UNIX)
27+
set_target_properties(casbin_benchmark PROPERTIES
28+
POSITION_INDEPENDENT_CODE ON
29+
)
30+
endif()
31+
2632
target_link_libraries(
2733
casbin_benchmark
2834
benchmark

0 commit comments

Comments
 (0)