|
| 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 |
0 commit comments