BREAKING CHANGE: modify column p_type to ptype (#31)

BREAKING CHANGE: modify column p_type to ptype (#31)

Co-authored-by: liujun <liujun@163.com>
This commit is contained in:
dawn-darkest 2021-09-07 18:40:12 +08:00 committed by GitHub
parent 1f0702af05
commit be98eed115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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');
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();

View File

@ -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();

View File

@ -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();

View File

@ -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)']);
}
}

View File

@ -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());

View File

@ -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)