Skip to content

Commit 3e1e242

Browse files
authored
fix: add StringAdapter for Enforcer to pass policy as string (#204)
1 parent 5d9e56c commit 3e1e242

File tree

9 files changed

+196
-3
lines changed

9 files changed

+196
-3
lines changed

casbin/enforcer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Enforcer::Enforcer(const std::shared_ptr<Model>& m, std::shared_ptr<Adapter> ada
228228

229229
this->Initialize();
230230

231-
if (m_adapter && m_adapter->file_path != "")
231+
if (m_adapter && m_adapter->IsValid())
232232
this->LoadPolicy();
233233
}
234234

casbin/persist/adapter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void LoadPolicyLine(std::string line, const std::shared_ptr<Model>& model);
3232
*/
3333
class Adapter {
3434
public:
35-
std::string file_path;
3635
bool filtered;
3736

3837
/**
@@ -82,6 +81,8 @@ class Adapter {
8281
virtual void RemoveFilteredPolicy(std::string sec, std::string ptype, int field_index, std::vector<std::string> field_values) = 0;
8382

8483
virtual bool IsFiltered() = 0;
84+
85+
virtual bool IsValid() = 0;
8586
};
8687

8788
}; // namespace casbin

casbin/persist/file_adapter/file_adapter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ bool FileAdapter ::IsFiltered() {
113113
return this->filtered;
114114
}
115115

116+
// IsValid returns true if the loaded policy is valid.
117+
bool FileAdapter ::IsValid() {
118+
return this->file_path != "";
119+
}
120+
116121
} // namespace casbin
117122

118123
#endif // FILE_ADAPTER_CPP

casbin/persist/file_adapter/file_adapter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace casbin {
99
// It can load policy from file or save policy to file.
1010
class FileAdapter : virtual public Adapter {
1111
public:
12+
std::string file_path;
13+
1214
// NewAdapter is the constructor for Adapter.
1315
FileAdapter(std::string file_path);
1416

@@ -35,6 +37,9 @@ class FileAdapter : virtual public Adapter {
3537

3638
// IsFiltered returns true if the loaded policy has been filtered.
3739
bool IsFiltered();
40+
41+
// IsValid returns true if the loaded policy is valid.
42+
bool IsValid();
3843
};
3944

4045
}; // namespace casbin

casbin/persist/string_adapter.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

casbin/persist/string_adapter.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER
2+
#define CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER
3+
4+
#include "./adapter.h"
5+
6+
namespace casbin {
7+
8+
// Adapter is the string adapter for Casbin.
9+
// It can load policy from string buffer or save policy to string buffer.
10+
class StringAdapter : virtual public Adapter {
11+
public:
12+
std::string line;
13+
14+
// NewAdapter is the constructor for Adapter.
15+
StringAdapter(std::string line);
16+
17+
static std::shared_ptr<StringAdapter> NewStringAdapter(std::string line);
18+
19+
// LoadPolicy loads all policy rules from the string buffer.
20+
void LoadPolicy(const std::shared_ptr<Model>& model);
21+
22+
// SavePolicy saves all policy rules to the string buffer.
23+
void SavePolicy(const std::shared_ptr<Model>& model);
24+
25+
// AddPolicy adds a policy rule to the string buffer.
26+
void AddPolicy(std::string sec, std::string p_type, std::vector<std::string> rule);
27+
28+
// RemovePolicy removes a policy rule from the string buffer.
29+
void RemovePolicy(std::string sec, std::string p_type, std::vector<std::string> rule);
30+
31+
// RemoveFilteredPolicy removes policy rules that match the filter from the string buffer.
32+
void RemoveFilteredPolicy(std::string sec, std::string p_type, int field_index, std::vector<std::string> field_values);
33+
34+
// IsFiltered returns true if the loaded policy has been filtered.
35+
bool IsFiltered();
36+
37+
// IsValid returns true if the loaded policy is valid.
38+
bool IsValid();
39+
};
40+
41+
}; // namespace casbin
42+
43+
#endif

include/casbin/persist/adapter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void LoadPolicyLine(std::string line, const std::shared_ptr<Model>& model);
3232
*/
3333
class Adapter {
3434
public:
35-
std::string file_path;
3635
bool filtered;
3736

3837
/**
@@ -82,6 +81,8 @@ class Adapter {
8281
virtual void RemoveFilteredPolicy(std::string sec, std::string ptype, int field_index, std::vector<std::string> field_values) = 0;
8382

8483
virtual bool IsFiltered() = 0;
84+
85+
virtual bool IsValid() = 0;
8586
};
8687

8788
}; // namespace casbin

include/casbin/persist/file_adapter/file_adapter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace casbin {
99
// It can load policy from file or save policy to file.
1010
class FileAdapter : virtual public Adapter {
1111
public:
12+
std::string file_path;
13+
1214
// NewAdapter is the constructor for Adapter.
1315
FileAdapter(std::string file_path);
1416

@@ -35,6 +37,9 @@ class FileAdapter : virtual public Adapter {
3537

3638
// IsFiltered returns true if the loaded policy has been filtered.
3739
bool IsFiltered();
40+
41+
// IsValid returns true if the loaded policy is valid.
42+
bool IsValid();
3843
};
3944

4045
}; // namespace casbin
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER
2+
#define CASBIN_CPP_PERSIST_STRING_ADAPTER_STRING_ADAPTER
3+
4+
#include "./adapter.h"
5+
6+
namespace casbin {
7+
8+
// Adapter is the string adapter for Casbin.
9+
// It can load policy from string buffer or save policy to string buffer.
10+
class StringAdapter : virtual public Adapter {
11+
public:
12+
std::string line;
13+
14+
// NewAdapter is the constructor for Adapter.
15+
StringAdapter(std::string line);
16+
17+
static std::shared_ptr<StringAdapter> NewStringAdapter(std::string line);
18+
19+
// LoadPolicy loads all policy rules from the string buffer.
20+
void LoadPolicy(const std::shared_ptr<Model>& model);
21+
22+
// SavePolicy saves all policy rules to the string buffer.
23+
void SavePolicy(const std::shared_ptr<Model>& model);
24+
25+
// AddPolicy adds a policy rule to the string buffer.
26+
void AddPolicy(std::string sec, std::string p_type, std::vector<std::string> rule);
27+
28+
// RemovePolicy removes a policy rule from the string buffer.
29+
void RemovePolicy(std::string sec, std::string p_type, std::vector<std::string> rule);
30+
31+
// RemoveFilteredPolicy removes policy rules that match the filter from the string buffer.
32+
void RemoveFilteredPolicy(std::string sec, std::string p_type, int field_index, std::vector<std::string> field_values);
33+
34+
// IsFiltered returns true if the loaded policy has been filtered.
35+
bool IsFiltered();
36+
37+
// IsValid returns true if the loaded policy is valid.
38+
bool IsValid();
39+
};
40+
41+
}; // namespace casbin
42+
43+
#endif

0 commit comments

Comments
 (0)