Skip to content

Commit fde0b93

Browse files
committed
feat: Added bindings for model
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent aeadf83 commit fde0b93

File tree

7 files changed

+94
-1
lines changed

7 files changed

+94
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,4 @@ MigrationBackup/
359359

360360
# CMake work directory
361361
cmake-build/
362+
xcode-build/

bindings/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(SOURCES
1717
py_cached_enforcer.cpp
1818
py_enforcer.cpp
1919
py_abac_data.cpp
20+
py_model.cpp
2021
)
2122

2223
set(HEADERS
@@ -65,7 +66,6 @@ endif()
6566

6667
target_link_libraries(pycasbin
6768
PRIVATE
68-
# casbin
6969
pybind11::module
7070
casbin
7171
)

bindings/python/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ PYBIND11_MODULE(pycasbin, m) {
3737
bindPyEnforcer(m);
3838
bindPyCachedEnforcer(m);
3939
bindABACData(m);
40+
bindPyModel(m);
4041

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

bindings/python/py_casbin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ namespace py = pybind11;
2121
void bindPyEnforcer(py::module &m);
2222
void bindPyCachedEnforcer(py::module &m);
2323
void bindABACData(py::module &m);
24+
void bindPyModel(py::module &m);

bindings/python/py_model.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#include "py_casbin.h"
22+
23+
void bindPyModel(py::module &m) {
24+
py::class_<casbin::Model>(m, "Model")
25+
.def(py::init<>())
26+
.def(py::init<const std::string &>())
27+
28+
.def("HasSection", &casbin::Model::HasSection)
29+
.def("AddDef", &casbin::Model::AddDef, "AddDef adds an assertion to the model.")
30+
.def("LoadModel", &casbin::Model::LoadModel, "LoadModel loads the model from model CONF file.")
31+
.def("LoadModelFromText", &casbin::Model::LoadModelFromText, "LoadModelFromText loads the model from the text.")
32+
.def("LoadModelFromConfig", &casbin::Model::LoadModelFromConfig)
33+
.def("PrintModel", &casbin::Model::PrintModel, "PrintModel prints the model to the log.")
34+
35+
.def("BuildIncrementalRoleLinks", &casbin::Model::BuildIncrementalRoleLinks)
36+
.def("BuildRoleLinks", &casbin::Model::BuildRoleLinks, "BuildRoleLinks initializes the roles in RBAC.")
37+
.def("PrintPolicy", &casbin::Model::PrintPolicy, "PrintPolicy prints the policy to log.")
38+
.def("ClearPolicy", &casbin::Model::ClearPolicy, "ClearPolicy clears all current policy.")
39+
.def("GetPolicy", &casbin::Model::GetPolicy, "GetPolicy gets all rules in a policy.")
40+
.def("GetFilteredPolicy", &casbin::Model::GetFilteredPolicy, "GetFilteredPolicy gets rules based on field filters from a policy.")
41+
.def("HasPolicy", &casbin::Model::HasPolicy, "HasPolicy determines whether a model has the specified policy rule.")
42+
.def("AddPolicy", &casbin::Model::AddPolicy, "AddPolicy adds a policy rule to the model.")
43+
.def("AddPolicies", &casbin::Model::AddPolicies, "AddPolicies adds policy rules to the model.")
44+
.def("UpdatePolicy", &casbin::Model::UpdatePolicy, "UpdatePolicy updates a policy rule from the model.")
45+
.def("UpdatePolicies", &casbin::Model::UpdatePolicies, "UpdatePolicies updates a set of policy rules from the model.")
46+
.def("RemovePolicy", &casbin::Model::RemovePolicy, "RemovePolicy removes a policy rule from the model.")
47+
.def("RemovePolicies", &casbin::Model::RemovePolicies, "RemovePolicies removes policy rules from the model.")
48+
.def("RemoveFilteredPolicy", &casbin::Model::RemoveFilteredPolicy, "RemoveFilteredPolicy removes policy rules based on field filters from the model.")
49+
.def("GetValuesForFieldInPolicy", &casbin::Model::GetValuesForFieldInPolicy, "GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.")
50+
.def("GetValuesForFieldInPolicyAllTypes", &casbin::Model::GetValuesForFieldInPolicyAllTypes, "GetValuesForFieldInPolicyAllTypes gets all values for a field for all rules in a policy of all p_types, duplicated values are removed.")
51+
52+
.def_static("NewModel", &casbin::Model::NewModel, "NewModel creates an empty model.")
53+
.def_static("NewModelFromFile", &casbin::Model::NewModelFromFile, "NewModelFromFile creates a model from a .CONF file.")
54+
.def_static("NewModelFromString", &casbin::Model::NewModelFromString, "NewModel creates a model from a std::string which contains model text.");
55+
}

tests/python/pycasbin_test_suite.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import pycasbin
1919
import test_enforcer
20+
import test_model
2021

2122
def suite():
2223

@@ -25,6 +26,8 @@ def suite():
2526
loader = unittest.TestLoader()
2627

2728
suite.addTest(loader.loadTestsFromModule(test_enforcer))
29+
suite.addTest(loader.loadTestsFromModule(test_model))
30+
2831
return suite
2932

3033

tests/python/test_model.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2021 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pycasbin as casbin
16+
from config_path import *
17+
import unittest
18+
19+
class TestModel(unittest.TestCase):
20+
def test_NewModel(self):
21+
model = casbin.Model.NewModel()
22+
self.assertIsNotNone(model)
23+
24+
def test_NewModelFromFile(self):
25+
model = casbin.Model.NewModelFromFile(basic_model_path)
26+
self.assertIsNotNone(model)
27+
28+
# def test_NewModelFromString(self):
29+
# with open(basic_model_path, 'r') as model_file:
30+
# model_string = model_file.read().replace('\n', '')
31+
# model = casbin.Model.NewModelFromString(model_string)
32+
# self.assertIsNotNone(model)

0 commit comments

Comments
 (0)