Updated models for encryption.

This commit is contained in:
James Cole 2015-05-23 08:46:46 +02:00
parent 1c2cbd5b40
commit f05002c729
10 changed files with 221 additions and 25 deletions

View File

@ -145,9 +145,7 @@ class Account extends Model
return Crypt::decrypt($value); return Crypt::decrypt($value);
} }
// @codeCoverageIgnoreStart
return $value; return $value;
// @codeCoverageIgnoreEnd
} }
@ -228,7 +226,7 @@ class Account extends Model
// save in cents: // save in cents:
$value = intval($value * 100); $value = intval($value * 100);
$this->attributes['virtual_balance_encrypted'] = Crypt::encrypt($value); $this->attributes['virtual_balance_encrypted'] = Crypt::encrypt($value);
$this->attributes['virtual_balance'] = 0; $this->attributes['virtual_balance'] = ($value / 100);
} }
/** /**

View File

@ -96,7 +96,7 @@ class Bill extends Model
// save in cents: // save in cents:
$value = intval($value * 100); $value = intval($value * 100);
$this->attributes['amount_max_encrypted'] = Crypt::encrypt($value); $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: // save in cents:
$value = intval($value * 100); $value = intval($value * 100);
$this->attributes['amount_min_encrypted'] = Crypt::encrypt($value); $this->attributes['amount_min_encrypted'] = Crypt::encrypt($value);
$this->attributes['amount_min'] = 0; $this->attributes['amount_min'] = ($value / 100);
} }
/** /**

View File

@ -1,5 +1,6 @@
<?php namespace FireflyIII\Models; <?php namespace FireflyIII\Models;
use Crypt;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
@ -19,6 +20,22 @@ class BudgetLimit extends Model
return $this->belongsTo('FireflyIII\Models\Budget'); 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 * @return array
*/ */
@ -35,4 +52,15 @@ class BudgetLimit extends Model
return $this->hasMany('FireflyIII\Models\LimitRepetition'); 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);
}
} }

View File

@ -1,6 +1,7 @@
<?php namespace FireflyIII\Models; <?php namespace FireflyIII\Models;
use Auth; use Auth;
use Crypt;
use DB; use DB;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -51,4 +52,31 @@ class LimitRepetition extends Model
return floatval($sum); 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);
}
} }

View File

@ -62,6 +62,25 @@ class PiggyBank extends Model
return ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate']; 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 * @codeCoverageIgnore
* *
@ -74,6 +93,22 @@ class PiggyBank extends Model
return intval($value) == 1; 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 * @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
@ -104,21 +139,13 @@ class PiggyBank extends Model
} }
/** /**
* @codeCoverageIgnore
*
* @param $value * @param $value
*
* @return string
*/ */
public function getNameAttribute($value) public function setTargetamountAttribute($value)
{ {
// save in cents:
if (intval($this->encrypted) == 1) { $value = intval($value * 100);
return Crypt::decrypt($value); $this->attributes['targetamount_encrypted'] = Crypt::encrypt($value);
} $this->attributes['targetamount'] = ($value / 100);
// @codeCoverageIgnoreStart
return $value;
// @codeCoverageIgnoreEnd
} }
} }

View File

@ -1,5 +1,6 @@
<?php namespace FireflyIII\Models; <?php namespace FireflyIII\Models;
use Crypt;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
@ -13,6 +14,22 @@ class PiggyBankEvent extends Model
protected $fillable = ['piggy_bank_id', 'transaction_journal_id', 'date', 'amount']; 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 * @return array
*/ */
@ -29,6 +46,17 @@ class PiggyBankEvent extends Model
return $this->belongsTo('FireflyIII\Models\PiggyBank'); 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 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */

View File

@ -1,6 +1,7 @@
<?php namespace FireflyIII\Models; <?php namespace FireflyIII\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model; 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);
}
} }

View File

@ -1,5 +1,6 @@
<?php namespace FireflyIII\Models; <?php namespace FireflyIII\Models;
use Crypt;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
@ -20,7 +21,12 @@ class Preference extends Model
*/ */
public function getDataAttribute($value) 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']; 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 * @param $value
*/ */
public function setDataAttribute($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;
} }
/** /**

View File

@ -1,6 +1,7 @@
<?php namespace FireflyIII\Models; <?php namespace FireflyIII\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -33,6 +34,30 @@ class Transaction extends Model
return $this->belongsTo('FireflyIII\Models\Account'); 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 EloquentBuilder $query
* @param Carbon $date * @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);
} }
/** /**

View File

@ -131,8 +131,8 @@ class ChangesForV3409 extends Migration
// encrypt preference data (add field) // encrypt preference data (add field)
Schema::table( Schema::table(
'preferences', function (Blueprint $table) { 'preferences', function (Blueprint $table) {
$table->smallInteger('name_encrypted', false, true)->default(0)->after('name'); $table->text('name_encrypted')->nullable()->after('name');
$table->smallInteger('data_encrypted', false, true)->default(0)->after('data'); $table->text('data_encrypted')->nullable()->after('data');
} }
); );