From 5087906b24eb3d4ac1fdb7204c2163198dacf38e Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:35:44 +0100 Subject: [PATCH] fix: encrypt resend API key in instance settings --- ...pt_resend_api_key_in_instance_settings.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 database/migrations/2024_12_13_103007_encrypt_resend_api_key_in_instance_settings.php diff --git a/database/migrations/2024_12_13_103007_encrypt_resend_api_key_in_instance_settings.php b/database/migrations/2024_12_13_103007_encrypt_resend_api_key_in_instance_settings.php new file mode 100644 index 000000000..33e18e25a --- /dev/null +++ b/database/migrations/2024_12_13_103007_encrypt_resend_api_key_in_instance_settings.php @@ -0,0 +1,56 @@ +exists()) { + Schema::table('instance_settings', function (Blueprint $table) { + $table->text('resend_api_key')->nullable()->change(); + }); + + $settings = DB::table('instance_settings')->get(); + foreach ($settings as $setting) { + try { + DB::table('instance_settings')->where('id', $setting->id)->update([ + 'resend_api_key' => $setting->resend_api_key ? Crypt::encryptString($setting->resend_api_key) : null, + ]); + } catch (Exception $e) { + \Log::error('Error encrypting resend_api_key: '.$e->getMessage()); + } + } + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('instance_settings', function (Blueprint $table) { + $table->text('resend_api_key')->nullable()->change(); + }); + + if (DB::table('instance_settings')->exists()) { + $settings = DB::table('instance_settings')->get(); + foreach ($settings as $setting) { + try { + DB::table('instance_settings')->where('id', $setting->id)->update([ + 'resend_api_key' => $setting->resend_api_key ? Crypt::decryptString($setting->resend_api_key) : null, + ]); + } catch (Exception $e) { + \Log::error('Error decrypting resend_api_key: '.$e->getMessage()); + } + } + } + } +};