Skip to content

Commit cd76b7c

Browse files
authored
feat: fix bug that get_all_subjects() and similar APIs get wrong result (#399)
1 parent 0894af7 commit cd76b7c

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

casbin/async_management_enforcer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
from casbin.async_internal_enforcer import AsyncInternalEnforcer
1515
from casbin.model.policy_op import PolicyOp
16+
from casbin.constant.constants import ACTION_INDEX, SUBJECT_INDEX, OBJECT_INDEX
1617

1718

1819
class AsyncManagementEnforcer(AsyncInternalEnforcer):
@@ -22,27 +23,30 @@ class AsyncManagementEnforcer(AsyncInternalEnforcer):
2223

2324
def get_all_subjects(self):
2425
"""gets the list of subjects that show up in the current policy."""
25-
return self.get_all_named_subjects("p")
26+
return self.model.get_values_for_field_in_policy_all_types_by_name("p", SUBJECT_INDEX)
2627

2728
def get_all_named_subjects(self, ptype):
2829
"""gets the list of subjects that show up in the current named policy."""
29-
return self.model.get_values_for_field_in_policy("p", ptype, 0)
30+
field_index = self.model.get_field_index(ptype, SUBJECT_INDEX)
31+
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
3032

3133
def get_all_objects(self):
3234
"""gets the list of objects that show up in the current policy."""
33-
return self.get_all_named_objects("p")
35+
return self.model.get_values_for_field_in_policy_all_types_by_name("p", OBJECT_INDEX)
3436

3537
def get_all_named_objects(self, ptype):
3638
"""gets the list of objects that show up in the current named policy."""
37-
return self.model.get_values_for_field_in_policy("p", ptype, 1)
39+
field_index = self.model.get_field_index(ptype, OBJECT_INDEX)
40+
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
3841

3942
def get_all_actions(self):
4043
"""gets the list of actions that show up in the current policy."""
41-
return self.get_all_named_actions("p")
44+
return self.model.get_values_for_field_in_policy_all_types_by_name("p", ACTION_INDEX)
4245

4346
def get_all_named_actions(self, ptype):
4447
"""gets the list of actions that show up in the current named policy."""
45-
return self.model.get_values_for_field_in_policy("p", ptype, 2)
48+
field_index = self.model.get_field_index(ptype, ACTION_INDEX)
49+
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
4650

4751
def get_all_roles(self):
4852
"""gets the list of roles that show up in the current named policy."""

casbin/management_enforcer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ManagementEnforcer(InternalEnforcer):
2424

2525
def get_all_subjects(self):
2626
"""gets the list of subjects that show up in the current policy."""
27-
return self.get_all_named_subjects("p")
27+
return self.model.get_values_for_field_in_policy_all_types_by_name("p", SUBJECT_INDEX)
2828

2929
def get_all_named_subjects(self, ptype):
3030
"""gets the list of subjects that show up in the current named policy."""
@@ -33,7 +33,7 @@ def get_all_named_subjects(self, ptype):
3333

3434
def get_all_objects(self):
3535
"""gets the list of objects that show up in the current policy."""
36-
return self.get_all_named_objects("p")
36+
return self.model.get_values_for_field_in_policy_all_types_by_name("p", OBJECT_INDEX)
3737

3838
def get_all_named_objects(self, ptype):
3939
"""gets the list of objects that show up in the current named policy."""
@@ -42,7 +42,7 @@ def get_all_named_objects(self, ptype):
4242

4343
def get_all_actions(self):
4444
"""gets the list of actions that show up in the current policy."""
45-
return self.get_all_named_actions("p")
45+
return self.model.get_values_for_field_in_policy_all_types_by_name("p", ACTION_INDEX)
4646

4747
def get_all_named_actions(self, ptype):
4848
"""gets the list of actions that show up in the current named policy."""

casbin/model/policy.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import logging
16+
from casbin.util import util
1617

1718
DEFAULT_SEP = ","
1819

@@ -318,3 +319,18 @@ def get_values_for_field_in_policy(self, sec, ptype, field_index):
318319
values.append(value)
319320

320321
return values
322+
323+
def get_values_for_field_in_policy_all_types_by_name(self, sec, field):
324+
"""gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed."""
325+
values = []
326+
if sec not in self.keys():
327+
return values
328+
329+
for ptype in self[sec]:
330+
index = self.get_field_index(ptype, field)
331+
value = self.get_values_for_field_in_policy(sec, ptype, index)
332+
values.extend(value)
333+
334+
values = util.array_remove_duplicates(values)
335+
336+
return values

0 commit comments

Comments
 (0)