Skip to content

update:#25 modify column p_type to ptype #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function up()
$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();
Expand Down
16 changes: 8 additions & 8 deletions src/Adapters/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(Rule $eloquent)
*/
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;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
$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) {
Expand All @@ -149,7 +149,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): 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) {
$instance->where('v'.strval($key), $value);
Expand Down Expand Up @@ -187,7 +187,7 @@ public function removePolicies(string $sec, string $ptype, array $rules): 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) {
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
Expand All @@ -212,7 +212,7 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
*/
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);
}
Expand Down Expand Up @@ -255,7 +255,7 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
*/
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;
Expand All @@ -265,7 +265,7 @@ public function updateFilteredPolicies(string $sec, string $ptype, array $newPol
$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) {
Expand All @@ -282,7 +282,7 @@ public function updateFilteredPolicies(string $sec, string $ptype, array $newPol
$item = array_filter($item, function ($value) {
return !is_null($value) && $value !== '';
});
unset($item['p_type']);
unset($item['ptype']);
}

$oldRules->delete();
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,7 +62,7 @@ public function __construct(array $attributes = [], $guard = '')
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();
Expand Down
12 changes: 6 additions & 6 deletions tests/RequestMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ protected function initTable()
{
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)']);
}
}
4 changes: 2 additions & 2 deletions tests/RuleCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testEnableCache()
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());

Expand All @@ -48,7 +48,7 @@ public function testDisableCache()
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());

Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ protected function initTable()
{
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)
Expand Down