perf: Optimize the cache to improve performance
* Improve performance * Trigger event * add DatabaseAdapterForCacheTest.php Co-authored-by: Donjan <Donjan@cqbaobao.cn>
This commit is contained in:
parent
66a857628e
commit
cc73dea28a
|
@ -135,6 +135,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
$temp = [];
|
$temp = [];
|
||||||
}
|
}
|
||||||
$this->eloquent->insert($cols);
|
$this->eloquent->insert($cols);
|
||||||
|
Rule::fireModelEvent('saved');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,19 +147,14 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
*/
|
*/
|
||||||
public function removePolicy(string $sec, string $ptype, array $rule): void
|
public function removePolicy(string $sec, string $ptype, array $rule): void
|
||||||
{
|
{
|
||||||
$count = 0;
|
|
||||||
|
|
||||||
$instance = $this->eloquent->where('p_type', $ptype);
|
$instance = $this->eloquent->where('p_type', $ptype);
|
||||||
|
|
||||||
foreach ($rule as $key => $value) {
|
foreach ($rule as $key => $value) {
|
||||||
$instance->where('v'.strval($key), $value);
|
$instance->where('v'.strval($key), $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($instance->get() as $model) {
|
$instance->delete();
|
||||||
if ($model->delete()) {
|
Rule::fireModelEvent('deleted');
|
||||||
++$count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -171,7 +167,6 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
*/
|
*/
|
||||||
public function removePolicies(string $sec, string $ptype, array $rules): void
|
public function removePolicies(string $sec, string $ptype, array $rules): void
|
||||||
{
|
{
|
||||||
$count = 0;
|
|
||||||
$instance = $this->eloquent->where('p_type', $ptype);
|
$instance = $this->eloquent->where('p_type', $ptype);
|
||||||
foreach($rules as $rule)
|
foreach($rules as $rule)
|
||||||
{
|
{
|
||||||
|
@ -184,8 +179,8 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
foreach($keys as $key){
|
foreach($keys as $key){
|
||||||
$instance->whereIn($key, $con[$key]);
|
$instance->whereIn($key, $con[$key]);
|
||||||
}
|
}
|
||||||
$num = $instance->delete();
|
$instance->delete();
|
||||||
$count += $num;
|
Rule::fireModelEvent('deleted');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -199,9 +194,8 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
*/
|
*/
|
||||||
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
|
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
|
||||||
{
|
{
|
||||||
$count = 0;
|
|
||||||
|
|
||||||
$instance = $this->eloquent->where('p_type', $ptype);
|
$instance = $this->eloquent->where('p_type', $ptype);
|
||||||
|
|
||||||
foreach (range(0, 5) as $value) {
|
foreach (range(0, 5) as $value) {
|
||||||
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
|
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
|
||||||
if ('' != $fieldValues[$value - $fieldIndex]) {
|
if ('' != $fieldValues[$value - $fieldIndex]) {
|
||||||
|
@ -209,12 +203,9 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($instance->get() as $model) {
|
$instance->delete();
|
||||||
if ($model->delete()) {
|
Rule::fireModelEvent('deleted');
|
||||||
++$count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -238,6 +229,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
|
||||||
$update['v' . $k] = $v;
|
$update['v' . $k] = $v;
|
||||||
}
|
}
|
||||||
$instance->update($update);
|
$instance->update($update);
|
||||||
|
Rule::fireModelEvent('saved');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,149 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lauthz\Tests;
|
||||||
|
|
||||||
|
use Enforcer;
|
||||||
|
use Lauthz\Models\Rule;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Casbin\Persist\Adapters\Filter;
|
||||||
|
use Casbin\Exceptions\InvalidFilterTypeException;
|
||||||
|
|
||||||
|
class DatabaseAdapterForCacheTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
use DatabaseMigrations;
|
||||||
|
|
||||||
|
public function testAddPolicy()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$this->assertFalse(Enforcer::enforce('eve', 'data3', 'read'));
|
||||||
|
Enforcer::addPermissionForUser('eve', 'data3', 'read');
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertTrue(Enforcer::enforce('eve', 'data3', 'read'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddPolicies()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$policies = [
|
||||||
|
['u1', 'd1', 'read'],
|
||||||
|
['u2', 'd2', 'read'],
|
||||||
|
['u3', 'd3', 'read'],
|
||||||
|
];
|
||||||
|
$this->refreshPolicies();
|
||||||
|
Rule::truncate();
|
||||||
|
Enforcer::addPolicies($policies);
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertEquals($policies, Enforcer::getPolicy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSavePolicy()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$this->assertFalse(Enforcer::enforce('alice', 'data4', 'read'));
|
||||||
|
|
||||||
|
$model = Enforcer::getModel();
|
||||||
|
$model->clearPolicy();
|
||||||
|
$model->addPolicy('p', 'p', ['alice', 'data4', 'read']);
|
||||||
|
|
||||||
|
$adapter = Enforcer::getAdapter();
|
||||||
|
$adapter->savePolicy($model);
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertTrue(Enforcer::enforce('alice', 'data4', 'read'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemovePolicy()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
|
||||||
|
|
||||||
|
Enforcer::addPermissionForUser('alice', 'data5', 'read');
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertTrue(Enforcer::enforce('alice', 'data5', 'read'));
|
||||||
|
|
||||||
|
Enforcer::deletePermissionForUser('alice', 'data5', 'read');
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemovePolicies()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$this->assertEquals([
|
||||||
|
['alice', 'data1', 'read'],
|
||||||
|
['bob', 'data2', 'write'],
|
||||||
|
['data2_admin', 'data2', 'read'],
|
||||||
|
['data2_admin', 'data2', 'write'],
|
||||||
|
], Enforcer::getPolicy());
|
||||||
|
|
||||||
|
Enforcer::removePolicies([
|
||||||
|
['data2_admin', 'data2', 'read'],
|
||||||
|
['data2_admin', 'data2', 'write'],
|
||||||
|
]);
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertEquals([
|
||||||
|
['alice', 'data1', 'read'],
|
||||||
|
['bob', 'data2', 'write']
|
||||||
|
], Enforcer::getPolicy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveFilteredPolicy()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$this->assertTrue(Enforcer::enforce('alice', 'data1', 'read'));
|
||||||
|
Enforcer::removeFilteredPolicy(1, 'data1');
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertFalse(Enforcer::enforce('alice', 'data1', 'read'));
|
||||||
|
$this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
|
||||||
|
$this->assertTrue(Enforcer::enforce('alice', 'data2', 'read'));
|
||||||
|
$this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
|
||||||
|
Enforcer::removeFilteredPolicy(1, 'data2', 'read');
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
|
||||||
|
$this->assertFalse(Enforcer::enforce('alice', 'data2', 'read'));
|
||||||
|
$this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
|
||||||
|
Enforcer::removeFilteredPolicy(2, 'write');
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertFalse(Enforcer::enforce('bob', 'data2', 'write'));
|
||||||
|
$this->assertFalse(Enforcer::enforce('alice', 'data2', 'write'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdatePolicy()
|
||||||
|
{
|
||||||
|
$this->enableCache();
|
||||||
|
$this->assertEquals([
|
||||||
|
['alice', 'data1', 'read'],
|
||||||
|
['bob', 'data2', 'write'],
|
||||||
|
['data2_admin', 'data2', 'read'],
|
||||||
|
['data2_admin', 'data2', 'write'],
|
||||||
|
], Enforcer::getPolicy());
|
||||||
|
|
||||||
|
Enforcer::updatePolicy(
|
||||||
|
['alice', 'data1', 'read'],
|
||||||
|
['alice', 'data1', 'write']
|
||||||
|
);
|
||||||
|
|
||||||
|
Enforcer::updatePolicy(
|
||||||
|
['bob', 'data2', 'write'],
|
||||||
|
['bob', 'data2', 'read']
|
||||||
|
);
|
||||||
|
$this->refreshPolicies();
|
||||||
|
$this->assertEquals([
|
||||||
|
['alice', 'data1', 'write'],
|
||||||
|
['bob', 'data2', 'read'],
|
||||||
|
['data2_admin', 'data2', 'read'],
|
||||||
|
['data2_admin', 'data2', 'write'],
|
||||||
|
], Enforcer::getPolicy());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function refreshPolicies()
|
||||||
|
{
|
||||||
|
Enforcer::loadPolicy();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function enableCache()
|
||||||
|
{
|
||||||
|
$this->app['config']->set('lauthz.basic.cache.enabled', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue