Skip to content

Commit 353d4d8

Browse files
committed
Improve add_policy api
1 parent 652f687 commit 353d4d8

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

casbin/model/policy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ def has_policy(self, sec, ptype, rule):
6161

6262
return False
6363

64+
def add_policy(self, sec, ptype, rule):
65+
"""adds a policy rule to the model."""
66+
67+
if not self.has_policy(sec, ptype, rule):
68+
self.model[sec][ptype].policy.append(rule)
69+
return True
70+
71+
return False
72+
6473
def get_values_for_field_in_policy(self, sec, ptype, field_index):
6574
"""gets all values for a field for all rules in a policy, duplicated values are removed."""
6675

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name="casbin",
10-
version="0.4",
10+
version="0.5",
1111
author="TechLee",
1212
author_email="techlee@qq.com",
1313
description="An authorization library that supports access control models like ACL, RBAC, ABAC in Python",

tests/test_management_api.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,28 @@ def test_get_policy_api(self):
6060
self.assertEqual(e.get_filtered_grouping_policy(0, '', 'data2_admin'), [['alice', 'data2_admin']])
6161
self.assertTrue(e.has_grouping_policy(['alice', 'data2_admin']))
6262
self.assertFalse(e.has_grouping_policy(['bob', 'data2_admin']))
63+
64+
def test_modify_policy_api(self):
65+
e = get_enforcer(
66+
get_examples("rbac_model.conf"),
67+
get_examples("rbac_policy.csv"),
68+
# True,
69+
)
70+
71+
self.assertEqual(e.get_policy(), [
72+
['alice', 'data1', 'read'],
73+
['bob', 'data2', 'write'],
74+
['data2_admin', 'data2', 'read'],
75+
['data2_admin', 'data2', 'write'],
76+
])
77+
78+
e.add_policy('eve', 'data3', 'read')
79+
e.add_named_policy('p', ['eve', 'data3', 'write'])
80+
self.assertEqual(e.get_policy(), [
81+
['alice', 'data1', 'read'],
82+
['bob', 'data2', 'write'],
83+
['data2_admin', 'data2', 'read'],
84+
['data2_admin', 'data2', 'write'],
85+
['eve', 'data3', 'read'],
86+
['eve', 'data3', 'write'],
87+
])

0 commit comments

Comments
 (0)