Skip to content

Commit be98eed

Browse files
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>
1 parent 1f0702a commit be98eed

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

database/migrations/2019_03_01_000000_create_rules_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function up()
1313
$connection = config('lauthz.basic.database.connection') ?: config('database.default');
1414
Schema::connection($connection)->create(config('lauthz.basic.database.rules_table'), function (Blueprint $table) {
1515
$table->increments('id');
16-
$table->string('p_type')->nullable();
16+
$table->string('ptype')->nullable();
1717
$table->string('v0')->nullable();
1818
$table->string('v1')->nullable();
1919
$table->string('v2')->nullable();

src/Adapters/DatabaseAdapter.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(Rule $eloquent)
5555
*/
5656
public function savePolicyLine(string $ptype, array $rule): void
5757
{
58-
$col['p_type'] = $ptype;
58+
$col['ptype'] = $ptype;
5959
foreach ($rule as $key => $value) {
6060
$col['v'.strval($key)] = $value;
6161
}
@@ -127,7 +127,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
127127
$i = 0;
128128

129129
foreach($rules as $rule) {
130-
$temp['p_type'] = $ptype;
130+
$temp['ptype'] = $ptype;
131131
$temp['created_at'] = new DateTime();
132132
$temp['updated_at'] = $temp['created_at'];
133133
foreach ($rule as $key => $value) {
@@ -149,7 +149,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
149149
*/
150150
public function removePolicy(string $sec, string $ptype, array $rule): void
151151
{
152-
$instance = $this->eloquent->where('p_type', $ptype);
152+
$instance = $this->eloquent->where('ptype', $ptype);
153153

154154
foreach ($rule as $key => $value) {
155155
$instance->where('v'.strval($key), $value);
@@ -187,7 +187,7 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
187187
*/
188188
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
189189
{
190-
$instance = $this->eloquent->where('p_type', $ptype);
190+
$instance = $this->eloquent->where('ptype', $ptype);
191191

192192
foreach (range(0, 5) as $value) {
193193
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
@@ -212,7 +212,7 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
212212
*/
213213
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
214214
{
215-
$instance = $this->eloquent->where('p_type', $ptype);
215+
$instance = $this->eloquent->where('ptype', $ptype);
216216
foreach($oldRule as $k => $v) {
217217
$instance->where('v' . $k, $v);
218218
}
@@ -255,7 +255,7 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
255255
*/
256256
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
257257
{
258-
$where['p_type'] = $ptype;
258+
$where['ptype'] = $ptype;
259259
foreach ($fieldValues as $fieldValue) {
260260
if (!is_null($fieldValue) && $fieldValue !== '') {
261261
$where['v'. $fieldIndex++] = $fieldValue;
@@ -265,7 +265,7 @@ public function updateFilteredPolicies(string $sec, string $ptype, array $newPol
265265
$newP = [];
266266
$oldP = [];
267267
foreach ($newPolicies as $newRule) {
268-
$col['p_type'] = $ptype;
268+
$col['ptype'] = $ptype;
269269
$col['created_at'] = new DateTime();
270270
$col['updated_at'] = $col['created_at'];
271271
foreach ($newRule as $key => $value) {
@@ -282,7 +282,7 @@ public function updateFilteredPolicies(string $sec, string $ptype, array $newPol
282282
$item = array_filter($item, function ($value) {
283283
return !is_null($value) && $value !== '';
284284
});
285-
unset($item['p_type']);
285+
unset($item['ptype']);
286286
}
287287

288288
$oldRules->delete();

src/Models/Rule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Rule extends Model
2929
*
3030
* @var array
3131
*/
32-
protected $fillable = ['p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
32+
protected $fillable = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
3333

3434
/**
3535
* Create a new Eloquent model instance.
@@ -62,7 +62,7 @@ public function __construct(array $attributes = [], $guard = '')
6262
public function getAllFromCache()
6363
{
6464
$get = function () {
65-
return $this->select('p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
65+
return $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
6666
};
6767
if (!$this->config('cache.enabled', false)) {
6868
return $get();

tests/RequestMiddlewareTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ protected function initTable()
6868
{
6969
Rule::truncate();
7070

71-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'GET']);
72-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'GET']);
73-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'POST']);
74-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'PUT']);
75-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'DELETE']);
76-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => '/foo1/*', 'v2' => '(GET)|(POST)']);
71+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'GET']);
72+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'GET']);
73+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo', 'v2' => 'POST']);
74+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'PUT']);
75+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo/:id', 'v2' => 'DELETE']);
76+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => '/foo1/*', 'v2' => '(GET)|(POST)']);
7777
}
7878
}

tests/RuleCacheTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testEnableCache()
2828
app(Rule::class)->getAllFromCache();
2929
$this->assertCount(0, DB::getQueryLog());
3030

31-
$rule = Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
31+
$rule = Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
3232
app(Rule::class)->getAllFromCache();
3333
$this->assertCount(2, DB::getQueryLog());
3434

@@ -48,7 +48,7 @@ public function testDisableCache()
4848
app(Rule::class)->getAllFromCache();
4949
$this->assertCount(1, DB::getQueryLog());
5050

51-
$rule = Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
51+
$rule = Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
5252
app(Rule::class)->getAllFromCache();
5353
$this->assertCount(3, DB::getQueryLog());
5454

tests/TestCase.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ protected function initTable()
5353
{
5454
Rule::truncate();
5555

56-
Rule::create(['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
57-
Rule::create(['p_type' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']);
56+
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
57+
Rule::create(['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']);
5858

59-
Rule::create(['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']);
60-
Rule::create(['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']);
61-
Rule::create(['p_type' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']);
59+
Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']);
60+
Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']);
61+
Rule::create(['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']);
6262
}
6363

6464
protected function runMiddleware($middleware, $request, ...$args)

0 commit comments

Comments
 (0)