From 093fa2da09912abe2ea128e38374c01bede99813 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Tue, 4 Jun 2024 13:41:29 +0200 Subject: [PATCH 1/3] change `allowed_ips` to non nullable --- .../ApiKeyResource/Pages/CreateApiKey.php | 3 +- app/Models/ApiKey.php | 11 +++++-- database/Factories/ApiKeyFactory.php | 2 +- ...4_make_allowed_ips_column_non_nullable.php | 33 +++++++++++++++++++ 4 files changed, 44 insertions(+), 5 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 64fd7c77c0..511e787316 100644 --- a/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php +++ b/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php @@ -72,8 +72,7 @@ public function form(Form $form): Form ->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), + ->hidden(), Forms\Components\Textarea::make('memo') ->required() diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 21c044c548..0bee860f74 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -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 @@ -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 3361433b47..1b258767b7 100644 --- a/database/Factories/ApiKeyFactory.php +++ b/database/Factories/ApiKeyFactory.php @@ -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(), 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 0000000000..907981734f --- /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(); + }); + } +}; From 148f43937096d42c3e73a08d4f17fc242705b6df Mon Sep 17 00:00:00 2001 From: Boy132 Date: Tue, 11 Jun 2024 21:42:13 +0200 Subject: [PATCH 2/3] fix default value --- app/Models/ApiKey.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 0bee860f74..52d78a97bb 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -117,7 +117,7 @@ class ApiKey extends Model * Default attributes when creating a new model. */ protected $attributes = [ - 'allowed_ips' => [], + 'allowed_ips' => '[]', ]; /** From c578359962d8235f853022a412443b393b9a404a Mon Sep 17 00:00:00 2001 From: Boy132 Date: Tue, 11 Jun 2024 21:57:41 +0200 Subject: [PATCH 3/3] show "allowed_ips" input --- app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php b/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php index 511e787316..a864660dd2 100644 --- a/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php +++ b/app/Filament/Resources/ApiKeyResource/Pages/CreateApiKey.php @@ -71,8 +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(), + ->columnSpanFull(), Forms\Components\Textarea::make('memo') ->required()