Skip to content

Commit aeadf83

Browse files
authored
Merge pull request #130 from EmperorYP7/utility-bindings
feat: Added bindings for `casbin::ABACData`
2 parents e176189 + 6cc4926 commit aeadf83

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

bindings/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(SOURCES
1616
main.cpp
1717
py_cached_enforcer.cpp
1818
py_enforcer.cpp
19+
py_abac_data.cpp
1920
)
2021

2122
set(HEADERS

bindings/python/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ PYBIND11_MODULE(pycasbin, m) {
3636

3737
bindPyEnforcer(m);
3838
bindPyCachedEnforcer(m);
39+
bindABACData(m);
3940

4041
m.attr("__version__") = PY_CASBIN_VERSION;
4142
}

bindings/python/py_abac_data.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 bindABACData(py::module& m) {
24+
py::class_<casbin::ABACData>(m, "ABACData")
25+
.def(py::init<const casbin::AttributeMap &>(), R"doc(
26+
@brief Construct a new casbin::ABACData object
27+
28+
@param attribs Should be of the format: {
29+
{ "attrib_name1", value1 },
30+
{ "attring_name2", value2 },
31+
...
32+
}
33+
34+
Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only
35+
)doc")
36+
.def("AddAttribute", &casbin::ABACData::AddAttribute, R"doc(
37+
@brief Add attribute to the corresponding ABAC entity
38+
@param key Name of the attribute
39+
@param value Value of the attribute
40+
@return true when attribute is added successfully, false otherwise
41+
)doc")
42+
.def("AddAttributes", &casbin::ABACData::AddAttributes, R"doc(
43+
@brief Add attributes to the corresponding ABAC entity
44+
45+
@param attribs Should be of the format: {
46+
{ "attrib_name1", value1 },
47+
{ "attring_name2", value2 },
48+
...
49+
}
50+
51+
Key's type is std::string and value's type can be one of std::string, int32_t, and float only
52+
@return true if attributes are added successfully, false otherwise
53+
54+
)doc")
55+
.def("DeleteAttribute", &casbin::ABACData::DeleteAttribute, R"doc(
56+
@brief Delete attribute of the corresponding ABAC entity
57+
@param key Name of the attribute to be deleted
58+
@return true when attribute is deleted successfully, false otherwise
59+
)doc")
60+
.def("UpdateAttribute", &casbin::ABACData::UpdateAttribute, R"doc(
61+
@brief Update attribute of the corresponding ABAC entity
62+
@param key Name of the attribute to be updated
63+
@param value Value which would replace the current value of the attribute corresponding
64+
to the given key
65+
@return true
66+
@return false
67+
)doc");
68+
// .def("GetAttributes", &casbin::ABACData::GetAttributes, R"doc(
69+
// @brief Get the Attributes of the corresponding ABAC entity
70+
// @return const reference to the hashmap containing attributes in key-value pairs
71+
// )doc");
72+
73+
m.def("GetDataObject", &casbin::GetDataObject, R"doc(
74+
@brief Get casbin::ABACData object
75+
@param attribs Should be of the format: {
76+
{ "attrib_name1", value1 },
77+
{ "attrib_name2", value2 },
78+
...
79+
}
80+
Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only
81+
@return Pointer to casbin::ABACData entity
82+
)doc");
83+
}

bindings/python/py_casbin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ namespace py = pybind11;
2020

2121
void bindPyEnforcer(py::module &m);
2222
void bindPyCachedEnforcer(py::module &m);
23+
void bindABACData(py::module &m);

0 commit comments

Comments
 (0)