|
| 1 | +#include "casbin/pch.h" |
| 2 | + |
| 3 | +#ifndef STRING_ADAPTER_CPP |
| 4 | +#define STRING_ADAPTER_CPP |
| 5 | + |
| 6 | +#include <fstream> |
| 7 | + |
| 8 | +#include "casbin/exception/casbin_adapter_exception.h" |
| 9 | +#include "casbin/exception/io_exception.h" |
| 10 | +#include "casbin/exception/unsupported_operation_exception.h" |
| 11 | +#include "casbin/persist/string_adapter.h" |
| 12 | +#include "casbin/util/util.h" |
| 13 | + |
| 14 | +namespace casbin { |
| 15 | + |
| 16 | +// NewAdapter is the constructor for Adapter. |
| 17 | +StringAdapter ::StringAdapter(std::string line) { |
| 18 | + this->line = line; |
| 19 | + this->filtered = false; |
| 20 | +} |
| 21 | + |
| 22 | +std::shared_ptr<casbin::StringAdapter> StringAdapter::NewStringAdapter(std::string line) { |
| 23 | + return std::make_shared<StringAdapter>(line); |
| 24 | +} |
| 25 | + |
| 26 | +// LoadPolicy loads all policy rules from the string buffer. |
| 27 | +void StringAdapter ::LoadPolicy(const std::shared_ptr<Model>& model) { |
| 28 | + if (this->line == "") |
| 29 | + throw CasbinAdapterException("Invalid line, line cannot be empty"); |
| 30 | + |
| 31 | + std::vector<std::string> strs = Split(this->line, "\n", -1); |
| 32 | + for (int i = 0; i < strs.size(); i++) |
| 33 | + LoadPolicyLine(strs[i], model); |
| 34 | +} |
| 35 | + |
| 36 | +// SavePolicy saves all policy rules to the string buffer. |
| 37 | +void StringAdapter ::SavePolicy(const std::shared_ptr<Model>& model) { |
| 38 | + if (this->line == "") { |
| 39 | + throw CasbinAdapterException("Invalid line, line cannot be empty"); |
| 40 | + } |
| 41 | + |
| 42 | + std::string tmp; |
| 43 | + |
| 44 | + for (std::unordered_map<std::string, std::shared_ptr<Assertion>>::iterator it = model->m["p"].assertion_map.begin(); it != model->m["p"].assertion_map.end(); it++) { |
| 45 | + for (int i = 0; i < it->second->policy.size(); i++) { |
| 46 | + tmp += it->first + ", "; |
| 47 | + tmp += ArrayToString(it->second->policy[i]); |
| 48 | + tmp += "\n"; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + for (std::unordered_map<std::string, std::shared_ptr<Assertion>>::iterator it = model->m["g"].assertion_map.begin(); it != model->m["g"].assertion_map.end(); it++) { |
| 53 | + for (int i = 0; i < it->second->policy.size(); i++) { |
| 54 | + tmp += it->first + ", "; |
| 55 | + tmp += ArrayToString(it->second->policy[i]); |
| 56 | + tmp += "\n"; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + this->line = RTrim(tmp, "\n"); |
| 61 | +} |
| 62 | + |
| 63 | +// AddPolicy adds a policy rule to the string buffer. |
| 64 | +void StringAdapter ::AddPolicy(std::string sec, std::string p_type, std::vector<std::string> rule) { |
| 65 | + throw UnsupportedOperationException("not implemented"); |
| 66 | +} |
| 67 | + |
| 68 | +// RemovePolicy removes a policy rule from the string buffer. |
| 69 | +void StringAdapter ::RemovePolicy(std::string sec, std::string p_type, std::vector<std::string> rule) { |
| 70 | + this->line = ""; |
| 71 | +} |
| 72 | + |
| 73 | +// RemoveFilteredPolicy removes policy rules that match the filter from the string buffer. |
| 74 | +void StringAdapter ::RemoveFilteredPolicy(std::string sec, std::string p_type, int field_index, std::vector<std::string> field_values) { |
| 75 | + throw UnsupportedOperationException("not implemented"); |
| 76 | +} |
| 77 | + |
| 78 | +// IsFiltered returns true if the loaded policy has been filtered. |
| 79 | +bool StringAdapter ::IsFiltered() { |
| 80 | + return this->filtered; |
| 81 | +} |
| 82 | + |
| 83 | +// IsValid returns true if the loaded policy is valid. |
| 84 | +bool StringAdapter ::IsValid() { |
| 85 | + return this->line != ""; |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace casbin |
| 89 | + |
| 90 | +#endif // STRING_ADAPTER_CPP |
0 commit comments