From 5bbaaece3839d189994cd051702dc379922af592 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 14 Jan 2017 17:13:57 +0100 Subject: [PATCH] Encryption is optional (but on by default) and a command to switch from one to the other --- .env.example | 1 + app/Console/Commands/UseEncryption.php | 66 ++++++++++++++++++++++++++ app/Console/Kernel.php | 3 +- app/Models/Account.php | 8 ++-- app/Models/Bill.php | 14 +++--- app/Models/Budget.php | 5 +- app/Models/Category.php | 5 +- app/Models/PiggyBank.php | 5 +- app/Models/TransactionJournal.php | 5 +- config/firefly.php | 1 + 10 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 app/Console/Commands/UseEncryption.php diff --git a/.env.example b/.env.example index 87540a4f1c..04528da7fc 100755 --- a/.env.example +++ b/.env.example @@ -43,6 +43,7 @@ CACHE_PREFIX=firefly GOOGLE_MAPS_API_KEY= ANALYTICS_ID= SITE_OWNER=mail@example.com +USE_ENCRYPTION=true PUSHER_KEY= PUSHER_SECRET= diff --git a/app/Console/Commands/UseEncryption.php b/app/Console/Commands/UseEncryption.php new file mode 100644 index 0000000000..d3c3e178c0 --- /dev/null +++ b/app/Console/Commands/UseEncryption.php @@ -0,0 +1,66 @@ +handleObjects('Account', 'name', 'encrypted'); + $this->handleObjects('Bill', 'name', 'name_encrypted'); + $this->handleObjects('Bill', 'match', 'match_encrypted'); + $this->handleObjects('Budget', 'name', 'encrypted'); + $this->handleObjects('Category', 'name', 'encrypted'); + $this->handleObjects('PiggyBank', 'name', 'encrypted'); + $this->handleObjects('TransactionJournal', 'description', 'encrypted'); + } + + /** + * @param string $class + * @param string $field + * @param string $indicator + */ + public function handleObjects(string $class, string $field, string $indicator) + { + $fqn = sprintf('FireflyIII\Models\%s', $class); + $encrypt = config('firefly.encryption') ? 0 : 1; + $set = $fqn::where($indicator, $encrypt)->get(); + + foreach ($set as $entry) { + $newName = $entry->$field; + $entry->$field = $newName; + $entry->save(); + } + + $this->line(sprintf('Updated %d %s.', $set->count(), strtolower(Str::plural($class)))); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 61537ce4e8..8ea0f88014 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -16,10 +16,10 @@ namespace FireflyIII\Console; use FireflyIII\Console\Commands\CreateImport; use FireflyIII\Console\Commands\EncryptFile; use FireflyIII\Console\Commands\Import; -use FireflyIII\Console\Commands\MoveRepository; use FireflyIII\Console\Commands\ScanAttachments; use FireflyIII\Console\Commands\UpgradeDatabase; use FireflyIII\Console\Commands\UpgradeFireflyInstructions; +use FireflyIII\Console\Commands\UseEncryption; use FireflyIII\Console\Commands\VerifyDatabase; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -63,6 +63,7 @@ class Kernel extends ConsoleKernel EncryptFile::class, ScanAttachments::class, UpgradeDatabase::class, + UseEncryption::class, ]; /** diff --git a/app/Models/Account.php b/app/Models/Account.php index 4810c6006e..11b337ba10 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -85,7 +85,7 @@ class Account extends Model foreach ($search as $name => $value) { $query->where($name, $value); } - $set = $query->get(['accounts.*']); + $set = $query->get(['accounts.*']); // account must have a name. If not set, use IBAN. if (!isset($fields['name'])) { @@ -93,7 +93,6 @@ class Account extends Model } - /** @var Account $account */ foreach ($set as $account) { if ($account->name == $fields['name']) { @@ -316,8 +315,9 @@ class Account extends Model */ public function setNameAttribute($value) { - $this->attributes['name'] = $value; - $this->attributes['encrypted'] = false; + $encrypt = config('firefly.encryption'); + $this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['encrypted'] = $encrypt; } /** diff --git a/app/Models/Bill.php b/app/Models/Bill.php index bf67e0f730..54a7429746 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -35,7 +35,7 @@ class Bill extends Model * @var array */ protected $casts - = [ + = [ 'created_at' => 'date', 'updated_at' => 'date', 'deleted_at' => 'date', @@ -47,7 +47,7 @@ class Bill extends Model 'match_encrypted' => 'boolean', ]; /** @var array */ - protected $dates = ['created_at', 'updated_at', 'deleted_at']; + protected $dates = ['created_at', 'updated_at', 'deleted_at']; protected $fillable = ['name', 'match', 'amount_min', 'match_encrypted', 'name_encrypted', 'user_id', 'amount_max', 'date', 'repeat_freq', 'skip', 'automatch', 'active',]; @@ -120,8 +120,9 @@ class Bill extends Model */ public function setMatchAttribute($value) { - $this->attributes['match'] = Crypt::encrypt($value); - $this->attributes['match_encrypted'] = true; + $encrypt = config('firefly.encryption'); + $this->attributes['match'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['match_encrypted'] = $encrypt; } /** @@ -129,8 +130,9 @@ class Bill extends Model */ public function setNameAttribute($value) { - $this->attributes['name'] = Crypt::encrypt($value); - $this->attributes['name_encrypted'] = true; + $encrypt = config('firefly.encryption'); + $this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['name_encrypted'] = $encrypt; } /** diff --git a/app/Models/Budget.php b/app/Models/Budget.php index d62e195352..504802e05a 100644 --- a/app/Models/Budget.php +++ b/app/Models/Budget.php @@ -121,8 +121,9 @@ class Budget extends Model */ public function setNameAttribute($value) { - $this->attributes['name'] = $value; - $this->attributes['encrypted'] = false; + $encrypt = config('firefly.encryption'); + $this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['encrypted'] = $encrypt; } /** diff --git a/app/Models/Category.php b/app/Models/Category.php index e8e665bff2..50349e9b44 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -115,8 +115,9 @@ class Category extends Model */ public function setNameAttribute($value) { - $this->attributes['name'] = $value; - $this->attributes['encrypted'] = false; + $encrypt = config('firefly.encryption'); + $this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['encrypted'] = $encrypt; } /** diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php index 34c742bd7f..d5b2b00187 100644 --- a/app/Models/PiggyBank.php +++ b/app/Models/PiggyBank.php @@ -159,8 +159,9 @@ class PiggyBank extends Model */ public function setNameAttribute($value) { - $this->attributes['name'] = $value; - $this->attributes['encrypted'] = false; + $encrypt = config('firefly.encryption'); + $this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['encrypted'] = $encrypt; } /** diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 1cf5faf17f..1808955271 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -371,8 +371,9 @@ class TransactionJournal extends TransactionJournalSupport */ public function setDescriptionAttribute($value) { - $this->attributes['description'] = $value; - $this->attributes['encrypted'] = false; + $encrypt = config('firefly.encryption'); + $this->attributes['description'] = $encrypt ? Crypt::encrypt($value) : $value; + $this->attributes['encrypted'] = $encrypt; } /** diff --git a/config/firefly.php b/config/firefly.php index 73ae9563e8..67f9ccdd15 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -22,6 +22,7 @@ return [ 'single_user_mode' => true, 'is_demo_site' => false, ], + 'encryption' => (is_null(env('USE_ENCRYPTION')) || env('USE_ENCRYPTION') === true), 'chart' => 'chartjs', 'version' => '4.3.2', 'csv_import_enabled' => true,