Skip to content

Commit 031868e

Browse files
committed
feat: added synced enforcer
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 74fbd8c commit 031868e

File tree

4 files changed

+298
-1
lines changed

4 files changed

+298
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ MigrationBackup/
353353

354354
.idea/
355355
*.iml
356+
.vscode
356357

357358
# CMake work directory
358-
cmake-build/
359+
cmake-build/

casbin/casbin.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@
233233
<ClInclude Include="enforcer_cached.h" />
234234
<ClInclude Include="enforcer_interface.h" />
235235
<ClInclude Include="exception.h" />
236+
<ClInclude Include="enforcer_synced.h" />
236237
<ClInclude Include="exception\casbin_adapter_exception.h" />
237238
<ClInclude Include="exception\casbin_enforcer_exception.h" />
238239
<ClInclude Include="exception\casbin_rbac_exception.h" />

casbin/casbin.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@
470470
<ClInclude Include="ip_parser\exception\parser_exception.h">
471471
<Filter>Header Files\ip_parser\exception</Filter>
472472
</ClInclude>
473+
<ClInclude Include="enforcer_synced.h">
474+
<Filter>Header Files</Filter>
475+
</ClInclude>
473476
</ItemGroup>
474477
<ItemGroup>
475478
<None Include=".clang-format" />

casbin/enforcer_synced.h

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/*
2+
* Copyright 2020 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CASBIN_H_ENFORCER_SYNC
18+
#define CASBIN_H_ENFORCER_SYNC
19+
20+
#include <mutex>
21+
#include <atomic>
22+
#include <memory>
23+
24+
#include "./enforcer.h"
25+
#include "./persist/watcher.h"
26+
#include "./util/ticker.h"
27+
28+
class SyncedEnforcer : public Enforcer {
29+
mutex policyMutex;
30+
atomic_bool autoLoadRunning;
31+
atomic_int n;
32+
shared_ptr<Watcher> watcher;
33+
unique_ptr<Ticker> ticker;
34+
35+
public:
36+
/**
37+
* Enforcer is the default constructor.
38+
*/
39+
SyncedEnforcer();
40+
41+
/**
42+
* Enforcer initializes an enforcer with a model file and a policy file.
43+
*
44+
* @param model_path the path of the model file.
45+
* @param policy_file the path of the policy file.
46+
*/
47+
SyncedEnforcer(string model_path, string policy_file);
48+
49+
/**
50+
* Enforcer initializes an enforcer with a database adapter.
51+
*
52+
* @param model_path the path of the model file.
53+
* @param adapter the adapter.
54+
*/
55+
SyncedEnforcer(string model_path, shared_ptr<Adapter> adapter);
56+
57+
/**
58+
* Enforcer initializes an enforcer with a model and a database adapter.
59+
*
60+
* @param m the model.
61+
* @param adapter the adapter.
62+
*/
63+
SyncedEnforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter);
64+
65+
/**
66+
* Enforcer initializes an enforcer with a model.
67+
*
68+
* @param m the model.
69+
*/
70+
SyncedEnforcer(shared_ptr<Model> m);
71+
72+
/**
73+
* Enforcer initializes an enforcer with a model file.
74+
*
75+
* @param model_path the path of the model file.
76+
*/
77+
SyncedEnforcer(string model_path);
78+
79+
/**
80+
* Enforcer initializes an enforcer with a model file, a policy file and an enable log flag.
81+
*
82+
* @param model_path the path of the model file.
83+
* @param policy_file the path of the policy file.
84+
* @param enable_log whether to enable Casbin's log.
85+
*/
86+
SyncedEnforcer(string model_path, string policy_file, bool enable_log);
87+
88+
// StartAutoLoadPolicy starts a thread that will go through every specified duration call LoadPolicy
89+
void StartAutoLoadPolicy(std::chrono::duration<int64_t, std::nano> t);
90+
91+
// IsAutoLoadingRunning check if SyncedEnforcer is auto loading policies
92+
inline bool IsAutoLoadingRunning();
93+
94+
// StopAutoLoadPolicy causes the thread to exit
95+
void StopAutoLoadPolicy();
96+
97+
string UpdateWrapper();
98+
99+
// SetWatcher sets the current watcher.
100+
void SetWatcher(shared_ptr<Watcher> w);
101+
102+
// LoadModel reloads the model from the model CONF file.
103+
void LoadModel();
104+
105+
// ClearPolicy clears all policy.
106+
void ClearPolicy();
107+
108+
// LoadPolicy reloads the policy from file/database.
109+
void LoadPolicy();
110+
111+
void LoadPolicyWrapper();
112+
113+
// LoadFilteredPolicy reloads a filtered policy from file/database.
114+
template <typename Filter>
115+
void LoadFilteredPolicy(Filter);
116+
117+
// LoadIncrementalFilteredPolicy reloads a filtered policy from file/database.
118+
void LoadIncrementalFilteredPolicy(Filter);
119+
120+
// SavePolicy saves the current policy (usually after changed with Casbin API) back to file/database.
121+
void SavePolicy();
122+
123+
// BuildRoleLinks manually rebuild the role inheritance relations.
124+
void BuildRoleLinks();
125+
126+
// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
127+
bool Enforce(Scope);
128+
129+
// Enforce with a vector param,decides whether a "subject" can access a
130+
// "object" with the operation "action", input parameters are usually: (sub,
131+
// obj, act).
132+
bool Enforce(vector<string> params);
133+
134+
// Enforce with a map param,decides whether a "subject" can access a "object"
135+
// with the operation "action", input parameters are usually: (sub, obj, act).
136+
bool Enforce(unordered_map<string, string> params);
137+
138+
// BatchEnforce enforce in batches
139+
vector<bool> BatchEnforce(vector<vector<string>> requests);
140+
141+
// BatchEnforceWithMatcher enforce with matcher in batches
142+
vector<bool> BatchEnforceWithMatcher(string matcher, vector<vector<string>> requests);
143+
144+
// GetAllSubjects gets the list of subjects that show up in the current policy.
145+
vector<string> GetAllSubjects();
146+
147+
// GetAllNamedSubjects gets the list of subjects that show up in the current named policy.
148+
vector<string> GetAllNamedSubjects(string ptype);
149+
150+
// GetAllObjects gets the list of objects that show up in the current policy.
151+
vector<string> GetAllObjects();
152+
153+
// GetAllNamedObjects gets the list of objects that show up in the current named policy.
154+
vector<string> GetAllNamedObjects(string ptype);
155+
156+
// GetAllNamedActions gets the list of actions that show up in the current named policy.
157+
vector<string> GetAllNamedActions(string ptype);
158+
159+
// GetAllRoles gets the list of roles that show up in the current policy.
160+
vector<string> GetAllRoles();
161+
162+
// GetAllNamedRoles gets the list of roles that show up in the current named policy.
163+
vector<string> GetAllNamedRoles(string ptype);
164+
165+
// GetPolicy gets all the authorization rules in the policy.
166+
vector<vector<string>> GetPolicy();
167+
168+
// GetNamedPolicy gets all the authorization rules in the named policy.
169+
vector<vector<string>> GetNamedPolicy(string ptype);
170+
171+
// GetFilteredNamedPolicy gets all the authorization rules in the named policy, field filters can be specified.
172+
vector<vector<string>> GetFilteredNamedPolicy(string ptype, int fieldIndex, vector<string> fieldValues);
173+
174+
// GetGroupingPolicy gets all the role inheritance rules in the policy.
175+
vector<vector<string>> GetGroupingPolicy();
176+
177+
// GetFilteredGroupingPolicy gets all the role inheritance rules in the policy, field filters can be specified.
178+
vector<vector<string>> GetFilteredGroupingPolicy(int fieldIndex, vector<string> fieldValues);
179+
180+
// GetNamedGroupingPolicy gets all the role inheritance rules in the policy.
181+
vector<vector<string>> GetNamedGroupingPolicy(string ptype);
182+
183+
// GetFilteredNamedGroupingPolicy gets all the role inheritance rules in the policy, field filters can be specified.
184+
vector<vector<string>> GetFilteredNamedGroupingPolicy(string ptype, int fieldIndex, vector<string> fieldValues);
185+
186+
// HasPolicy determines whether an authorization rule exists.
187+
bool HasPolicy(vector<string> params);
188+
189+
// HasNamedPolicy determines whether a named authorization rule exists.
190+
bool HasNamedPolicy(string ptype, vector<string> params);
191+
192+
// AddPolicy adds an authorization rule to the current policy.
193+
// If the rule already exists, the function returns false and the rule will not be added.
194+
// Otherwise the function returns true by adding the new rule.
195+
bool AddPolicy(vector<string> params);
196+
197+
// AddPolicies adds authorization rules to the current policy.
198+
// If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
199+
// Otherwise the function returns true for the corresponding rule by adding the new rule.
200+
bool AddPolicies(vector<vector<string>> rules);
201+
202+
// AddNamedPolicy adds an authorization rule to the current named policy.
203+
// If the rule already exists, the function returns false and the rule will not be added.
204+
// Otherwise the function returns true by adding the new rule.
205+
bool AddNamedPolicy(string ptype, vector<string> params);
206+
207+
// AddNamedPolicies adds authorization rules to the current named policy.
208+
// If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
209+
// Otherwise the function returns true for the corresponding by adding the new rule.
210+
bool AddNamedPolicies(string ptype, vector<vector<string>> rules);
211+
212+
// RemovePolicy removes an authorization rule from the current policy.
213+
bool RemovePolicy(vector<string> params);
214+
215+
// UpdatePolicy updates an authorization rule from the current policy.
216+
bool UpdatePolicy(vector<string> oldPolicy, vector<string> newPolicy);
217+
218+
bool UpdateNamedPolicy(string ptype, vector<string> p1, vector<string> p2);
219+
220+
// UpdatePolicies updates authorization rules from the current policies.
221+
bool UpdatePolicies(vector<vector<string>> oldPolices, vector<vector<string>> newPolicies);
222+
223+
bool UpdateNamedPolicies(string ptype, vector<vector<string>> p1, vector<vector<string>> p2);
224+
225+
// RemovePolicies removes authorization rules from the current policy.
226+
bool RemovePolicies(vector<vector<string>> rules);
227+
228+
// RemoveFilteredPolicy removes an authorization rule from the current policy, field filters can be specified.
229+
bool RemoveFilteredPolicy(int fieldIndex, vector<string> fieldValues);
230+
231+
// RemoveNamedPolicy removes an authorization rule from the current named policy.
232+
bool RemoveNamedPolicy(string ptype, vector<string> params);
233+
234+
// RemoveNamedPolicies removes authorization rules from the current named policy.
235+
bool RemoveNamedPolicies(string ptype, vector<vector<string>> rules);
236+
237+
// RemoveFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
238+
bool RemoveFilteredNamedPolicy(string ptype, int fieldIndex, vector<string> fieldValues);
239+
240+
// HasGroupingPolicy determines whether a role inheritance rule exists.
241+
bool HasGroupingPolicy(vector<string> params);
242+
243+
// HasNamedGroupingPolicy determines whether a named role inheritance rule exists.
244+
bool HasNamedGroupingPolicy(string ptype, vector<string> params);
245+
246+
// AddGroupingPolicy adds a role inheritance rule to the current policy.
247+
// If the rule already exists, the function returns false and the rule will not be added.
248+
// Otherwise the function returns true by adding the new rule.
249+
bool AddGroupingPolicy(vector<string> params);
250+
251+
// AddGroupingPolicies adds role inheritance rulea to the current policy.
252+
// If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
253+
// Otherwise the function returns true for the corresponding policy rule by adding the new rule.
254+
bool AddGroupingPolicies(vector<vector<string>> rules);
255+
256+
// AddNamedGroupingPolicy adds a named role inheritance rule to the current policy.
257+
// If the rule already exists, the function returns false and the rule will not be added.
258+
// Otherwise the function returns true by adding the new rule.
259+
bool AddNamedGroupingPolicy(string ptype, vector<string> params);
260+
261+
// AddNamedGroupingPolicies adds named role inheritance rules to the current policy.
262+
// If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
263+
// Otherwise the function returns true for the corresponding policy rule by adding the new rule.
264+
bool AddNamedGroupingPolicies(string ptype, vector<vector<string>> rules);
265+
266+
// RemoveGroupingPolicy removes a role inheritance rule from the current policy.
267+
bool RemoveGroupingPolicy(vector<string> params);
268+
269+
// RemoveGroupingPolicies removes role inheritance rules from the current policy.
270+
bool RemoveGroupingPolicies(vector<vector<string>> rules);
271+
272+
// RemoveFilteredGroupingPolicy removes a role inheritance rule from the current policy, field filters can be specified.
273+
bool RemoveFilteredGroupingPolicy(int fieldIndex, vector<string> fieldValues);
274+
275+
// RemoveNamedGroupingPolicy removes a role inheritance rule from the current named policy.
276+
bool RemoveNamedGroupingPolicy(string ptype, vector<string> params);
277+
278+
// RemoveNamedGroupingPolicies removes role inheritance rules from the current named policy.
279+
bool RemoveNamedGroupingPolicies(string ptype, vector<vector<string>> rules);
280+
281+
bool UpdateGroupingPolicy(vector<string> oldRule, vector<string> newRule);
282+
283+
bool UpdateNamedGroupingPolicy(string ptype, vector<string> oldRule, vector<string> newRule);
284+
285+
// RemoveFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
286+
bool RemoveFilteredNamedGroupingPolicy(string ptype, int fieldIndex, vector<string> fieldValues);
287+
288+
// AddFunction adds a customized function.
289+
void AddFunction(string name, Function function, Index nargs);
290+
};
291+
292+
#endif // CASBIN_CPP_ENFORCER_SYNC

0 commit comments

Comments
 (0)