Skip to content

Commit

Permalink
feat: Added ABAC entity wrapper (#105)
Browse files Browse the repository at this point in the history
* feat: Added ABAC entity wrapper

Signed-off-by: Yash Pandey (YP) <yash.btech.cs19@iiitranchi.ac.in>

* feat: renamed abac data

Signed-off-by: Yash Pandey (YP) <yash.btech.cs19@iiitranchi.ac.in>

* feat: updated ABACData

Signed-off-by: Yash Pandey (YP) <yash.btech.cs19@iiitranchi.ac.in>

* fix: added prelude

Signed-off-by: Yash Pandey (YP) <yash.btech.cs19@iiitranchi.ac.in>

* feat: Configured VS project files

Signed-off-by: EmperorYP7 <yash.btech.cs19@iiitranchi.ac.in>

* fix: Removed vector

Signed-off-by: Yash Pandey (YP) <yash.btech.cs19@iiitranchi.ac.in>
  • Loading branch information
EmperorYP7 authored Jun 20, 2021
1 parent 7143cc4 commit f5c0ef9
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 7 deletions.
82 changes: 82 additions & 0 deletions casbin/abac_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 "pch.h"

#ifndef ABAC_CPP
#define ABAC_CPP

#include "abac_data.h"
#include "./model/scope_config.h"

namespace casbin {

/**
* @brief Get casbin::ABACData object
*
* @param attribs Should be of the format: {
* { "attrib_name1", value1 },
* { "attrib_name2", value2 },
* ...
* }
*
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
* @return Pointer to casbin::ABACData entity
*/
static const std::shared_ptr<ABACData> GetData(const ABACData::VariantMap& attribs) {
return std::make_shared<ABACData>(attribs);
}

ABACData::ABACData(const VariantMap& attrib)
: m_attributes(attrib)
{}

bool ABACData::AddAttribute(const std::string& key, const VariantType& value) {
m_attributes[key] = value;
return true;
}

bool ABACData::AddAttributes(const VariantMap& attribs) {
for(auto attrib : attribs) {
m_attributes[attrib.first] = attrib.second;
}
return true;
}

bool ABACData::DeleteAttribute(const std::string& key) {
auto it = m_attributes.find(key);

// If key is not present in the map, indicate deletion failiure
if(it == m_attributes.end()) {
return false;
}

m_attributes.erase(it);
return true;
}

bool ABACData::UpdateAttribute(const std::string& key, const VariantType& value) {
m_attributes[key] = value;
return true;
}

const ABACData::VariantMap& ABACData::GetAttributes() {
return m_attributes;
}

}

#endif // ABAC_CPP
109 changes: 109 additions & 0 deletions casbin/abac_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.
*/

#ifndef ABAC_H
#define ABAC_H

#include <unordered_map>
#include <string>
#include <vector>
#include <variant>
#include <memory>

namespace casbin {

/**
* @brief A wrapper to contain ABAC entity with a list of attributes stored in a hashmap
*
*/
class ABACData {

public:

// Intrinsic definitions
typedef std::variant<std::string, int32_t, float> VariantType;
typedef std::unordered_map<std::string, VariantType> VariantMap;

private:

// HashMap containing attributes as key-value pairs
VariantMap m_attributes;

public:
/**
* @brief Construct a new casbin::ABACData object
*
* @param attribs Should be of the format: {
* { "attrib_name1", value1 },
* { "attring_name2", value2 },
* ...
* }
*
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
*/
ABACData(const VariantMap& attribs);
/**
* @brief Add attribute to the corresponding ABAC entity
*
* @param key Name of the attribute
* @param value Value of the attribute
* @return true when attribute is added successfully, false otherwise
*/
bool AddAttribute(const std::string& key, const VariantType& value);
/**
* @brief Add attributes to the corresponding ABAC entity
*
* @param attribs Should be of the format: {
* { "attrib_name1", value1 },
* { "attring_name2", value2 },
* ...
* }
*
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
* @return true if attributes are added successfully, false otherwise
*/
bool AddAttributes(const VariantMap& attribs);
/**
* @brief Delete attribute of the corresponding ABAC entity
*
* @param key Name of the attribute to be deleted
* @return true when attribute is deleted successfully, false otherwise
*/
bool DeleteAttribute(const std::string& key);
/**
* @brief Update attribute of the corresponding ABAC entity
*
* @param key Name of the attribute to be updated
* @param value Value which would replace the current value of the attribute corresponding
* to the given key
* @return true
* @return false
*/
bool UpdateAttribute(const std::string& key, const VariantType& value);
/**
* @brief Get the Attributes of the corresponding ABAC entity
*
* @return const reference to the hashmap containing attributes in key-value pairs
*/
const VariantMap& GetAttributes();
};

// Casbin ABAC entity type
typedef ABACData ABACData;

}

#endif
4 changes: 4 additions & 0 deletions casbin/casbin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -143,6 +144,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -153,6 +155,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="abac_data.cpp" />
<ClCompile Include="config\config.cpp" />
<ClCompile Include="duktape\duktape.cpp" />
<ClCompile Include="effect\default_effector.cpp" />
Expand Down Expand Up @@ -219,6 +222,7 @@
<ClCompile Include="util\trim.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="abac_data.h" />
<ClInclude Include="config.h" />
<ClInclude Include="config\config.h" />
<ClInclude Include="config\config_interface.h" />
Expand Down
6 changes: 6 additions & 0 deletions casbin/casbin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@
<ClCompile Include="logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="abac_data.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="config\config_interface.h">
Expand Down Expand Up @@ -494,6 +497,9 @@
<ClInclude Include="log\Logger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="abac_data.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
7 changes: 0 additions & 7 deletions casbin/enforcer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ namespace casbin {
// with the operation "action", input parameters are usually: (matcher, sub, obj, act),
// use model matcher by default when matcher is "".
bool Enforcer :: m_enforce(const std::string& matcher, Scope scope) {
// TODO
// defer func() {
// if err := recover(); err != nil {
// fmt.Errorf("panic: %v", err)
// }
// }()

m_func_map.scope = scope;

if(!m_enabled)
Expand Down

0 comments on commit f5c0ef9

Please sign in to comment.