Skip to content
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

Change allowed_ips to non-nullable #373

Merged
merged 4 commits into from
Jun 13, 2024
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
4 changes: 1 addition & 3 deletions app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public function form(Form $form): Form
->placeholder('Example: 127.0.0.1 or 192.168.1.1')
->label('Whitelisted IPv4 Addresses')
->helperText('Press enter to add a new IP address or leave blank to allow any IP address')
->columnSpanFull()
->hidden()
->default(null),
->columnSpanFull(),

Forms\Components\Textarea::make('memo')
->required()
Expand Down
11 changes: 9 additions & 2 deletions app/Models/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @property int $key_type
* @property string $identifier
* @property string $token
* @property array|null $allowed_ips
* @property array $allowed_ips
* @property string|null $memo
* @property \Illuminate\Support\Carbon|null $last_used_at
* @property \Illuminate\Support\Carbon|null $expires_at
Expand Down Expand Up @@ -113,6 +113,13 @@ class ApiKey extends Model
'r_' . AdminAcl::RESOURCE_MOUNTS,
];

/**
* Default attributes when creating a new model.
*/
protected $attributes = [
'allowed_ips' => '[]',
];

/**
* Fields that should not be included when calling toArray() or toJson()
* on this model.
Expand All @@ -128,7 +135,7 @@ class ApiKey extends Model
'identifier' => 'required|string|size:16|unique:api_keys,identifier',
'token' => 'required|string',
'memo' => 'required|nullable|string|max:500',
'allowed_ips' => 'nullable|array',
'allowed_ips' => 'array',
'allowed_ips.*' => 'string',
'last_used_at' => 'nullable|date',
'expires_at' => 'nullable|date',
Expand Down
2 changes: 1 addition & 1 deletion database/Factories/ApiKeyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function definition(): array
'key_type' => ApiKey::TYPE_APPLICATION,
'identifier' => ApiKey::generateTokenIdentifier(ApiKey::TYPE_APPLICATION),
'token' => $token ?: $token = Str::random(ApiKey::KEY_LENGTH),
'allowed_ips' => null,
'allowed_ips' => [],
'memo' => 'Test Function Key',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::table('api_keys')->whereNull('allowed_ips')->update([
'allowed_ips' => '[]',
]);

Schema::table('api_keys', function (Blueprint $table) {
$table->text('allowed_ips')->nullable(false)->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('api_keys', function (Blueprint $table) {
$table->text('allowed_ips')->nullable()->change();
});
}
};
Loading