Skip to content

Commit f5c0ef9

Browse files
authored
feat: Added ABAC entity wrapper (#105)
* feat: Added ABAC entity wrapper Signed-off-by: Yash Pandey (YP) <[email protected]> * feat: renamed abac data Signed-off-by: Yash Pandey (YP) <[email protected]> * feat: updated ABACData Signed-off-by: Yash Pandey (YP) <[email protected]> * fix: added prelude Signed-off-by: Yash Pandey (YP) <[email protected]> * feat: Configured VS project files Signed-off-by: EmperorYP7 <[email protected]> * fix: Removed vector Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 7143cc4 commit f5c0ef9

File tree

5 files changed

+201
-7
lines changed

5 files changed

+201
-7
lines changed

casbin/abac_data.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 "pch.h"
18+
19+
#ifndef ABAC_CPP
20+
#define ABAC_CPP
21+
22+
#include "abac_data.h"
23+
#include "./model/scope_config.h"
24+
25+
namespace casbin {
26+
27+
/**
28+
* @brief Get casbin::ABACData object
29+
*
30+
* @param attribs Should be of the format: {
31+
* { "attrib_name1", value1 },
32+
* { "attrib_name2", value2 },
33+
* ...
34+
* }
35+
*
36+
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
37+
* @return Pointer to casbin::ABACData entity
38+
*/
39+
static const std::shared_ptr<ABACData> GetData(const ABACData::VariantMap& attribs) {
40+
return std::make_shared<ABACData>(attribs);
41+
}
42+
43+
ABACData::ABACData(const VariantMap& attrib)
44+
: m_attributes(attrib)
45+
{}
46+
47+
bool ABACData::AddAttribute(const std::string& key, const VariantType& value) {
48+
m_attributes[key] = value;
49+
return true;
50+
}
51+
52+
bool ABACData::AddAttributes(const VariantMap& attribs) {
53+
for(auto attrib : attribs) {
54+
m_attributes[attrib.first] = attrib.second;
55+
}
56+
return true;
57+
}
58+
59+
bool ABACData::DeleteAttribute(const std::string& key) {
60+
auto it = m_attributes.find(key);
61+
62+
// If key is not present in the map, indicate deletion failiure
63+
if(it == m_attributes.end()) {
64+
return false;
65+
}
66+
67+
m_attributes.erase(it);
68+
return true;
69+
}
70+
71+
bool ABACData::UpdateAttribute(const std::string& key, const VariantType& value) {
72+
m_attributes[key] = value;
73+
return true;
74+
}
75+
76+
const ABACData::VariantMap& ABACData::GetAttributes() {
77+
return m_attributes;
78+
}
79+
80+
}
81+
82+
#endif // ABAC_CPP

casbin/abac_data.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
#ifndef ABAC_H
18+
#define ABAC_H
19+
20+
#include <unordered_map>
21+
#include <string>
22+
#include <vector>
23+
#include <variant>
24+
#include <memory>
25+
26+
namespace casbin {
27+
28+
/**
29+
* @brief A wrapper to contain ABAC entity with a list of attributes stored in a hashmap
30+
*
31+
*/
32+
class ABACData {
33+
34+
public:
35+
36+
// Intrinsic definitions
37+
typedef std::variant<std::string, int32_t, float> VariantType;
38+
typedef std::unordered_map<std::string, VariantType> VariantMap;
39+
40+
private:
41+
42+
// HashMap containing attributes as key-value pairs
43+
VariantMap m_attributes;
44+
45+
public:
46+
/**
47+
* @brief Construct a new casbin::ABACData object
48+
*
49+
* @param attribs Should be of the format: {
50+
* { "attrib_name1", value1 },
51+
* { "attring_name2", value2 },
52+
* ...
53+
* }
54+
*
55+
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
56+
*/
57+
ABACData(const VariantMap& attribs);
58+
/**
59+
* @brief Add attribute to the corresponding ABAC entity
60+
*
61+
* @param key Name of the attribute
62+
* @param value Value of the attribute
63+
* @return true when attribute is added successfully, false otherwise
64+
*/
65+
bool AddAttribute(const std::string& key, const VariantType& value);
66+
/**
67+
* @brief Add attributes to the corresponding ABAC entity
68+
*
69+
* @param attribs Should be of the format: {
70+
* { "attrib_name1", value1 },
71+
* { "attring_name2", value2 },
72+
* ...
73+
* }
74+
*
75+
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
76+
* @return true if attributes are added successfully, false otherwise
77+
*/
78+
bool AddAttributes(const VariantMap& attribs);
79+
/**
80+
* @brief Delete attribute of the corresponding ABAC entity
81+
*
82+
* @param key Name of the attribute to be deleted
83+
* @return true when attribute is deleted successfully, false otherwise
84+
*/
85+
bool DeleteAttribute(const std::string& key);
86+
/**
87+
* @brief Update attribute of the corresponding ABAC entity
88+
*
89+
* @param key Name of the attribute to be updated
90+
* @param value Value which would replace the current value of the attribute corresponding
91+
* to the given key
92+
* @return true
93+
* @return false
94+
*/
95+
bool UpdateAttribute(const std::string& key, const VariantType& value);
96+
/**
97+
* @brief Get the Attributes of the corresponding ABAC entity
98+
*
99+
* @return const reference to the hashmap containing attributes in key-value pairs
100+
*/
101+
const VariantMap& GetAttributes();
102+
};
103+
104+
// Casbin ABAC entity type
105+
typedef ABACData ABACData;
106+
107+
}
108+
109+
#endif

casbin/casbin.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<ConformanceMode>true</ConformanceMode>
107107
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
108108
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
109+
<LanguageStandard>stdcpp17</LanguageStandard>
109110
</ClCompile>
110111
<Link>
111112
<SubSystem>Windows</SubSystem>
@@ -143,6 +144,7 @@
143144
<ConformanceMode>true</ConformanceMode>
144145
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
145146
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
147+
<LanguageStandard>stdcpp17</LanguageStandard>
146148
</ClCompile>
147149
<Link>
148150
<SubSystem>Windows</SubSystem>
@@ -153,6 +155,7 @@
153155
</Link>
154156
</ItemDefinitionGroup>
155157
<ItemGroup>
158+
<ClCompile Include="abac_data.cpp" />
156159
<ClCompile Include="config\config.cpp" />
157160
<ClCompile Include="duktape\duktape.cpp" />
158161
<ClCompile Include="effect\default_effector.cpp" />
@@ -219,6 +222,7 @@
219222
<ClCompile Include="util\trim.cpp" />
220223
</ItemGroup>
221224
<ItemGroup>
225+
<ClInclude Include="abac_data.h" />
222226
<ClInclude Include="config.h" />
223227
<ClInclude Include="config\config.h" />
224228
<ClInclude Include="config\config_interface.h" />

casbin/casbin.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@
264264
<ClCompile Include="logger.cpp">
265265
<Filter>Source Files</Filter>
266266
</ClCompile>
267+
<ClCompile Include="abac_data.cpp">
268+
<Filter>Source Files</Filter>
269+
</ClCompile>
267270
</ItemGroup>
268271
<ItemGroup>
269272
<ClInclude Include="config\config_interface.h">
@@ -494,6 +497,9 @@
494497
<ClInclude Include="log\Logger.h">
495498
<Filter>Header Files</Filter>
496499
</ClInclude>
500+
<ClInclude Include="abac_data.h">
501+
<Filter>Header Files</Filter>
502+
</ClInclude>
497503
<ClInclude Include="pch.h">
498504
<Filter>Header Files</Filter>
499505
</ClInclude>

casbin/enforcer.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ namespace casbin {
3737
// with the operation "action", input parameters are usually: (matcher, sub, obj, act),
3838
// use model matcher by default when matcher is "".
3939
bool Enforcer :: m_enforce(const std::string& matcher, Scope scope) {
40-
// TODO
41-
// defer func() {
42-
// if err := recover(); err != nil {
43-
// fmt.Errorf("panic: %v", err)
44-
// }
45-
// }()
46-
4740
m_func_map.scope = scope;
4841

4942
if(!m_enabled)

0 commit comments

Comments
 (0)