Skip to content

Commit 24d43ce

Browse files
authored
Merge pull request #139 from EmperorYP7/config-binding
feat: Added python bindings for `casbin::Config`
2 parents 673bc01 + 1365d97 commit 24d43ce

File tree

17 files changed

+255
-78
lines changed

17 files changed

+255
-78
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ endif()
3232
# Project definition.
3333

3434
project(CasbinCPP
35-
VERSION 1.36.0
35+
VERSION 1.37.0
3636
DESCRIPTION "An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++"
3737
HOMEPAGE_URL https://github.com/casbin/casbin-cpp
3838
LANGUAGES CXX C

bindings/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(SOURCES
1818
py_enforcer.cpp
1919
py_abac_data.cpp
2020
py_model.cpp
21-
)
21+
py_config.cpp)
2222

2323
set(HEADERS
2424
py_casbin.h

bindings/python/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PYBIND11_MODULE(pycasbin, m) {
3838
bindPyCachedEnforcer(m);
3939
bindABACData(m);
4040
bindPyModel(m);
41+
bindPyConfig(m);
4142

4243
m.attr("__version__") = PY_CASBIN_VERSION;
4344
}

bindings/python/py_casbin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ void bindPyEnforcer(py::module &m);
2222
void bindPyCachedEnforcer(py::module &m);
2323
void bindABACData(py::module &m);
2424
void bindPyModel(py::module &m);
25+
void bindPyConfig(py::module &m);

bindings/python/py_config.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 bindPyConfig(py::module &m) {
24+
py::class_<casbin::Config, std::shared_ptr<casbin::Config>>(m, "Config")
25+
.def(py::init<>())
26+
.def(py::init<const std::string&>())
27+
.def_static("NewConfig", &casbin::Config::NewConfig, R"doc(
28+
/**
29+
* NewConfig create an empty configuration representation from file.
30+
*
31+
* @param confName the path of the model file.
32+
* @return the constructor of Config.
33+
*/
34+
)doc")
35+
.def_static("NewConfigFromText", &casbin::Config::NewConfigFromText, R"doc(
36+
/**
37+
* newConfigFromText create an empty configuration representation from text.
38+
*
39+
* @param text the model text.
40+
* @return the constructor of Config.
41+
*/
42+
)doc")
43+
.def("GetBool", &casbin::Config::GetBool)
44+
.def("GetInt", &casbin::Config::GetInt)
45+
.def("GetFloat", &casbin::Config::GetFloat)
46+
.def("GetString", &casbin::Config::GetString)
47+
.def("GetStrings", &casbin::Config::GetStrings)
48+
.def("Set", &casbin::Config::Set)
49+
.def("Get", &casbin::Config::Get);
50+
}

bindings/python/py_model.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020

2121
#include "py_casbin.h"
2222

23+
void PyLoadModelFromConfig(casbin::Model* model, std::shared_ptr<casbin::Config> config) {
24+
model->LoadModelFromConfig(config);
25+
}
26+
2327
void bindPyModel(py::module &m) {
2428
py::class_<casbin::Model, std::shared_ptr<casbin::Model>>(m, "Model")
2529
.def(py::init<>())
2630
.def(py::init<const std::string &>())
2731

32+
.def_readonly_static("required_sections", &casbin::Model::required_sections)
33+
2834
.def("HasSection", &casbin::Model::HasSection)
2935
.def("AddDef", &casbin::Model::AddDef, "AddDef adds an assertion to the model.")
3036
.def("LoadModel", &casbin::Model::LoadModel, "LoadModel loads the model from model CONF file.")

casbin/CMakeLists.txt

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,65 @@
1313
# limitations under the License.
1414

1515
set(CASBIN_SOURCE
16-
abac_data.cpp
17-
enforcer.cpp
18-
enforcer_cached.cpp
19-
enforcer_synced.cpp
20-
internal_api.cpp
21-
logger.cpp
22-
management_api.cpp
23-
pch.cpp
24-
rbac_api.cpp
25-
rbac_api_with_domains.cpp
26-
config/config.cpp
27-
duktape/duktape.cpp
28-
effect/default_effector.cpp
29-
ip_parser/exception/parser_exception.cpp
30-
ip_parser/parser/allFF.cpp
31-
ip_parser/parser/CIDRMask.cpp
32-
ip_parser/parser/dtoi.cpp
33-
ip_parser/parser/equal.cpp
34-
ip_parser/parser/IP.cpp
35-
ip_parser/parser/IPNet.cpp
36-
ip_parser/parser/IPv4.cpp
37-
ip_parser/parser/parseCIDR.cpp
38-
ip_parser/parser/parseIP.cpp
39-
ip_parser/parser/parseIPv4.cpp
40-
ip_parser/parser/parseIPv6.cpp
41-
ip_parser/parser/Print.cpp
42-
ip_parser/parser/xtoi.cpp
43-
model/assertion.cpp
44-
model/function.cpp
45-
model/model.cpp
46-
model/scope_config.cpp
47-
persist/file_adapter/batch_file_adapter.cpp
48-
persist/file_adapter/file_adapter.cpp
49-
persist/file_adapter/filtered_file_adapter.cpp
50-
persist/adapter.cpp
51-
persist/default_watcher.cpp
52-
persist/default_watcher_ex.cpp
53-
rbac/default_role_manager.cpp
54-
util/array_equals.cpp
55-
util/array_remove_duplicates.cpp
56-
util/array_to_string.cpp
57-
util/built_in_functions.cpp
58-
util/ends_with.cpp
59-
util/escape_assertion.cpp
60-
util/find_all_occurences.cpp
61-
util/is_instance_of.cpp
62-
util/join.cpp
63-
util/join_slice.cpp
64-
util/remove_comments.cpp
65-
util/set_subtract.cpp
66-
util/split.cpp
67-
util/ticker.cpp
68-
util/trim.cpp
69-
)
16+
abac_data.cpp
17+
enforcer.cpp
18+
enforcer_cached.cpp
19+
enforcer_synced.cpp
20+
internal_api.cpp
21+
logger.cpp
22+
management_api.cpp
23+
pch.cpp
24+
rbac_api.cpp
25+
rbac_api_with_domains.cpp
26+
config/config.cpp
27+
duktape/duktape.cpp
28+
effect/default_effector.cpp
29+
ip_parser/exception/parser_exception.cpp
30+
ip_parser/parser/allFF.cpp
31+
ip_parser/parser/CIDRMask.cpp
32+
ip_parser/parser/dtoi.cpp
33+
ip_parser/parser/equal.cpp
34+
ip_parser/parser/IP.cpp
35+
ip_parser/parser/IPNet.cpp
36+
ip_parser/parser/IPv4.cpp
37+
ip_parser/parser/parseCIDR.cpp
38+
ip_parser/parser/parseIP.cpp
39+
ip_parser/parser/parseIPv4.cpp
40+
ip_parser/parser/parseIPv6.cpp
41+
ip_parser/parser/Print.cpp
42+
ip_parser/parser/xtoi.cpp
43+
model/assertion.cpp
44+
model/function.cpp
45+
model/model.cpp
46+
model/scope_config.cpp
47+
persist/file_adapter/batch_file_adapter.cpp
48+
persist/file_adapter/file_adapter.cpp
49+
persist/file_adapter/filtered_file_adapter.cpp
50+
persist/adapter.cpp
51+
persist/default_watcher.cpp
52+
persist/default_watcher_ex.cpp
53+
rbac/default_role_manager.cpp
54+
util/array_equals.cpp
55+
util/array_remove_duplicates.cpp
56+
util/array_to_string.cpp
57+
util/built_in_functions.cpp
58+
util/ends_with.cpp
59+
util/escape_assertion.cpp
60+
util/find_all_occurences.cpp
61+
util/is_instance_of.cpp
62+
util/join.cpp
63+
util/join_slice.cpp
64+
util/remove_comments.cpp
65+
util/set_subtract.cpp
66+
util/split.cpp
67+
util/ticker.cpp
68+
util/trim.cpp
69+
)
7070

7171
# Setting to C++ standard to C++17
7272
set(CMAKE_CXX_STANDARD 17)
7373

74-
add_library(casbin ${CASBIN_SOURCE})
74+
add_library(casbin STATIC ${CASBIN_SOURCE})
7575
target_include_directories(casbin PUBLIC ${CMAKE_SOURCE_DIR}/casbin)
7676

7777
target_precompile_headers(casbin PUBLIC "pch.h")
@@ -89,12 +89,13 @@ elseif(UNIX)
8989
)
9090
endif()
9191

92-
install(TARGETS casbin EXPORT CasbinTargets
93-
LIBRARY DESTINATION lib
94-
ARCHIVE DESTINATION lib
95-
RUNTIME DESTINATION bin
96-
INCLUDES DESTINATION include
97-
)
92+
install(
93+
TARGETS casbin EXPORT CasbinTargets
94+
LIBRARY DESTINATION lib
95+
ARCHIVE DESTINATION lib
96+
RUNTIME DESTINATION bin
97+
INCLUDES DESTINATION include
98+
)
9899

99100
install(
100101
DIRECTORY ${CMAKE_SOURCE_DIR}/include/casbin

casbin/config/config.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ std::shared_ptr<Config> Config::NewConfigFromText(const std::string& text) {
114114
return c;
115115
}
116116

117+
Config::Config() {
118+
}
119+
120+
Config::Config(const std::string& conf_name) {
121+
this->Parse(conf_name);
122+
}
123+
117124
bool Config::GetBool(std::string_view key) {
118125
return Get(key).compare("true")==0;
119126
}

casbin/config/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class Config : public ConfigInterface {
6464

6565
bool GetBool(std::string_view key);
6666

67+
Config();
68+
69+
Config(const std::string& conf_name);
70+
6771
int GetInt(std::string_view key);
6872

6973
float GetFloat(std::string_view key);

casbin/model/model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ std::unordered_map<std::string, std::string> Model::section_name_map = {
3939

4040
std::vector<std::string> Model::required_sections{"r","p","e","m"};
4141

42-
void Model::LoadModelFromConfig(std::shared_ptr<ConfigInterface> cfg) {
42+
void Model::LoadModelFromConfig(std::shared_ptr<Config>& cfg) {
4343
for (auto [section_name, _] : section_name_map)
4444
LoadSection(this ,cfg, section_name);
4545

0 commit comments

Comments
 (0)