Skip to content

Commit

Permalink
Merge fb58023 into 5987342
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest committed Aug 25, 2021
2 parents 5987342 + fb58023 commit c29083a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/Adapter.php
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
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

0 comments on commit c29083a

Please sign in to comment.