Skip to content

Commit

Permalink
Merge pull request #135 from EmperorYP7/python-test
Browse files Browse the repository at this point in the history
feat: Added bindings for `casbin::Model`
  • Loading branch information
hsluoyz authored Aug 6, 2021
2 parents 8aaa857 + fde0b93 commit 77dccf5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,4 @@ MigrationBackup/

# CMake work directory
cmake-build/
xcode-build/
2 changes: 1 addition & 1 deletion bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(SOURCES
py_cached_enforcer.cpp
py_enforcer.cpp
py_abac_data.cpp
py_model.cpp
)

set(HEADERS
Expand Down Expand Up @@ -65,7 +66,6 @@ endif()

target_link_libraries(pycasbin
PRIVATE
# casbin
pybind11::module
casbin
)
Expand Down
1 change: 1 addition & 0 deletions bindings/python/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ PYBIND11_MODULE(pycasbin, m) {
bindPyEnforcer(m);
bindPyCachedEnforcer(m);
bindABACData(m);
bindPyModel(m);

m.attr("__version__") = PY_CASBIN_VERSION;
}
1 change: 1 addition & 0 deletions bindings/python/py_casbin.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ namespace py = pybind11;
void bindPyEnforcer(py::module &m);
void bindPyCachedEnforcer(py::module &m);
void bindABACData(py::module &m);
void bindPyModel(py::module &m);
55 changes: 55 additions & 0 deletions bindings/python/py_model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 The casbin Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <casbin/casbin.h>

#include "py_casbin.h"

void bindPyModel(py::module &m) {
py::class_<casbin::Model>(m, "Model")
.def(py::init<>())
.def(py::init<const std::string &>())

.def("HasSection", &casbin::Model::HasSection)
.def("AddDef", &casbin::Model::AddDef, "AddDef adds an assertion to the model.")
.def("LoadModel", &casbin::Model::LoadModel, "LoadModel loads the model from model CONF file.")
.def("LoadModelFromText", &casbin::Model::LoadModelFromText, "LoadModelFromText loads the model from the text.")
.def("LoadModelFromConfig", &casbin::Model::LoadModelFromConfig)
.def("PrintModel", &casbin::Model::PrintModel, "PrintModel prints the model to the log.")

.def("BuildIncrementalRoleLinks", &casbin::Model::BuildIncrementalRoleLinks)
.def("BuildRoleLinks", &casbin::Model::BuildRoleLinks, "BuildRoleLinks initializes the roles in RBAC.")
.def("PrintPolicy", &casbin::Model::PrintPolicy, "PrintPolicy prints the policy to log.")
.def("ClearPolicy", &casbin::Model::ClearPolicy, "ClearPolicy clears all current policy.")
.def("GetPolicy", &casbin::Model::GetPolicy, "GetPolicy gets all rules in a policy.")
.def("GetFilteredPolicy", &casbin::Model::GetFilteredPolicy, "GetFilteredPolicy gets rules based on field filters from a policy.")
.def("HasPolicy", &casbin::Model::HasPolicy, "HasPolicy determines whether a model has the specified policy rule.")
.def("AddPolicy", &casbin::Model::AddPolicy, "AddPolicy adds a policy rule to the model.")
.def("AddPolicies", &casbin::Model::AddPolicies, "AddPolicies adds policy rules to the model.")
.def("UpdatePolicy", &casbin::Model::UpdatePolicy, "UpdatePolicy updates a policy rule from the model.")
.def("UpdatePolicies", &casbin::Model::UpdatePolicies, "UpdatePolicies updates a set of policy rules from the model.")
.def("RemovePolicy", &casbin::Model::RemovePolicy, "RemovePolicy removes a policy rule from the model.")
.def("RemovePolicies", &casbin::Model::RemovePolicies, "RemovePolicies removes policy rules from the model.")
.def("RemoveFilteredPolicy", &casbin::Model::RemoveFilteredPolicy, "RemoveFilteredPolicy removes policy rules based on field filters from the model.")
.def("GetValuesForFieldInPolicy", &casbin::Model::GetValuesForFieldInPolicy, "GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.")
.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.")

.def_static("NewModel", &casbin::Model::NewModel, "NewModel creates an empty model.")
.def_static("NewModelFromFile", &casbin::Model::NewModelFromFile, "NewModelFromFile creates a model from a .CONF file.")
.def_static("NewModelFromString", &casbin::Model::NewModelFromString, "NewModel creates a model from a std::string which contains model text.");
}
3 changes: 3 additions & 0 deletions tests/python/pycasbin_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import pycasbin
import test_enforcer
import test_model

def suite():

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

suite.addTest(loader.loadTestsFromModule(test_enforcer))
suite.addTest(loader.loadTestsFromModule(test_model))

return suite


Expand Down
32 changes: 32 additions & 0 deletions tests/python/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021 The casbin Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pycasbin as casbin
from config_path import *
import unittest

class TestModel(unittest.TestCase):
def test_NewModel(self):
model = casbin.Model.NewModel()
self.assertIsNotNone(model)

def test_NewModelFromFile(self):
model = casbin.Model.NewModelFromFile(basic_model_path)
self.assertIsNotNone(model)

# def test_NewModelFromString(self):
# with open(basic_model_path, 'r') as model_file:
# model_string = model_file.read().replace('\n', '')
# model = casbin.Model.NewModelFromString(model_string)
# self.assertIsNotNone(model)

0 comments on commit 77dccf5

Please sign in to comment.