From 5123ab57c91b23b495a45e78676431f2f0ee8f0b Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 7 Mar 2024 14:40:52 +0000 Subject: [PATCH] Added console command to encrypt previously unencrypted fields Signed-off-by: snipe --- .../Commands/ToggleCustomfieldEncryption.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/Console/Commands/ToggleCustomfieldEncryption.php diff --git a/app/Console/Commands/ToggleCustomfieldEncryption.php b/app/Console/Commands/ToggleCustomfieldEncryption.php new file mode 100644 index 0000000000..54b1e704b9 --- /dev/null +++ b/app/Console/Commands/ToggleCustomfieldEncryption.php @@ -0,0 +1,71 @@ +argument('fieldname'); + + if ($field = CustomField::where('db_column', $fieldname)->first()) { + + // If the field is not encrypted, make it encrypted and encrypt the data in the assets table for the + // corresponding field. + if ($field->field_encrypted == 0) { + $assets = Asset::whereNotNull($field->db_column)->get(); + + foreach ($assets as $asset) { + $asset->{$field->db_column} = encrypt($asset->{$field->db_column}); + $asset->save(); + } + + $field->field_encrypted = 1; + $field->save(); + + // This field is already encrypted. Do nothing. + } else { + $this->error('The custom field ' . $fieldname.' is already encrypted. No action was taken.'); + } + + } else { + $this->error('No matching results for unencrypted custom fields with db_column name: ' . $fieldname.'. Please check the fieldname.'); + } + + } +}