Skip to content

Commit 367254b

Browse files
authored
feat: add json parser to build attribute in duktape (#166)
* feat: add json parser to build attribute in duktape Signed-off-by: stonex <[email protected]> * feat: implement test basic ABAC model without policy using json. Signed-off-by: stonex <[email protected]>
1 parent 4371da5 commit 367254b

File tree

15 files changed

+175
-3
lines changed

15 files changed

+175
-3
lines changed

bindings/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ target_link_libraries(pycasbin
7171
PRIVATE
7272
pybind11::module
7373
casbin
74+
nlohmann_json::nlohmann_json
7475
)
7576

7677
if(WIN32)

casbin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_library(casbin STATIC ${CASBIN_SOURCE_FILES})
7575

7676
target_precompile_headers(casbin PRIVATE pch.h)
7777
target_include_directories(casbin PRIVATE ${CASBIN_SOURCE_DIR})
78+
target_link_libraries(casbin PRIVATE nlohmann_json::nlohmann_json)
7879

7980
set_target_properties(casbin PROPERTIES
8081
PREFIX ""

casbin/data_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
#include <vector>
1919
#include <initializer_list>
2020
#include <unordered_map>
21+
#include <nlohmann/json.hpp>
2122
#include "abac_data.h"
2223

2324
namespace casbin {
2425

25-
typedef std::variant<std::string, std::shared_ptr<ABACData>> Data;
26+
typedef std::variant<std::string, std::shared_ptr<ABACData>, std::shared_ptr<nlohmann::json>> Data;
2627
typedef std::vector<Data> DataVector;
2728
typedef std::initializer_list<Data> DataList;
2829
typedef std::unordered_map<std::string, Data> DataMap;

casbin/enforcer.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,14 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataList& pa
501501
else
502502
throw CasbinEnforcerException("Not a valid type");
503503
}
504+
} else if (const auto json_param = std::get_if<std::shared_ptr<nlohmann::json>>(&param)) {
505+
506+
auto data_ptr = *json_param;
507+
std::string token_name = r_tokens[i].substr(2, r_tokens[i].size() - 2);
508+
509+
PushObject(scope, token_name);
510+
PushObjectPropFromJson(scope, *data_ptr, token_name);
511+
PushObjectPropToObject(scope, "r", token_name);
504512
}
505513
++i;
506514
}
@@ -556,7 +564,16 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataVector&
556564
else
557565
throw CasbinEnforcerException("Not a valid type");
558566
}
567+
} else if (const auto json_param = std::get_if<std::shared_ptr<nlohmann::json>>(&param)) {
568+
569+
auto data_ptr = *json_param;
570+
std::string token_name = r_tokens[i].substr(2, r_tokens[i].size() - 2);
571+
572+
PushObject(scope, token_name);
573+
PushObjectPropFromJson(scope, *data_ptr, token_name);
574+
PushObjectPropToObject(scope, "r", token_name);
559575
}
576+
560577
++i;
561578
}
562579

@@ -601,7 +618,14 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataMap& par
601618
else
602619
throw CasbinEnforcerException("Not a valid type");
603620
}
621+
} else if (const auto json_param = std::get_if<std::shared_ptr<nlohmann::json>>(&param_data)) {
622+
623+
auto data_ptr = *json_param;
624+
PushObject(scope, param_name);
625+
PushObjectPropFromJson(scope, *data_ptr, param_name);
626+
PushObjectPropToObject(scope, "r", param_name);
604627
}
628+
605629
}
606630

607631
bool result = m_enforce(matcher, scope);

casbin/model/scope_config.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
#include "./scope_config.h"
24-
24+
#include "./exception/illegal_argument_exception.h"
2525
namespace casbin {
2626

2727
Scope InitializeScope() {
@@ -194,6 +194,38 @@ void PushObjectPropToObject(Scope scope, std::string obj, std::string identifier
194194
duk_eval_string_noresult(scope, (obj+"len += 1;").c_str());
195195
}
196196

197+
void PushObjectPropFromJson(Scope scope, nlohmann::json& j, std::string objName) {
198+
if (j.is_null()) {
199+
return;
200+
}
201+
202+
for (auto& curJson: j.items()) {
203+
auto key = curJson.key();
204+
auto value = curJson.value();
205+
if (value.is_object()) {
206+
207+
auto nextJsonName = key + "__";
208+
PushObject(scope, nextJsonName);
209+
210+
PushObjectPropFromJson(scope, value, nextJsonName);
211+
212+
duk_get_global_string(scope, objName.c_str());
213+
duk_get_global_string(scope, nextJsonName.c_str());
214+
duk_put_prop_string(scope, -2, key.c_str());
215+
} else if (value.is_number_float()) {
216+
PushDoublePropToObject(scope, objName, value, key);
217+
} else if (value.is_number_integer()) {
218+
PushIntPropToObject(scope, objName, value, key);
219+
} else if (value.is_string()) {
220+
PushStringPropToObject(scope, objName, value, key);
221+
} else if (value.is_boolean()) {
222+
PushBooleanPropToObject(scope, objName, value, key);
223+
} else {
224+
throw IllegalArgumentException("Unsupported json value type");
225+
}
226+
}
227+
}
228+
197229
Type CheckType(Scope scope){
198230
if(duk_is_boolean(scope, -1))
199231
return Type::Bool;

casbin/model/scope_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void PushDoublePropToObject(Scope scope, std::string obj, double d, std::string
6969
void PushStringPropToObject(Scope scope, std::string obj, std::string s, std::string identifier);
7070
void PushPointerPropToObject(Scope scope, std::string obj, void * ptr, std::string identifier);
7171
void PushObjectPropToObject(Scope scope, std::string obj, std::string identifier);
72+
void PushObjectPropFromJson(Scope scope, nlohmann::json& j, std::string j_name);
7273
Type CheckType(Scope scope);
7374
bool FetchIdentifier(Scope scope, std::string identifier);
7475
unsigned int Size(Scope scope);

cmake/modules/FindExtPackages.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ set(CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY ON CACHE BOOL
2424
###############################################################################
2525
### Packages and versions ###
2626

27+
find_package(json 3.7.3 REQUIRED)
28+
2729
if(CASBIN_BUILD_TEST)
2830
# googletest
2931
# https://github.com/google/googletest

cmake/modules/Findjson.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
include(FetchContent)
16+
17+
FetchContent_Declare(json
18+
GIT_REPOSITORY https://github.com/nlohmann/json.git
19+
GIT_TAG v3.7.3)
20+
21+
set(JSON_BuildTests OFF CACHE INTERNAL "")
22+
FetchContent_GetProperties(json)
23+
FetchContent_MakeAvailable(json)
24+
25+
if(NOT json_POPULATED)
26+
FetchContent_Populate(json)
27+
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
28+
endif()

include/casbin/casbin_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ namespace casbin {
544544
void PushStringPropToObject(Scope scope, std::string obj, std::string s, std::string identifier);
545545
void PushPointerPropToObject(Scope scope, std::string obj, void * ptr, std::string identifier);
546546
void PushObjectPropToObject(Scope scope, std::string obj, std::string identifier);
547+
void PushObjectPropFromJson(Scope scope, nlohmann::json& j, std::string j_name);
547548
Type CheckType(Scope scope);
548549
bool FetchIdentifier(Scope scope, std::string identifier);
549550
unsigned int Size(Scope scope);

include/casbin/casbin_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <initializer_list>
2626
#include <memory>
2727
#include <unordered_map>
28+
#include <nlohmann/json.hpp>
2829

2930
namespace casbin {
3031

@@ -121,7 +122,7 @@ namespace casbin {
121122
*/
122123
const std::shared_ptr<ABACData> GetDataObject(const AttributeMap& attribs);
123124

124-
typedef std::variant<std::string, std::shared_ptr<ABACData>> Data;
125+
typedef std::variant<std::string, std::shared_ptr<ABACData>, std::shared_ptr<nlohmann::json>> Data;
125126
typedef std::vector<Data> DataVector;
126127
typedef std::initializer_list<Data> DataList;
127128
typedef std::unordered_map<std::string, Data> DataMap;

0 commit comments

Comments
 (0)