From ca6f570cd1b2d1b24ec8f30ade421477cf306e29 Mon Sep 17 00:00:00 2001 From: basakest Date: Thu, 1 Apr 2021 15:56:57 +0800 Subject: [PATCH] feat: support Casbin BatchAdapter interface fix: refactor the logic --- src/Adapters/DatabaseAdapter.php | 52 +++++++++++++++++++++++++++++++- tests/DatabaseAdapterTest.php | 35 +++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/Adapters/DatabaseAdapter.php b/src/Adapters/DatabaseAdapter.php index e84bc2f..7da4b24 100644 --- a/src/Adapters/DatabaseAdapter.php +++ b/src/Adapters/DatabaseAdapter.php @@ -11,8 +11,9 @@ use EasySwoole\ORM\Exception\Exception; use EasySwoole\Permission\Model\RulesModel; use Throwable; +use Casbin\Persist\BatchAdapter; -class DatabaseAdapter implements Adapter +class DatabaseAdapter implements Adapter, BatchAdapter { use AdapterHelper; @@ -145,4 +146,53 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex $instance->destroy(); } + + /** + * adds a policy rules to the storage. + * This is part of the Auto-Save feature. + * + * @param string $sec + * @param string $ptype + * @param array $rules + */ + public function addPolicies(string $sec, string $ptype, array $rules): void + { + $cols = []; + + foreach($rules as $rule) { + $temp = []; + $temp['ptype'] = $ptype; + foreach ($rule as $key => $value) { + $temp['v'.strval($key)] = $value; + } + $cols[] = $temp; + } + + RulesModel::create()->saveAll($cols); + } + + /** + * removes policy rules from the storage. + * This is part of the Auto-Save feature. + * + * @param string $sec + * @param string $ptype + * @param array $rules + */ + public function removePolicies(string $sec, string $ptype, array $rules): void + { + $ids = []; + + foreach($rules as $rule) { + $where = []; + $where['ptype'] = $ptype; + foreach ($rule as $key => $value) { + $where['v'.strval($key)] = $value; + } + $ret = RulesModel::create()->get($where); + $ret && $ids[] = $ret->id; + } + + RulesModel::create()->destroy($ids); + } } diff --git a/tests/DatabaseAdapterTest.php b/tests/DatabaseAdapterTest.php index 2a952c0..e8db6d6 100644 --- a/tests/DatabaseAdapterTest.php +++ b/tests/DatabaseAdapterTest.php @@ -108,4 +108,39 @@ public function testSavePolicy() $adapter->savePolicy($model); $this->assertTrue($e->enforce('alice', 'data4', 'read')); } + + public function testAddPolicies() + { + $policies = [ + ['u1', 'd1', 'read'], + ['u2', 'd2', 'read'], + ['u3', 'd3', 'read'], + ]; + $e = $this->getEnforcer(); + $e->clearPolicy(); + $this->assertEquals([], $e->getPolicy()); + $e->addPolicies($policies); + $this->assertEquals($policies, $e->getPolicy()); + } + + public function testRemovePolicies() + { + $e = $this->getEnforcer(); + $this->assertEquals([ + ['alice', 'data1', 'read'], + ['bob', 'data2', 'write'], + ['data2_admin', 'data2', 'read'], + ['data2_admin', 'data2', 'write'], + ], $e->getPolicy()); + + $e->removePolicies([ + ['data2_admin', 'data2', 'read'], + ['data2_admin', 'data2', 'write'], + ]); + + $this->assertEquals([ + ['alice', 'data1', 'read'], + ['bob', 'data2', 'write'] + ], $e->getPolicy()); + } } \ No newline at end of file