Skip to content

Commit

Permalink
Improve add_policy api
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Jun 5, 2019
1 parent 652f687 commit 353d4d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
9 changes: 9 additions & 0 deletions casbin/model/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def has_policy(self, sec, ptype, rule):

return False

def add_policy(self, sec, ptype, rule):
"""adds a policy rule to the model."""

if not self.has_policy(sec, ptype, rule):
self.model[sec][ptype].policy.append(rule)
return True

return False

def get_values_for_field_in_policy(self, sec, ptype, field_index):
"""gets all values for a field for all rules in a policy, duplicated values are removed."""

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="casbin",
version="0.4",
version="0.5",
author="TechLee",
author_email="[email protected]",
description="An authorization library that supports access control models like ACL, RBAC, ABAC in Python",
Expand Down
25 changes: 25 additions & 0 deletions tests/test_management_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,28 @@ def test_get_policy_api(self):
self.assertEqual(e.get_filtered_grouping_policy(0, '', 'data2_admin'), [['alice', 'data2_admin']])
self.assertTrue(e.has_grouping_policy(['alice', 'data2_admin']))
self.assertFalse(e.has_grouping_policy(['bob', 'data2_admin']))

def test_modify_policy_api(self):
e = get_enforcer(
get_examples("rbac_model.conf"),
get_examples("rbac_policy.csv"),
# True,
)

self.assertEqual(e.get_policy(), [
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
])

e.add_policy('eve', 'data3', 'read')
e.add_named_policy('p', ['eve', 'data3', 'write'])
self.assertEqual(e.get_policy(), [
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
['eve', 'data3', 'read'],
['eve', 'data3', 'write'],
])

0 comments on commit 353d4d8

Please sign in to comment.