Skip to content

Commit 54bc5a0

Browse files
committed
fix: field_index is incorrect in RBAC with domains mode (#345)
1 parent 846cf24 commit 54bc5a0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

casbin/management_enforcer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ def get_all_subjects(self):
2727

2828
def get_all_named_subjects(self, ptype):
2929
"""gets the list of subjects that show up in the current named policy."""
30-
return self.model.get_values_for_field_in_policy("p", ptype, 0)
30+
field_index = self.model.get_field_index(ptype, "sub")
31+
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
3132

3233
def get_all_objects(self):
3334
"""gets the list of objects that show up in the current policy."""
3435
return self.get_all_named_objects("p")
3536

3637
def get_all_named_objects(self, ptype):
3738
"""gets the list of objects that show up in the current named policy."""
38-
return self.model.get_values_for_field_in_policy("p", ptype, 1)
39+
field_index = self.model.get_field_index(ptype, "obj")
40+
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
3941

4042
def get_all_actions(self):
4143
"""gets the list of actions that show up in the current policy."""
4244
return self.get_all_named_actions("p")
4345

4446
def get_all_named_actions(self, ptype):
4547
"""gets the list of actions that show up in the current named policy."""
46-
return self.model.get_values_for_field_in_policy("p", ptype, 2)
48+
field_index = self.model.get_field_index(ptype, "act")
49+
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
4750

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

tests/test_management_api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ def test_get_list(self):
3838
self.assertEqual(e.get_all_actions(), ["read", "write"])
3939
self.assertEqual(e.get_all_roles(), ["data2_admin"])
4040

41+
def test_get_list_with_domains(self):
42+
e = self.get_enforcer(
43+
get_examples("rbac_with_domains_model.conf"),
44+
get_examples("rbac_with_domains_policy.csv"),
45+
# True,
46+
)
47+
48+
self.assertEqual(e.get_all_subjects(), ["admin"])
49+
self.assertEqual(e.get_all_objects(), ["data1", "data2"])
50+
self.assertEqual(e.get_all_actions(), ["read", "write"])
51+
self.assertEqual(e.get_all_roles(), ["admin"])
52+
4153
def test_get_policy_api(self):
4254
e = self.get_enforcer(
4355
get_examples("rbac_model.conf"),

0 commit comments

Comments
 (0)