diff --git a/database/migrations/2019_03_01_000000_create_rules_table.php b/database/migrations/2019_03_01_000000_create_rules_table.php index ebda4d1..e455235 100644 --- a/database/migrations/2019_03_01_000000_create_rules_table.php +++ b/database/migrations/2019_03_01_000000_create_rules_table.php @@ -13,7 +13,7 @@ class CreateRulesTable extends Migration $connection = config('lauthz.basic.database.connection') ?: config('database.default'); Schema::connection($connection)->create(config('lauthz.basic.database.rules_table'), function (Blueprint $table) { $table->increments('id'); - $table->string('p_type')->nullable(); + $table->string('ptype')->nullable(); $table->string('v0')->nullable(); $table->string('v1')->nullable(); $table->string('v2')->nullable(); diff --git a/src/Adapters/DatabaseAdapter.php b/src/Adapters/DatabaseAdapter.php index aeea972..63c5a9e 100755 --- a/src/Adapters/DatabaseAdapter.php +++ b/src/Adapters/DatabaseAdapter.php @@ -55,7 +55,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo */ public function savePolicyLine(string $ptype, array $rule): void { - $col['p_type'] = $ptype; + $col['ptype'] = $ptype; foreach ($rule as $key => $value) { $col['v'.strval($key)] = $value; } @@ -127,7 +127,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo $i = 0; foreach($rules as $rule) { - $temp['p_type'] = $ptype; + $temp['ptype'] = $ptype; $temp['created_at'] = new DateTime(); $temp['updated_at'] = $temp['created_at']; foreach ($rule as $key => $value) { @@ -149,7 +149,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo */ 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) { $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 { - $instance = $this->eloquent->where('p_type', $ptype); + $instance = $this->eloquent->where('ptype', $ptype); foreach (range(0, 5) as $value) { 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 { - $instance = $this->eloquent->where('p_type', $ptype); + $instance = $this->eloquent->where('ptype', $ptype); foreach($oldRule as $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 { - $where['p_type'] = $ptype; + $where['ptype'] = $ptype; foreach ($fieldValues as $fieldValue) { if (!is_null($fieldValue) && $fieldValue !== '') { $where['v'. $fieldIndex++] = $fieldValue; @@ -265,7 +265,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo $newP = []; $oldP = []; foreach ($newPolicies as $newRule) { - $col['p_type'] = $ptype; + $col['ptype'] = $ptype; $col['created_at'] = new DateTime(); $col['updated_at'] = $col['created_at']; foreach ($newRule as $key => $value) { @@ -282,7 +282,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo $item = array_filter($item, function ($value) { return !is_null($value) && $value !== ''; }); - unset($item['p_type']); + unset($item['ptype']); } $oldRules->delete(); diff --git a/src/Models/Rule.php b/src/Models/Rule.php index 9550e1e..9259a40 100755 --- a/src/Models/Rule.php +++ b/src/Models/Rule.php @@ -29,7 +29,7 @@ class Rule extends Model * * @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. @@ -62,7 +62,7 @@ class Rule extends Model public function getAllFromCache() { $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)) { return $get(); diff --git a/tests/RequestMiddlewareTest.php b/tests/RequestMiddlewareTest.php index c7dfb0e..b4e6088 100644 --- a/tests/RequestMiddlewareTest.php +++ b/tests/RequestMiddlewareTest.php @@ -68,11 +68,11 @@ EOT; { Rule::truncate(); - Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'GET']); - Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'GET']); - Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'POST']); - Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'PUT']); - Rule::create(['p_type' => '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' => '/foo', 'v2' => 'GET']); + Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'GET']); + Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'POST']); + Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'PUT']); + Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'DELETE']); + Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo1/*', 'v2' => '(GET)|(POST)']); } } diff --git a/tests/RuleCacheTest.php b/tests/RuleCacheTest.php index 8a78277..701bc57 100644 --- a/tests/RuleCacheTest.php +++ b/tests/RuleCacheTest.php @@ -28,7 +28,7 @@ class RuleCacheTest extends TestCase app(Rule::class)->getAllFromCache(); $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(); $this->assertCount(2, DB::getQueryLog()); @@ -48,7 +48,7 @@ class RuleCacheTest extends TestCase app(Rule::class)->getAllFromCache(); $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(); $this->assertCount(3, DB::getQueryLog()); diff --git a/tests/TestCase.php b/tests/TestCase.php index 7771487..262aac2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -53,12 +53,12 @@ abstract class TestCase extends BaseTestCase { Rule::truncate(); - Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']); - Rule::create(['p_type' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']); + Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']); + Rule::create(['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']); - Rule::create(['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']); - Rule::create(['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']); - Rule::create(['p_type' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']); + Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']); + Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']); + Rule::create(['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']); } protected function runMiddleware($middleware, $request, ...$args)