-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add StringAdapter for Enforcer to pass policy as string
- Loading branch information
1 parent
5d9e56c
commit 1279e0a
Showing
9 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "casbin/pch.h" | ||
|
||
#ifndef STRING_ADAPTER_CPP | ||
#define STRING_ADAPTER_CPP | ||
|
||
#include <fstream> | ||
|
||
#include "casbin/exception/casbin_adapter_exception.h" | ||
#include "casbin/exception/io_exception.h" | ||
#include "casbin/exception/unsupported_operation_exception.h" | ||
#include "casbin/persist/string_adapter.h" | ||
#include "casbin/util/util.h" | ||
|
||
namespace casbin { | ||
|
||
// NewAdapter is the constructor for Adapter. | ||
StringAdapter ::StringAdapter(std::string line) { | ||
this->line = line; | ||
this->filtered = false; | ||
} | ||
|
||
std::shared_ptr<casbin::StringAdapter> StringAdapter::NewStringAdapter(std::string line) { | ||
return std::make_shared<StringAdapter>(line); | ||
} | ||
|
||
// LoadPolicy loads all policy rules from the string buffer. | ||
void StringAdapter ::LoadPolicy(const std::shared_ptr<Model>& model) { | ||
if (this->line == "") | ||
throw CasbinAdapterException("Invalid line, line cannot be empty"); | ||
|
||
std::vector<std::string> strs = Split(this->line, "\n", -1); | ||
for (int i = 0; i < strs.size(); i++) | ||
LoadPolicyLine(strs[i], model); | ||
} | ||
|
||
// SavePolicy saves all policy rules to the string buffer. | ||
void StringAdapter ::SavePolicy(const std::shared_ptr<Model>& model) { | ||
if (this->line == "") { | ||
throw CasbinAdapterException("Invalid line, line cannot be empty"); | ||
} | ||
|
||
std::string tmp; | ||
|
||
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++) { | ||
for (int i = 0; i < it->second->policy.size(); i++) { | ||
tmp += it->first + ", "; | ||
tmp += ArrayToString(it->second->policy[i]); | ||
tmp += "\n"; | ||
} | ||
} | ||
|
||
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++) { | ||
for (int i = 0; i < it->second->policy.size(); i++) { | ||
tmp += it->first + ", "; | ||
tmp += ArrayToString(it->second->policy[i]); | ||
tmp += "\n"; | ||
} | ||
} | ||
|
||
this->line = RTrim(tmp, "\n"); | ||
} | ||
|
||
// AddPolicy adds a policy rule to the string buffer. | ||
void StringAdapter ::AddPolicy(std::string sec, std::string p_type, std::vector<std::string> rule) { | ||
throw UnsupportedOperationException("not implemented"); | ||
} | ||
|
||
// RemovePolicy removes a policy rule from the string buffer. | ||
void StringAdapter ::RemovePolicy(std::string sec, std::string p_type, std::vector<std::string> rule) { | ||
this->line = ""; | ||
} | ||
|
||
// RemoveFilteredPolicy removes policy rules that match the filter from the string buffer. | ||
void StringAdapter ::RemoveFilteredPolicy(std::string sec, std::string p_type, int field_index, std::vector<std::string> field_values) { | ||
throw UnsupportedOperationException("not implemented"); | ||
} | ||
|
||
// IsFiltered returns true if the loaded policy has been filtered. | ||
bool StringAdapter ::IsFiltered() { | ||
return this->filtered; | ||
} | ||
|
||
// IsFiltered returns true if the loaded policy has been filtered. | ||
bool StringAdapter ::IsValid() { | ||
return this->line != ""; | ||
} | ||
|
||
} // namespace casbin | ||
|
||
#endif // STRING_ADAPTER_CPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER | ||
#define CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER | ||
|
||
#include "./adapter.h" | ||
|
||
namespace casbin { | ||
|
||
// Adapter is the string adapter for Casbin. | ||
// It can load policy from string buffer or save policy to string buffer. | ||
class StringAdapter : virtual public Adapter { | ||
public: | ||
std::string line; | ||
|
||
// NewAdapter is the constructor for Adapter. | ||
StringAdapter(std::string line); | ||
|
||
static std::shared_ptr<StringAdapter> NewStringAdapter(std::string line); | ||
|
||
// LoadPolicy loads all policy rules from the string buffer. | ||
void LoadPolicy(const std::shared_ptr<Model>& model); | ||
|
||
// SavePolicy saves all policy rules to the string buffer. | ||
void SavePolicy(const std::shared_ptr<Model>& model); | ||
|
||
// AddPolicy adds a policy rule to the string buffer. | ||
void AddPolicy(std::string sec, std::string p_type, std::vector<std::string> rule); | ||
|
||
// RemovePolicy removes a policy rule from the string buffer. | ||
void RemovePolicy(std::string sec, std::string p_type, std::vector<std::string> rule); | ||
|
||
// RemoveFilteredPolicy removes policy rules that match the filter from the string buffer. | ||
void RemoveFilteredPolicy(std::string sec, std::string p_type, int field_index, std::vector<std::string> field_values); | ||
|
||
// IsFiltered returns true if the loaded policy has been filtered. | ||
bool IsFiltered(); | ||
|
||
// IsValid returns true if the loaded policy is valid. | ||
bool IsValid(); | ||
}; | ||
|
||
}; // namespace casbin | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER | ||
#define CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER | ||
|
||
#include "./adapter.h" | ||
|
||
namespace casbin { | ||
|
||
// Adapter is the string adapter for Casbin. | ||
// It can load policy from string buffer or save policy to string buffer. | ||
class StringAdapter : virtual public Adapter { | ||
public: | ||
std::string line; | ||
|
||
// NewAdapter is the constructor for Adapter. | ||
StringAdapter(std::string line); | ||
|
||
static std::shared_ptr<StringAdapter> NewStringAdapter(std::string line); | ||
|
||
// LoadPolicy loads all policy rules from the string buffer. | ||
void LoadPolicy(const std::shared_ptr<Model>& model); | ||
|
||
// SavePolicy saves all policy rules to the string buffer. | ||
void SavePolicy(const std::shared_ptr<Model>& model); | ||
|
||
// AddPolicy adds a policy rule to the string buffer. | ||
void AddPolicy(std::string sec, std::string p_type, std::vector<std::string> rule); | ||
|
||
// RemovePolicy removes a policy rule from the string buffer. | ||
void RemovePolicy(std::string sec, std::string p_type, std::vector<std::string> rule); | ||
|
||
// RemoveFilteredPolicy removes policy rules that match the filter from the string buffer. | ||
void RemoveFilteredPolicy(std::string sec, std::string p_type, int field_index, std::vector<std::string> field_values); | ||
|
||
// IsFiltered returns true if the loaded policy has been filtered. | ||
bool IsFiltered(); | ||
|
||
// IsValid returns true if the loaded policy is valid. | ||
bool IsValid(); | ||
}; | ||
|
||
}; // namespace casbin | ||
|
||
#endif |