-
Notifications
You must be signed in to change notification settings - Fork 53
feat: support updateFilteredPolicies method #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,133 @@ public function testUpdatePolicies() | |
], Enforcer::getPolicy()); | ||
} | ||
|
||
public function arrayEqualsWithoutOrder(array $expected, array $actual) | ||
{ | ||
if (method_exists($this, 'assertEqualsCanonicalizing')) { | ||
$this->assertEqualsCanonicalizing($expected, $actual); | ||
} else { | ||
array_multisort($expected); | ||
array_multisort($actual); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
} | ||
|
||
public function testUpdateFilteredPolicies() | ||
{ | ||
$this->assertEquals([ | ||
['alice', 'data1', 'read'], | ||
['bob', 'data2', 'write'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'], | ||
], Enforcer::getPolicy()); | ||
|
||
Enforcer::updateFilteredPolicies([["alice", "data1", "write"]], 0, "alice", "data1", "read"); | ||
Enforcer::updateFilteredPolicies([["bob", "data2", "read"]], 0, "bob", "data2", "write"); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test case can be more comprehensive, such as:
|
||
$policies = [ | ||
['alice', 'data1', 'write'], | ||
['bob', 'data2', 'read'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'] | ||
]; | ||
|
||
$this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy()); | ||
|
||
// test use updateFilteredPolicies to update all policies of a user | ||
$this->initTable(); | ||
Enforcer::loadPolicy(); | ||
$policies = [ | ||
['alice', 'data2', 'write'], | ||
['bob', 'data1', 'read'] | ||
]; | ||
Enforcer::addPolicies($policies); | ||
|
||
$this->arrayEqualsWithoutOrder([ | ||
['alice', 'data1', 'read'], | ||
['bob', 'data2', 'write'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'], | ||
['alice', 'data2', 'write'], | ||
['bob', 'data1', 'read'] | ||
], Enforcer::getPolicy()); | ||
|
||
Enforcer::updateFilteredPolicies([['alice', 'data1', 'write'], ['alice', 'data2', 'read']], 0, 'alice'); | ||
Enforcer::updateFilteredPolicies([['bob', 'data1', 'write'], ["bob", "data2", "read"]], 0, 'bob'); | ||
|
||
$policies = [ | ||
['alice', 'data1', 'write'], | ||
['alice', 'data2', 'read'], | ||
['bob', 'data1', 'write'], | ||
['bob', 'data2', 'read'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'] | ||
]; | ||
|
||
$this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy()); | ||
|
||
// test if $fieldValues contains empty string | ||
$this->initTable(); | ||
Enforcer::loadPolicy(); | ||
$policies = [ | ||
['alice', 'data2', 'write'], | ||
['bob', 'data1', 'read'] | ||
]; | ||
Enforcer::addPolicies($policies); | ||
|
||
$this->assertEquals([ | ||
['alice', 'data1', 'read'], | ||
['bob', 'data2', 'write'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'], | ||
['alice', 'data2', 'write'], | ||
['bob', 'data1', 'read'] | ||
], Enforcer::getPolicy()); | ||
|
||
Enforcer::updateFilteredPolicies([['alice', 'data1', 'write'], ['alice', 'data2', 'read']], 0, 'alice', '', ''); | ||
Enforcer::updateFilteredPolicies([['bob', 'data1', 'write'], ["bob", "data2", "read"]], 0, 'bob', '', ''); | ||
|
||
$policies = [ | ||
['alice', 'data1', 'write'], | ||
['alice', 'data2', 'read'], | ||
['bob', 'data1', 'write'], | ||
['bob', 'data2', 'read'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'] | ||
]; | ||
|
||
$this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy()); | ||
|
||
// test if $fieldIndex is not zero | ||
$this->initTable(); | ||
Enforcer::loadPolicy(); | ||
$policies = [ | ||
['alice', 'data2', 'write'], | ||
['bob', 'data1', 'read'] | ||
]; | ||
Enforcer::addPolicies($policies); | ||
|
||
$this->assertEquals([ | ||
['alice', 'data1', 'read'], | ||
['bob', 'data2', 'write'], | ||
['data2_admin', 'data2', 'read'], | ||
['data2_admin', 'data2', 'write'], | ||
['alice', 'data2', 'write'], | ||
['bob', 'data1', 'read'] | ||
], Enforcer::getPolicy()); | ||
|
||
Enforcer::updateFilteredPolicies([['alice', 'data1', 'write'], ['bob', 'data1', 'write']], 2, 'read'); | ||
Enforcer::updateFilteredPolicies([['alice', 'data2', 'read'], ["bob", "data2", "read"]], 2, 'write'); | ||
|
||
$policies = [ | ||
['alice', 'data1', 'write'], | ||
['alice', 'data2', 'read'], | ||
['bob', 'data1', 'write'], | ||
['bob', 'data2', 'read'], | ||
]; | ||
|
||
$this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy()); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it commented out this code? @basakest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method calls some methods in php-casbin, and the pr has not been merged before @techoner There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @basakest It has been merged , |
||
public function testLoadFilteredPolicy() | ||
{ | ||
$this->initTable(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@basakest I think this is not complete enough, empty("" or null) values are not excluded .