From 0c0b46852526658946629d46ef9a7e786bb619cc Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 13 Jun 2024 08:21:56 +0200 Subject: [PATCH] Change `allowed_ips` to non-nullable (#373) * change `allowed_ips` to non nullable * fix default value * show "allowed_ips" input --- .../ApiKeyResource/Pages/CreateApiKey.php | 4 +-- app/Models/ApiKey.php | 11 +++++-- database/Factories/ApiKeyFactory.php | 2 +- ...4_make_allowed_ips_column_non_nullable.php | 33 +++++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2024_06_04_133434_make_allowed_ips_column_non_nullable.php diff --git a/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php b/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php index 64fd7c77c..a864660dd 100644 --- a/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php +++ b/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php @@ -71,9 +71,7 @@ class CreateApiKey extends CreateRecord ->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() diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 21c044c54..52d78a97b 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -15,7 +15,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @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 @@ -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. @@ -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', diff --git a/database/Factories/ApiKeyFactory.php b/database/Factories/ApiKeyFactory.php index 3361433b4..1b258767b 100644 --- a/database/Factories/ApiKeyFactory.php +++ b/database/Factories/ApiKeyFactory.php @@ -27,7 +27,7 @@ class ApiKeyFactory extends Factory '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(), diff --git a/database/migrations/2024_06_04_133434_make_allowed_ips_column_non_nullable.php b/database/migrations/2024_06_04_133434_make_allowed_ips_column_non_nullable.php new file mode 100644 index 000000000..907981734 --- /dev/null +++ b/database/migrations/2024_06_04_133434_make_allowed_ips_column_non_nullable.php @@ -0,0 +1,33 @@ +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(); + }); + } +};