update:#25 modify column p_type to ptype

This commit is contained in:
liujun 2021-09-07 16:35:19 +08:00
parent 1f0702af05
commit 3c6a5e10b9
6 changed files with 24 additions and 24 deletions

View File

@ -13,7 +13,7 @@ class CreateRulesTable extends Migration
$connection = config('lauthz.basic.database.connection') ?: config('database.default'); $connection = config('lauthz.basic.database.connection') ?: config('database.default');
Schema::connection($connection)->create(config('lauthz.basic.database.rules_table'), function (Blueprint $table) { Schema::connection($connection)->create(config('lauthz.basic.database.rules_table'), function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('p_type')->nullable(); $table->string('ptype')->nullable();
$table->string('v0')->nullable(); $table->string('v0')->nullable();
$table->string('v1')->nullable(); $table->string('v1')->nullable();
$table->string('v2')->nullable(); $table->string('v2')->nullable();

View File

@ -55,7 +55,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
*/ */
public function savePolicyLine(string $ptype, array $rule): void public function savePolicyLine(string $ptype, array $rule): void
{ {
$col['p_type'] = $ptype; $col['ptype'] = $ptype;
foreach ($rule as $key => $value) { foreach ($rule as $key => $value) {
$col['v'.strval($key)] = $value; $col['v'.strval($key)] = $value;
} }
@ -127,7 +127,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
$i = 0; $i = 0;
foreach($rules as $rule) { foreach($rules as $rule) {
$temp['p_type'] = $ptype; $temp['ptype'] = $ptype;
$temp['created_at'] = new DateTime(); $temp['created_at'] = new DateTime();
$temp['updated_at'] = $temp['created_at']; $temp['updated_at'] = $temp['created_at'];
foreach ($rule as $key => $value) { foreach ($rule as $key => $value) {
@ -149,7 +149,7 @@ 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
{ {
$instance = $this->eloquent->where('p_type', $ptype); $instance = $this->eloquent->where('ptype', $ptype);
foreach ($rule as $key => $value) { foreach ($rule as $key => $value) {
$instance->where('v'.strval($key), $value); $instance->where('v'.strval($key), $value);
@ -187,7 +187,7 @@ 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
{ {
$instance = $this->eloquent->where('p_type', $ptype); $instance = $this->eloquent->where('ptype', $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)) {
@ -212,7 +212,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
*/ */
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
{ {
$instance = $this->eloquent->where('p_type', $ptype); $instance = $this->eloquent->where('ptype', $ptype);
foreach($oldRule as $k => $v) { foreach($oldRule as $k => $v) {
$instance->where('v' . $k, $v); $instance->where('v' . $k, $v);
} }
@ -255,7 +255,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
*/ */
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
{ {
$where['p_type'] = $ptype; $where['ptype'] = $ptype;
foreach ($fieldValues as $fieldValue) { foreach ($fieldValues as $fieldValue) {
if (!is_null($fieldValue) && $fieldValue !== '') { if (!is_null($fieldValue) && $fieldValue !== '') {
$where['v'. $fieldIndex++] = $fieldValue; $where['v'. $fieldIndex++] = $fieldValue;
@ -265,7 +265,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
$newP = []; $newP = [];
$oldP = []; $oldP = [];
foreach ($newPolicies as $newRule) { foreach ($newPolicies as $newRule) {
$col['p_type'] = $ptype; $col['ptype'] = $ptype;
$col['created_at'] = new DateTime(); $col['created_at'] = new DateTime();
$col['updated_at'] = $col['created_at']; $col['updated_at'] = $col['created_at'];
foreach ($newRule as $key => $value) { foreach ($newRule as $key => $value) {
@ -282,7 +282,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
$item = array_filter($item, function ($value) { $item = array_filter($item, function ($value) {
return !is_null($value) && $value !== ''; return !is_null($value) && $value !== '';
}); });
unset($item['p_type']); unset($item['ptype']);
} }
$oldRules->delete(); $oldRules->delete();

View File

@ -29,7 +29,7 @@ class Rule extends Model
* *
* @var array * @var array
*/ */
protected $fillable = ['p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']; protected $fillable = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
/** /**
* Create a new Eloquent model instance. * Create a new Eloquent model instance.
@ -62,7 +62,7 @@ class Rule extends Model
public function getAllFromCache() public function getAllFromCache()
{ {
$get = function () { $get = function () {
return $this->select('p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray(); return $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
}; };
if (!$this->config('cache.enabled', false)) { if (!$this->config('cache.enabled', false)) {
return $get(); return $get();

View File

@ -68,11 +68,11 @@ EOT;
{ {
Rule::truncate(); Rule::truncate();
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'GET']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'GET']);
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'GET']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'GET']);
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'POST']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'POST']);
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'PUT']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'PUT']);
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'DELETE']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'DELETE']);
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo1/*', 'v2' => '(GET)|(POST)']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo1/*', 'v2' => '(GET)|(POST)']);
} }
} }

View File

@ -28,7 +28,7 @@ class RuleCacheTest extends TestCase
app(Rule::class)->getAllFromCache(); app(Rule::class)->getAllFromCache();
$this->assertCount(0, DB::getQueryLog()); $this->assertCount(0, DB::getQueryLog());
$rule = Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']); $rule = Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
app(Rule::class)->getAllFromCache(); app(Rule::class)->getAllFromCache();
$this->assertCount(2, DB::getQueryLog()); $this->assertCount(2, DB::getQueryLog());
@ -48,7 +48,7 @@ class RuleCacheTest extends TestCase
app(Rule::class)->getAllFromCache(); app(Rule::class)->getAllFromCache();
$this->assertCount(1, DB::getQueryLog()); $this->assertCount(1, DB::getQueryLog());
$rule = Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']); $rule = Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
app(Rule::class)->getAllFromCache(); app(Rule::class)->getAllFromCache();
$this->assertCount(3, DB::getQueryLog()); $this->assertCount(3, DB::getQueryLog());

View File

@ -53,12 +53,12 @@ abstract class TestCase extends BaseTestCase
{ {
Rule::truncate(); Rule::truncate();
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']); Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
Rule::create(['p_type' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']); Rule::create(['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']);
Rule::create(['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']); Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']);
Rule::create(['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']); Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']);
Rule::create(['p_type' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']); Rule::create(['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']);
} }
protected function runMiddleware($middleware, $request, ...$args) protected function runMiddleware($middleware, $request, ...$args)