Skip to content

Commit 27ac2bd

Browse files
committed
feat: Incorporated in internal code
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 02a54a2 commit 27ac2bd

File tree

11 files changed

+173
-86
lines changed

11 files changed

+173
-86
lines changed

casbin/data_types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
#include "abac_data.h"
2222

2323
namespace casbin {
24+
2425
typedef std::variant<std::string, std::shared_ptr<ABACData>> Data;
2526
typedef std::vector<Data> DataVector;
2627
typedef std::initializer_list<Data> DataList;
2728
typedef std::unordered_map<std::string, Data> DataMap;
28-
}
29+
30+
} // namespace casbin

casbin/enforcer.cpp

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,12 @@ bool Enforcer :: Enforce(Scope scope) {
435435
}
436436

437437
// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
438-
bool Enforcer::Enforce(const std::vector<std::string>& params) {
438+
bool Enforcer::Enforce(const DataList& params) {
439439
return this->EnforceWithMatcher("", params);
440440
}
441441

442442
// Enforce with a map param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
443-
bool Enforcer::Enforce(const std::unordered_map<std::string, std::string>& params) {
443+
bool Enforcer::Enforce(const DataMap& params) {
444444
return this->EnforceWithMatcher("", params);
445445
}
446446

@@ -450,22 +450,55 @@ bool Enforcer :: EnforceWithMatcher(const std::string& matcher, Scope scope) {
450450
}
451451

452452
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
453-
bool Enforcer::EnforceWithMatcher(const std::string& matcher, const std::vector<std::string>& params) {
454-
std::vector<std::string> r_tokens = m_model->m["r"].assertion_map["r"]->tokens;
453+
bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataList& params) {
454+
const std::vector<std::string>& r_tokens = m_model->m["r"].assertion_map["r"]->tokens;
455455

456-
int r_cnt = int(r_tokens.size());
457-
int cnt = int(params.size());
456+
size_t r_cnt = r_tokens.size();
457+
size_t cnt = params.size();
458458

459459
if (cnt != r_cnt)
460460
return false;
461461

462462
Scope scope = InitializeScope();
463463
PushObject(scope, "r");
464464

465-
for (int i = 0; i < cnt; i++) {
466-
PushStringPropToObject(scope, "r", params[i], r_tokens[i].substr(2, r_tokens[i].size() - 2));
465+
size_t i = 0;
466+
467+
for(const auto& param : params) {
468+
if(const auto string_param = std::get_if<std::string>(&param)) {
469+
PushStringPropToObject(scope, "r", *string_param, r_tokens[i].substr(2, r_tokens[i].size() - 2));
470+
}
471+
else if(const auto abac_param = std::get_if<std::shared_ptr<ABACData>>(&param)) {
472+
auto data_ptr = *abac_param;
473+
std::string token_name = r_tokens[i].substr(2, r_tokens[i].size() - 2);
474+
475+
PushObjectPropToObject(scope, "r", token_name);
476+
477+
for(auto [attrib_name, attrib_value] : data_ptr->GetAttributes()) {
478+
479+
if(const auto string_value = std::get_if<std::string>(&attrib_value))
480+
PushStringPropToObject(scope, token_name, *string_value, attrib_name);
481+
482+
else if(const auto int_value = std::get_if<int32_t>(&attrib_value))
483+
PushIntPropToObject(scope, token_name, *int_value, attrib_name);
484+
485+
else if(const auto float_value = std::get_if<float>(&attrib_value))
486+
PushFloatPropToObject(scope, token_name, *float_value, attrib_name);
487+
488+
else if(const auto double_value = std::get_if<double>(&attrib_value))
489+
PushDoublePropToObject(scope, token_name, *double_value, attrib_name);
490+
491+
else
492+
throw CasbinEnforcerException("Not a valid type");
493+
}
494+
}
495+
++i;
467496
}
468497

498+
// for (size_t i = 0; i < cnt; i++) {
499+
// PushStringPropToObject(scope, "r", params[i], r_tokens[i].substr(2, r_tokens[i].size() - 2));
500+
// }
501+
469502
bool result = m_enforce(matcher, scope);
470503
DeinitializeScope(scope);
471504
return result;
@@ -474,12 +507,36 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const std::vector<
474507
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object"
475508
// with the operation "action", input parameters are usually: (matcher, sub, obj, act),
476509
// use model matcher by default when matcher is "".
477-
bool Enforcer::EnforceWithMatcher(const std::string& matcher, const std::unordered_map<std::string, std::string>& params) {
510+
bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataMap& params) {
478511
Scope scope = InitializeScope();
479512
PushObject(scope, "r");
480513

481-
for (auto r : params) {
482-
PushStringPropToObject(scope, "r", r.second, r.first);
514+
for (auto [param_name, param_data] : params) {
515+
if(const auto string_param = std::get_if<std::string>(&param_data))
516+
PushStringPropToObject(scope, "r", *string_param, param_name);
517+
else if(const auto abac_param = std::get_if<std::shared_ptr<ABACData>>(&param_data)) {
518+
auto data_ptr = *abac_param;
519+
520+
PushObjectPropToObject(scope, "r", param_name);
521+
522+
for(auto [attrib_name, attrib_value] : data_ptr->GetAttributes()) {
523+
524+
if(const auto string_value = std::get_if<std::string>(&attrib_value))
525+
PushStringPropToObject(scope, param_name, *string_value, attrib_name);
526+
527+
else if(const auto int_value = std::get_if<int32_t>(&attrib_value))
528+
PushIntPropToObject(scope, param_name, *int_value, attrib_name);
529+
530+
else if(const auto float_value = std::get_if<float>(&attrib_value))
531+
PushFloatPropToObject(scope, param_name, *float_value, attrib_name);
532+
533+
else if(const auto double_value = std::get_if<double>(&attrib_value))
534+
PushDoublePropToObject(scope, param_name, *double_value, attrib_name);
535+
536+
else
537+
throw CasbinEnforcerException("Not a valid type");
538+
}
539+
}
483540
}
484541

485542
bool result = m_enforce(matcher, scope);
@@ -488,21 +545,21 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const std::unorder
488545
}
489546

490547
// BatchEnforce enforce in batches
491-
std::vector<bool> Enforcer :: BatchEnforce(const std::vector<std::vector<std::string>>& requests) {
548+
std::vector<bool> Enforcer :: BatchEnforce(const std::initializer_list<DataList>& requests) {
492549
// Initializing an array for storing results with false
493550
std::vector<bool> results;
494551
results.reserve(requests.size());
495-
for (auto request : requests) {
552+
for (const auto& request : requests) {
496553
results.push_back(this->Enforce(request));
497554
}
498555
return results;
499556
}
500557

501558
// BatchEnforceWithMatcher enforce with matcher in batches
502-
std::vector<bool> Enforcer :: BatchEnforceWithMatcher(const std::string& matcher, const std::vector<std::vector<std::string>>& requests) {
559+
std::vector<bool> Enforcer :: BatchEnforceWithMatcher(const std::string& matcher, const std::initializer_list<DataList>& requests) {
503560
std::vector<bool> results;
504561
results.reserve(requests.size());
505-
for (auto request : requests) {
562+
for (const auto& request : requests) {
506563
results.push_back(this->EnforceWithMatcher(matcher, request));
507564
}
508565
return results;

casbin/enforcer.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,19 @@ class Enforcer : public IEnforcer {
156156
// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
157157
bool Enforce(Scope scope);
158158
// Enforce with a vector param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
159-
bool Enforce(const std::vector<std::string>& params);
159+
bool Enforce(const DataList& params);
160160
// Enforce with a map param,decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
161-
bool Enforce(const std::unordered_map<std::string,std::string>& params);
161+
bool Enforce(const DataMap& params);
162162
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
163163
bool EnforceWithMatcher(const std::string& matcher, Scope scope);
164164
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
165-
bool EnforceWithMatcher(const std::string& matcher, const std::vector<std::string>& params);
165+
bool EnforceWithMatcher(const std::string& matcher, const DataList& params);
166166
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
167-
bool EnforceWithMatcher(const std::string& matcher, const std::unordered_map<std::string, std::string>& params);
167+
bool EnforceWithMatcher(const std::string& matcher, const DataMap& params);
168168
// BatchEnforce enforce in batches
169-
std::vector<bool> BatchEnforce(const std::vector<std::vector<std::string>>& requests);
169+
std::vector<bool> BatchEnforce(const std::initializer_list<DataList>& requests);
170170
// BatchEnforceWithMatcher enforce with matcher in batches
171-
std::vector<bool> BatchEnforceWithMatcher(const std::string& matcher, const std::vector<std::vector<std::string>>& requests);
171+
std::vector<bool> BatchEnforceWithMatcher(const std::string& matcher, const std::initializer_list<DataList>& requests);
172172

173173
/*Management API member functions.*/
174174
std::vector<std::string> GetAllSubjects();

casbin/enforcer_cached.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ bool CachedEnforcer ::Enforce(Scope scope) {
150150

151151
// Enforce with a vector param,decides whether a "subject" can access a "object"
152152
// with the operation "action", input parameters are usually: (sub, obj, act).
153-
bool CachedEnforcer::Enforce(const std::vector<std::string>& params) {
153+
bool CachedEnforcer::Enforce(const DataList& params) {
154154
return EnforceWithMatcher("", params);
155155
}
156156

157157
// Enforce with a map param,decides whether a "subject" can access a "object"
158158
// with the operation "action", input parameters are usually: (sub, obj, act).
159-
bool CachedEnforcer::Enforce(const std::unordered_map<std::string, std::string>& params) {
159+
bool CachedEnforcer::Enforce(const DataMap& params) {
160160
return EnforceWithMatcher("", params);
161161
}
162162

@@ -170,14 +170,28 @@ bool CachedEnforcer ::EnforceWithMatcher(const std::string& matcher, Scope scope
170170
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can
171171
// access a "object" with the operation "action", input parameters are usually:
172172
// (matcher, sub, obj, act), use model matcher by default when matcher is "".
173-
bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const std::vector<std::string>& params) {
173+
bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const DataList& params) {
174174
if (!enableCache) {
175175
return Enforcer::EnforceWithMatcher(matcher, params);
176176
}
177177

178178
std::string key;
179-
for (auto r : params) {
180-
key += r;
179+
for (const auto& r : params) {
180+
if(const auto string_param = std::get_if<std::string>(&r))
181+
key += *string_param;
182+
else if(const auto abac_param = std::get_if<std::shared_ptr<ABACData>>(&r)) {
183+
auto data_ptr = *abac_param;
184+
for(auto [_, attrib_value] : data_ptr->GetAttributes()) {
185+
if(auto string_value = std::get_if<std::string>(&attrib_value))
186+
key += *string_value + "$";
187+
else if(auto int_value = std::get_if<int32_t>(&attrib_value))
188+
key += std::to_string(*int_value) + "$";
189+
else if(auto double_value = std::get_if<double>(&attrib_value))
190+
key += std::to_string(*double_value) + "$";
191+
else if(auto float_value = std::get_if<float>(&attrib_value))
192+
key += std::to_string(*float_value) + "$";
193+
}
194+
}
181195
key += "$$";
182196
}
183197
key += matcher;
@@ -197,14 +211,28 @@ bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const std::v
197211
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can
198212
// access a "object" with the operation "action", input parameters are usually:
199213
// (matcher, sub, obj, act), use model matcher by default when matcher is "".
200-
bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const std::unordered_map<std::string, std::string>& params) {
214+
bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const DataMap& params) {
201215
if (!enableCache) {
202216
return Enforcer::EnforceWithMatcher(matcher, params);
203217
}
204218

205219
std::string key;
206-
for (auto r : params) {
207-
key += r.second;
220+
for (auto [param_name, param_value] : params) {
221+
if(const auto string_value = std::get_if<std::string>(&param_value))
222+
key += *string_value;
223+
else if(const auto abac_param = std::get_if<std::shared_ptr<ABACData>>(&param_value)) {
224+
auto data_ptr = *abac_param;
225+
for(auto [_, attrib_value] : data_ptr->GetAttributes()) {
226+
if(auto string_value = std::get_if<std::string>(&attrib_value))
227+
key += *string_value + "$";
228+
else if(auto int_value = std::get_if<int32_t>(&attrib_value))
229+
key += std::to_string(*int_value) + "$";
230+
else if(auto double_value = std::get_if<double>(&attrib_value))
231+
key += std::to_string(*double_value) + "$";
232+
else if(auto float_value = std::get_if<float>(&attrib_value))
233+
key += std::to_string(*float_value) + "$";
234+
}
235+
}
208236
key += "$$";
209237
}
210238
key += matcher;

casbin/enforcer_cached.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ class CachedEnforcer : public Enforcer {
8888
// Enforce with a vector param,decides whether a "subject" can access a
8989
// "object" with the operation "action", input parameters are usually: (sub,
9090
// obj, act).
91-
bool Enforce(const std::vector<std::string>& params);
91+
bool Enforce(const DataList& params);
9292
// Enforce with a map param,decides whether a "subject" can access a "object"
9393
// with the operation "action", input parameters are usually: (sub, obj, act).
94-
bool Enforce(const std::unordered_map<std::string, std::string>& params);
94+
bool Enforce(const DataMap& params);
9595
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can
9696
// access a "object" with the operation "action", input parameters are
9797
// usually: (matcher, sub, obj, act), use model matcher by default when
@@ -101,12 +101,12 @@ class CachedEnforcer : public Enforcer {
101101
// access a "object" with the operation "action", input parameters are
102102
// usually: (matcher, sub, obj, act), use model matcher by default when
103103
// matcher is "".
104-
bool EnforceWithMatcher(const std::string& matcher, const std::vector<std::string>& params);
104+
bool EnforceWithMatcher(const std::string& matcher, const DataList& params);
105105
// EnforceWithMatcher use a custom matcher to decides whether a "subject" can
106106
// access a "object" with the operation "action", input parameters are
107107
// usually: (matcher, sub, obj, act), use model matcher by default when
108108
// matcher is "".
109-
bool EnforceWithMatcher(const std::string& matcher, const std::unordered_map<std::string, std::string>& params);
109+
bool EnforceWithMatcher(const std::string& matcher, const DataMap& params);
110110
};
111111

112112
} // namespace casbin

casbin/enforcer_interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class IEnforcer {
6060
virtual bool m_enforce(const std::string& matcher, Scope scope) = 0;
6161
virtual bool Enforce(Scope scope) = 0;
6262
virtual bool EnforceWithMatcher(const std::string& matcher, Scope scope) = 0;
63-
virtual std::vector<bool> BatchEnforce(const std::vector<std::vector<std::string>>& requests) = 0;
64-
virtual std::vector<bool> BatchEnforceWithMatcher(const std::string& matcher, const std::vector<std::vector<std::string>>& requests) = 0;
63+
virtual std::vector<bool> BatchEnforce(const std::initializer_list<DataList>& requests) = 0;
64+
virtual std::vector<bool> BatchEnforceWithMatcher(const std::string& matcher, const std::initializer_list<DataList>& requests) = 0;
6565

6666
/* RBAC API */
6767
virtual std::vector<std::string> GetRolesForUser(const std::string& name, const std::vector<std::string>& domain = {}) = 0;

casbin/enforcer_synced.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,26 @@ bool SyncedEnforcer ::Enforce(Scope s) {
189189
// Enforce with a vector param,decides whether a "subject" can access a
190190
// "object" with the operation "action", input parameters are usually: (sub,
191191
// obj, act).
192-
bool SyncedEnforcer ::Enforce(const std::vector<std::string>& params) {
192+
bool SyncedEnforcer ::Enforce(const DataList& params) {
193193
std::lock_guard<std::mutex> lock(policyMutex);
194194
return Enforcer::Enforce(params);
195195
}
196196

197197
// Enforce with a map param,decides whether a "subject" can access a "object"
198198
// with the operation "action", input parameters are usually: (sub, obj, act).
199-
bool SyncedEnforcer ::Enforce(const std::unordered_map<std::string, std::string>& params) {
199+
bool SyncedEnforcer ::Enforce(const DataMap& params) {
200200
std::lock_guard<std::mutex> lock(policyMutex);
201201
return Enforcer::Enforce(params);
202202
}
203203

204204
// BatchEnforce enforce in batches
205-
std::vector<bool> SyncedEnforcer ::BatchEnforce(const std::vector<std::vector<std::string>>& requests) {
205+
std::vector<bool> SyncedEnforcer ::BatchEnforce(const std::initializer_list<DataList>& requests) {
206206
std::lock_guard<std::mutex> lock(policyMutex);
207207
return Enforcer::BatchEnforce(requests);
208208
}
209209

210210
// BatchEnforceWithMatcher enforce with matcher in batches
211-
std::vector<bool> SyncedEnforcer ::BatchEnforceWithMatcher(const std::string& matcher, const std::vector<std::vector<std::string>>& requests) {
211+
std::vector<bool> SyncedEnforcer ::BatchEnforceWithMatcher(const std::string& matcher, const std::initializer_list<DataList>& requests) {
212212
std::lock_guard<std::mutex> lock(policyMutex);
213213
return Enforcer::BatchEnforceWithMatcher(matcher, requests);
214214
}

casbin/enforcer_synced.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ class SyncedEnforcer : public Enforcer {
131131
// Enforce with a vector param,decides whether a "subject" can access a
132132
// "object" with the operation "action", input parameters are usually: (sub,
133133
// obj, act).
134-
bool Enforce(const std::vector<std::string>& params);
134+
bool Enforce(const DataList& params);
135135

136136
// Enforce with a map param,decides whether a "subject" can access a "object"
137137
// with the operation "action", input parameters are usually: (sub, obj, act).
138-
bool Enforce(const std::unordered_map<std::string, std::string>& params);
138+
bool Enforce(const DataMap& params);
139139

140140
// BatchEnforce enforce in batches
141-
std::vector<bool> BatchEnforce(const std::vector<std::vector<std::string>>& requests);
141+
std::vector<bool> BatchEnforce(const std::initializer_list<DataList>& requests);
142142

143143
// BatchEnforceWithMatcher enforce with matcher in batches
144-
std::vector<bool> BatchEnforceWithMatcher(const std::string& matcher, const std::vector<std::vector<std::string>>& requests);
144+
std::vector<bool> BatchEnforceWithMatcher(const std::string& matcher, const std::initializer_list<DataList>& requests);
145145

146146
// GetAllSubjects gets the list of subjects that show up in the current policy.
147147
std::vector<std::string> GetAllSubjects();

0 commit comments

Comments
 (0)