From f05002c729c814d2bb003023292e90fb40feda71 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 23 May 2015 08:46:46 +0200 Subject: [PATCH] Updated models for encryption. --- app/Models/Account.php | 4 +- app/Models/Bill.php | 4 +- app/Models/BudgetLimit.php | 28 ++++++++++ app/Models/LimitRepetition.php | 28 ++++++++++ app/Models/PiggyBank.php | 53 ++++++++++++++----- app/Models/PiggyBankEvent.php | 28 ++++++++++ app/Models/PiggyBankRepetition.php | 28 ++++++++++ app/Models/Preference.php | 35 +++++++++++- app/Models/Transaction.php | 34 ++++++++++-- .../2015_05_22_172026_changes_for_v3409.php | 4 +- 10 files changed, 221 insertions(+), 25 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 0688607cd3..78e24f9ba6 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -145,9 +145,7 @@ class Account extends Model return Crypt::decrypt($value); } - // @codeCoverageIgnoreStart return $value; - // @codeCoverageIgnoreEnd } @@ -228,7 +226,7 @@ class Account extends Model // save in cents: $value = intval($value * 100); $this->attributes['virtual_balance_encrypted'] = Crypt::encrypt($value); - $this->attributes['virtual_balance'] = 0; + $this->attributes['virtual_balance'] = ($value / 100); } /** diff --git a/app/Models/Bill.php b/app/Models/Bill.php index 8d7d65f274..2725ad9888 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -96,7 +96,7 @@ class Bill extends Model // save in cents: $value = intval($value * 100); $this->attributes['amount_max_encrypted'] = Crypt::encrypt($value); - $this->attributes['amount_max'] = 0; + $this->attributes['amount_max'] = ($value / 100); } /** @@ -107,7 +107,7 @@ class Bill extends Model // save in cents: $value = intval($value * 100); $this->attributes['amount_min_encrypted'] = Crypt::encrypt($value); - $this->attributes['amount_min'] = 0; + $this->attributes['amount_min'] = ($value / 100); } /** diff --git a/app/Models/BudgetLimit.php b/app/Models/BudgetLimit.php index 25bf48a4b4..739330aa06 100644 --- a/app/Models/BudgetLimit.php +++ b/app/Models/BudgetLimit.php @@ -1,5 +1,6 @@ belongsTo('FireflyIII\Models\Budget'); } + /** + * @param $value + * + * @return float|int + */ + public function getAmountAttribute($value) + { + if (is_null($this->amount_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->amount_encrypted)); + $value = $value / 100; + + return $value; + } + /** * @return array */ @@ -35,4 +52,15 @@ class BudgetLimit extends Model return $this->hasMany('FireflyIII\Models\LimitRepetition'); } + /** + * @param $value + */ + public function setAmountAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['amount_encrypted'] = Crypt::encrypt($value); + $this->attributes['amount'] = ($value / 100); + } + } diff --git a/app/Models/LimitRepetition.php b/app/Models/LimitRepetition.php index 3faef2b46d..c4066352d5 100644 --- a/app/Models/LimitRepetition.php +++ b/app/Models/LimitRepetition.php @@ -1,6 +1,7 @@ amount_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->amount_encrypted)); + $value = $value / 100; + + return $value; + } + + /** + * @param $value + */ + public function setAmountAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['amount_encrypted'] = Crypt::encrypt($value); + $this->attributes['amount'] = ($value / 100); + } + } diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php index f4486b99c3..2d7279ad1d 100644 --- a/app/Models/PiggyBank.php +++ b/app/Models/PiggyBank.php @@ -62,6 +62,25 @@ class PiggyBank extends Model return ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate']; } + /** + * @codeCoverageIgnore + * + * @param $value + * + * @return string + */ + public function getNameAttribute($value) + { + + if (intval($this->encrypted) == 1) { + return Crypt::decrypt($value); + } + + // @codeCoverageIgnoreStart + return $value; + // @codeCoverageIgnoreEnd + } + /** * @codeCoverageIgnore * @@ -74,6 +93,22 @@ class PiggyBank extends Model return intval($value) == 1; } + /** + * @param $value + * + * @return float|int + */ + public function getTargetamountAttribute($value) + { + if (is_null($this->targetamount_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->targetamount_encrypted)); + $value = $value / 100; + + return $value; + } + /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\HasMany @@ -104,21 +139,13 @@ class PiggyBank extends Model } /** - * @codeCoverageIgnore - * * @param $value - * - * @return string */ - public function getNameAttribute($value) + public function setTargetamountAttribute($value) { - - if (intval($this->encrypted) == 1) { - return Crypt::decrypt($value); - } - - // @codeCoverageIgnoreStart - return $value; - // @codeCoverageIgnoreEnd + // save in cents: + $value = intval($value * 100); + $this->attributes['targetamount_encrypted'] = Crypt::encrypt($value); + $this->attributes['targetamount'] = ($value / 100); } } diff --git a/app/Models/PiggyBankEvent.php b/app/Models/PiggyBankEvent.php index 4a54c0b3f2..ad9ab0bb52 100644 --- a/app/Models/PiggyBankEvent.php +++ b/app/Models/PiggyBankEvent.php @@ -1,5 +1,6 @@ amount_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->amount_encrypted)); + $value = $value / 100; + + return $value; + } + /** * @return array */ @@ -29,6 +46,17 @@ class PiggyBankEvent extends Model return $this->belongsTo('FireflyIII\Models\PiggyBank'); } + /** + * @param $value + */ + public function setAmountAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['amount_encrypted'] = Crypt::encrypt($value); + $this->attributes['amount'] = ($value / 100); + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Models/PiggyBankRepetition.php b/app/Models/PiggyBankRepetition.php index 12019da640..1421f9219f 100644 --- a/app/Models/PiggyBankRepetition.php +++ b/app/Models/PiggyBankRepetition.php @@ -1,6 +1,7 @@ currentamount_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->currentamount_encrypted)); + $value = $value / 100; + + return $value; + } + + /** + * @param $value + */ + public function setCurrentamountAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['currentamount_encrypted'] = Crypt::encrypt($value); + $this->attributes['currentamount'] = ($value / 100); + } + } diff --git a/app/Models/Preference.php b/app/Models/Preference.php index 86c5dd9f40..cb91740bcf 100644 --- a/app/Models/Preference.php +++ b/app/Models/Preference.php @@ -1,5 +1,6 @@ data_encrypted)) { + return json_decode($value); + } + $data = Crypt::decrypt($this->data_encrypted); + + return json_decode($data); } /** @@ -31,12 +37,37 @@ class Preference extends Model return ['created_at', 'updated_at']; } + /** + * @param $value + * + * @return float|int + */ + public function getNameAttribute($value) + { + if (is_null($this->name_encrypted)) { + return $value; + } + $value = Crypt::decrypt($this->name_encrypted); + + return $value; + } + /** * @param $value */ public function setDataAttribute($value) { - $this->attributes['data'] = json_encode($value); + $this->attributes['data'] = '';//json_encode($value); + $this->attributes['data_encrypted'] = Crypt::encrypt(json_encode($value)); + } + + /** + * @param $value + */ + public function setNameAttribute($value) + { + $this->attributes['name_encrypted'] = Crypt::encrypt($value); + $this->attributes['name'] = $value; } /** diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 447c3085e0..dea16ca928 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -1,6 +1,7 @@ belongsTo('FireflyIII\Models\Account'); } + /** + * @param $value + * + * @return float|int + */ + public function getAmountAttribute($value) + { + if (is_null($this->amount_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->amount_encrypted)); + $value = $value / 100; + + return $value; + } + + /** + * @return array + */ + public function getDates() + { + return ['created_at', 'updated_at', 'deleted_at']; + } + /** * @param EloquentBuilder $query * @param Carbon $date @@ -56,11 +81,14 @@ class Transaction extends Model } /** - * @return array + * @param $value */ - public function getDates() + public function setAmountAttribute($value) { - return ['created_at', 'updated_at', 'deleted_at']; + // save in cents: + $value = intval($value * 100); + $this->attributes['amount_encrypted'] = Crypt::encrypt($value); + $this->attributes['amount'] = ($value / 100); } /** diff --git a/database/migrations/2015_05_22_172026_changes_for_v3409.php b/database/migrations/2015_05_22_172026_changes_for_v3409.php index 462b6d5af0..3f53f00441 100644 --- a/database/migrations/2015_05_22_172026_changes_for_v3409.php +++ b/database/migrations/2015_05_22_172026_changes_for_v3409.php @@ -131,8 +131,8 @@ class ChangesForV3409 extends Migration // encrypt preference data (add field) Schema::table( 'preferences', function (Blueprint $table) { - $table->smallInteger('name_encrypted', false, true)->default(0)->after('name'); - $table->smallInteger('data_encrypted', false, true)->default(0)->after('data'); + $table->text('name_encrypted')->nullable()->after('name'); + $table->text('data_encrypted')->nullable()->after('data'); } );