-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pycasbin adopter and test. (#164)
* fix: fix FileAdapter::SavePolicy Signed-off-by: stonex <1479765922@qq.com> * feat: add pycasbin::Adopter and test Signed-off-by: stonex <1479765922@qq.com>
- Loading branch information
1 parent
7790669
commit 4371da5
Showing
13 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ set(SOURCES | |
py_model.cpp | ||
py_config.cpp | ||
py_synced_enforcer.cpp | ||
py_adapter.cpp | ||
) | ||
|
||
set(HEADERS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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" | ||
|
||
namespace py = pybind11; | ||
|
||
void bindPyBaseAdapter(py::module& m) { | ||
// Base Adapter use shared_ptr to manage | ||
// must expose this interface for enforcer build | ||
py::class_<casbin::Adapter, std::shared_ptr<casbin::Adapter>>(m, "Adapter") | ||
.def("LoadPolicy", &casbin::Adapter::LoadPolicy, "LoadPolicy loads all policy rules from the storage.") | ||
.def("SavePolicy", &casbin::Adapter::SavePolicy, "SavePolicy saves all policy rules to the storage.") | ||
.def("AddPolicy", &casbin::Adapter::AddPolicy, "AddPolicy adds a policy rule to the storage.") | ||
.def("RemovePolicy", &casbin::Adapter::RemovePolicy, "RemovePolicy removes a policy rule from the storage.") | ||
.def("RemoveFilteredPolicy", &casbin::Adapter::RemoveFilteredPolicy, "RemoveFilteredPolicy removes policy rules that match the filter from the storage.") | ||
.def("IsFiltered", &casbin::Adapter::IsFiltered, "IsFiltered returns true if the loaded policy has been filtered."); | ||
} | ||
|
||
void bindPyBatchAdapter(py::module &m) { | ||
py::class_<casbin::BatchAdapter, casbin::Adapter, std::shared_ptr<casbin::BatchAdapter>>(m, "BatchAdapter") | ||
.def("AddPolicies", &casbin::BatchAdapter::AddPolicies, "") | ||
.def("RemovePolicies", &casbin::BatchAdapter::RemovePolicies, ""); | ||
} | ||
|
||
void bindPyFileAdapter(py::module &m) { | ||
// File adapter inhert base adapter and use shared_ptr to manage | ||
py::class_<casbin::FileAdapter, casbin::Adapter, std::shared_ptr<casbin::FileAdapter>>(m, "FileAdapter") | ||
.def(py::init<std::string>(), "") | ||
.def_static("NewFileAdapter", &casbin::FileAdapter::NewFileAdapter, "") | ||
.def("LoadPolicy", &casbin::FileAdapter::LoadPolicy, "LoadPolicy loads all policy rules from the storage.") | ||
.def("SavePolicy", &casbin::FileAdapter::SavePolicy, "SavePolicy saves all policy rules to the storage.") | ||
.def("AddPolicy", &casbin::FileAdapter::AddPolicy, "AddPolicy adds a policy rule to the storage.") | ||
.def("RemovePolicy", &casbin::FileAdapter::RemovePolicy, "RemovePolicy removes a policy rule from the storage.") | ||
.def("RemoveFilteredPolicy", &casbin::FileAdapter::RemoveFilteredPolicy, "RemoveFilteredPolicy removes policy rules that match the filter from the storage.") | ||
.def("IsFiltered", &casbin::FileAdapter::IsFiltered, "IsFiltered returns true if the loaded policy has been filtered."); | ||
} | ||
|
||
void bindPyBatchFileAdapter(py::module &m) { | ||
// Batch Adapter is virtual interface, maybe don't expose its' interface is ok. | ||
py::class_<casbin::BatchFileAdapter, casbin::BatchAdapter, casbin::FileAdapter, std::shared_ptr<casbin::BatchFileAdapter>>(m, "BatchFileAdapter") | ||
.def(py::init<std::string>(), "") | ||
.def_static("NewBatchFileAdapter", &casbin::BatchFileAdapter::NewBatchFileAdapter, "") | ||
.def("AddPolicies", &casbin::BatchFileAdapter::AddPolicies, "") | ||
.def("RemovePolicies", &casbin::BatchFileAdapter::RemovePolicies, ""); | ||
} | ||
|
||
void bindPyAdapter(py::module& m) { | ||
bindPyBaseAdapter(m); | ||
bindPyBatchAdapter(m); | ||
bindPyFileAdapter(m); | ||
bindPyBatchFileAdapter(m); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# 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. | ||
|
||
from logging import setLoggerClass | ||
import pycasbin as casbin | ||
from config_path import * | ||
import unittest | ||
import os | ||
|
||
class TestAdapter(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.Adapter = None | ||
self.Model = None | ||
|
||
def cleanUp(self): | ||
self.Adapter = None | ||
self.Model = None | ||
|
||
def FileAdapterInit(self, path): | ||
self.Adapter = casbin.FileAdapter.NewFileAdapter(path) | ||
|
||
def ModelInit(self, model_path): | ||
self.Model = casbin.Model.NewModelFromFile(model_path) | ||
|
||
def test_NewFileAdapter(self): | ||
self.cleanUp() | ||
self.Adapter = casbin.FileAdapter.NewFileAdapter(basic_policy_path) | ||
self.assertIsNotNone(self.Adapter) | ||
|
||
def test_NewBatchFileAdapter(self): | ||
self.cleanUp() | ||
self.Adapter = casbin.BatchFileAdapter.NewBatchFileAdapter(basic_model_path) | ||
self.assertIsNotNone(self.Adapter) | ||
|
||
def test_FileAdapterLoadPolicy(self): | ||
self.cleanUp() | ||
self.FileAdapterInit(basic_policy_path) | ||
self.ModelInit(basic_model_path) | ||
self.Adapter.LoadPolicy(self.Model) | ||
|
||
FilePolicies = [["alice", "data1", "read"], | ||
["bob", "data2", "write"]] | ||
|
||
self.assertTrue(self.Model.HasPolicy('p', 'p', FilePolicies[0])) | ||
self.assertTrue(self.Model.HasPolicy('p', 'p', FilePolicies[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters