Skip to content

Firefly III: a personal finances manager

Updated models for encryption.

James Colef05002c729c8
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 @@
<?php namespace FireflyIII\Models;
+use Crypt;
use Illuminate\Database\Eloquent\Model;
/**
@@ -19,6 +20,22 @@ class BudgetLimit extends Model
return $this->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 @@
<?php namespace FireflyIII\Models;
use Auth;
+use Crypt;
use DB;
use Illuminate\Database\Eloquent\Model;
@@ -51,4 +52,31 @@ class LimitRepetition extends Model
return floatval($sum);
}
+ /**
+ * @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;
+ }
+
+ /**
+ * @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 @@
<?php namespace FireflyIII\Models;
+use Crypt;
use Illuminate\Database\Eloquent\Model;
/**
@@ -13,6 +14,22 @@ class PiggyBankEvent extends Model
protected $fillable = ['piggy_bank_id', 'transaction_journal_id', 'date', 'amount'];
+ /**
+ * @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
*/
@@ -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 @@
<?php namespace FireflyIII\Models;
use Carbon\Carbon;
+use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
@@ -66,4 +67,31 @@ class PiggyBankRepetition extends Model
);
}
+ /**
+ * @param $value
+ *
+ * @return float|int
+ */
+ public function getCurrentamountAttribute($value)
+ {
+ if (is_null($this->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 @@
<?php namespace FireflyIII\Models;
+use Crypt;
use Illuminate\Database\Eloquent\Model;
/**
@@ -20,7 +21,12 @@ class Preference extends Model
*/
public function getDataAttribute($value)
{
- return json_decode($value);
+ if (is_null($this->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 @@
<?php namespace FireflyIII\Models;
use Carbon\Carbon;
+use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -33,6 +34,30 @@ class Transaction extends Model
return $this->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');
}
);