Skip to content

feat: support Casbin BatchAdapter interface #13

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

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use yii\permission\models\CasbinRule;
use Casbin\Model\Model;
use Casbin\Persist\Adapter as AdapterContract;
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
use Casbin\Persist\AdapterHelper;

/**
* DatabaseAdapter.
*
* @author techlee@qq.com
*/
class Adapter implements AdapterContract
class Adapter implements AdapterContract, BatchAdapterContract
{
use AdapterHelper;

Expand Down Expand Up @@ -85,6 +86,37 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
$this->savePolicyLine($ptype, $rule);
}

/**
* Adds a policy rules to the storage.
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string[][] $rules
*/
public function addPolicies(string $sec, string $ptype, array $rules): void
{
$rows = [];
$columns = array_keys($rules[0]);
array_walk($columns, function (&$item) {
$item = 'v' . strval($item);
});
array_unshift($columns, 'ptype');

foreach ($rules as $rule) {
$temp['`ptype`'] = $ptype;
foreach ($rule as $key => $value) {
$temp['`v'. strval($key) . '`'] = $value;
}
$rows[] = $temp;
$temp = [];
}

$command = $this->casbinRule->getDb()->createCommand();
$tableName = $this->casbinRule->tableName();
$command->batchInsert($tableName, $columns, $rows)->execute();
}

/**
* This is part of the Auto-Save feature.
*
Expand All @@ -104,6 +136,27 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
$this->casbinRule->deleteAll($where);
}

/**
* Removes policy rules from the storage.
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string[][] $rules
*/
public function removePolicies(string $sec, string $ptype, array $rules): void
{
$transaction = $this->casbinRule->getDb()->beginTransaction();
try {
foreach ($rules as $rule) {
$this->removePolicy($sec, $ptype, $rule);
}
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
}
}

/**
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
* This is part of the Auto-Save feature.
Expand Down
33 changes: 33 additions & 0 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function testAddPolicy()
$this->assertTrue(Yii::$app->permission->enforce('eve', 'data3', 'read'));
}

public function testAddPolicies()
{
$policies = [
['u1', 'd1', 'read'],
['u2', 'd2', 'read'],
['u3', 'd3', 'read'],
];
Yii::$app->permission->clearPolicy();
$this->assertEquals([], Yii::$app->permission->getPolicy());
Yii::$app->permission->addPolicies($policies);
$this->assertEquals($policies, Yii::$app->permission->getPolicy());
}

public function testSavePolicy()
{
$this->assertFalse(Yii::$app->permission->enforce('alice', 'data4', 'read'));
Expand All @@ -53,6 +66,26 @@ public function testRemovePolicy()
$this->assertFalse(Yii::$app->permission->enforce('alice', 'data5', 'read'));
}

public function testRemovePolicies()
{
$this->assertEquals([
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
], Yii::$app->permission->getPolicy());

Yii::$app->permission->removePolicies([
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
]);

$this->assertEquals([
['alice', 'data1', 'read'],
['bob', 'data2', 'write']
], Yii::$app->permission->getPolicy());
}

public function testRemoveFilteredPolicy()
{
$this->assertTrue(Yii::$app->permission->enforce('alice', 'data1', 'read'));
Expand Down