| #include "redfish_subtree_privileges_trie_node.h" |
| |
| #include <memory> |
| #include <string_view> |
| #include <utility> |
| #include <vector> |
| |
| #include "absl/container/flat_hash_set.h" |
| #include "authorizer_enums.h" |
| #include "redfish_privileges.h" |
| |
| namespace milotic::authz::internal { |
| |
| RedfishSubtreePrivilegesTrieNode* |
| RedfishSubtreePrivilegesTrieNode::GetOrDefaultChild(std::string_view path) { |
| std::unique_ptr<RedfishSubtreePrivilegesTrieNode>& node = children_[path]; |
| if (node == nullptr) { |
| node = std::make_unique<RedfishSubtreePrivilegesTrieNode>(); |
| return node.get(); |
| } |
| |
| return node.get(); |
| } |
| |
| RedfishSubtreePrivilegesTrieNode* RedfishSubtreePrivilegesTrieNode::GetChild( |
| std::string_view path) const { |
| auto it = children_.find(path); |
| if (it == children_.end()) { |
| return nullptr; |
| } |
| |
| return it->second.get(); |
| } |
| |
| bool RedfishSubtreePrivilegesTrieNode::ContainsPrivilegeForSubtree( |
| const ecclesia::Operation& operation, |
| const RedfishPrivileges& privilege) const { |
| auto it = subtree_privileges_.find(operation); |
| if (it == subtree_privileges_.end()) { |
| return false; |
| } |
| return it->second->contains(privilege); |
| } |
| |
| void RedfishSubtreePrivilegesTrieNode::AddPrivilegeForSubtree( |
| const ecclesia::Operation& operation, RedfishPrivileges&& privilege) { |
| std::unique_ptr<absl::flat_hash_set<RedfishPrivileges>>& privileges = |
| subtree_privileges_[operation]; |
| if (privileges == nullptr) { |
| privileges = std::make_unique<absl::flat_hash_set<RedfishPrivileges>>(); |
| } |
| privileges->insert(std::move(privilege)); |
| } |
| |
| std::vector<RedfishPrivileges> |
| RedfishSubtreePrivilegesTrieNode::GetPrivilegesForSubtree( |
| const ecclesia::Operation& operation) const { |
| auto it = subtree_privileges_.find(operation); |
| if (it == subtree_privileges_.end()) { |
| return std::vector<RedfishPrivileges>(); |
| } |
| return std::vector<RedfishPrivileges>(it->second->begin(), it->second->end()); |
| } |
| |
| void RedfishSubtreePrivilegesTrieNode::Clear() { |
| subtree_privileges_.clear(); |
| for (auto it = children_.begin(); it != children_.end(); ++it) { |
| it->second->Clear(); |
| } |
| children_.clear(); |
| } |
| } // namespace milotic::authz::internal |