From 1c2cbd5b40fe7763d920f86755bedbd83410e64e Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 23 May 2015 07:47:36 +0200 Subject: [PATCH] New stuff for encrypted amounts. --- app/Helpers/Report/ReportQuery.php | 8 +- app/Http/Middleware/Cleanup.php | 3 - app/Models/Account.php | 28 ++++ app/Models/Bill.php | 54 +++++++ .../2015_05_22_172026_changes_for_v3409.php | 148 ++++++++++++++++++ database/seeds/TestDataSeeder.php | 46 +++--- tests/helpers/ReportQueryTest.php | 100 ------------ 7 files changed, 256 insertions(+), 131 deletions(-) create mode 100644 database/migrations/2015_05_22_172026_changes_for_v3409.php delete mode 100644 tests/helpers/ReportQueryTest.php diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php index 0dcaddb3f7..990f6aef30 100644 --- a/app/Helpers/Report/ReportQuery.php +++ b/app/Helpers/Report/ReportQuery.php @@ -74,9 +74,7 @@ class ReportQuery implements ReportQueryInterface if ($journal->amount != 0) { return $journal; } - // @codeCoverageIgnoreStart - } - // @codeCoverageIgnoreEnd + } // @codeCoverageIgnore ); return $data; @@ -193,9 +191,7 @@ class ReportQuery implements ReportQueryInterface if ($journal->amount != 0) { return $journal; } - // @codeCoverageIgnoreStart - } - // @codeCoverageIgnoreEnd + } // @codeCoverageIgnore ); return $data; diff --git a/app/Http/Middleware/Cleanup.php b/app/Http/Middleware/Cleanup.php index c7e0f01d52..d818e8eac8 100644 --- a/app/Http/Middleware/Cleanup.php +++ b/app/Http/Middleware/Cleanup.php @@ -149,15 +149,12 @@ class Cleanup unset($set, $entry, $metadata); //encrypt budget limit amount - //encrypt limit repetition amount //encrypt piggy bank event amount //encrypt piggy bank repetition currentamount //encrypt piggy bank targetamount - //encrypt preference name (add field) //encrypt preference data (add field) - //encrypt transaction amount } if ($count == 0 && $run) { diff --git a/app/Models/Account.php b/app/Models/Account.php index d9733d77fd..0688607cd3 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -150,6 +150,23 @@ class Account extends Model // @codeCoverageIgnoreEnd } + + /** + * @param $value + * + * @return float|int + */ + public function getVirtualBalanceAttribute($value) + { + if (is_null($this->virtual_balance_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->virtual_balance_encrypted)); + $value = $value / 100; + + return $value; + } + /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\HasMany @@ -203,6 +220,17 @@ class Account extends Model $this->attributes['encrypted'] = true; } + /** + * @param $value + */ + public function setVirtualBalanceAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['virtual_balance_encrypted'] = Crypt::encrypt($value); + $this->attributes['virtual_balance'] = 0; + } + /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\HasMany diff --git a/app/Models/Bill.php b/app/Models/Bill.php index d181e616c3..8d7d65f274 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -14,6 +14,38 @@ class Bill extends Model protected $fillable = ['name', 'match', 'amount_min', 'match_encrypted', 'name_encrypted', 'user_id', 'amount_max', 'date', 'repeat_freq', 'skip', 'automatch', 'active',]; + /** + * @param $value + * + * @return float|int + */ + public function getAmountMaxAttribute($value) + { + if (is_null($this->amount_max_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->amount_max_encrypted)); + $value = $value / 100; + + return $value; + } + + /** + * @param $value + * + * @return float|int + */ + public function getAmountMinAttribute($value) + { + if (is_null($this->amount_min_encrypted)) { + return $value; + } + $value = intval(Crypt::decrypt($this->amount_min_encrypted)); + $value = $value / 100; + + return $value; + } + /** * @return array */ @@ -56,6 +88,28 @@ class Bill extends Model // @codeCoverageIgnoreEnd } + /** + * @param $value + */ + public function setAmountMaxAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['amount_max_encrypted'] = Crypt::encrypt($value); + $this->attributes['amount_max'] = 0; + } + + /** + * @param $value + */ + public function setAmountMinAttribute($value) + { + // save in cents: + $value = intval($value * 100); + $this->attributes['amount_min_encrypted'] = Crypt::encrypt($value); + $this->attributes['amount_min'] = 0; + } + /** * @param $value */ diff --git a/database/migrations/2015_05_22_172026_changes_for_v3409.php b/database/migrations/2015_05_22_172026_changes_for_v3409.php new file mode 100644 index 0000000000..462b6d5af0 --- /dev/null +++ b/database/migrations/2015_05_22_172026_changes_for_v3409.php @@ -0,0 +1,148 @@ +dropColumn('virtual_balance_encrypted'); + } + ); + + Schema::table( + 'bills', function (Blueprint $table) { + $table->dropColumn('amount_min_encrypted'); + $table->dropColumn('amount_max_encrypted'); + } + ); + + Schema::table( + 'budget_limits', function (Blueprint $table) { + + $table->dropColumn('amount_encrypted'); + } + ); + Schema::table( + 'limit_repetitions', function (Blueprint $table) { + $table->dropColumn('amount_encrypted'); + } + ); + Schema::table( + 'piggy_bank_events', function (Blueprint $table) { + $table->dropColumn('amount_encrypted'); + } + ); + Schema::table( + 'piggy_bank_repetitions', function (Blueprint $table) { + $table->dropColumn('currentamount_encrypted'); + } + ); + + Schema::table( + 'piggy_banks', function (Blueprint $table) { + $table->dropColumn('targetamount_encrypted'); + } + ); + Schema::table( + 'preferences', function (Blueprint $table) { + $table->dropColumn('name_encrypted'); + $table->dropColumn('data_encrypted'); + } + ); + + Schema::table( + 'transactions', function (Blueprint $table) { + $table->dropColumn('amount_encrypted'); + } + ); + } + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + // encrypt account virtual balance: + Schema::table( + 'accounts', function (Blueprint $table) { + $table->string('virtual_balance_encrypted')->nullable()->after('virtual_balance'); + } + ); + + // encrypt bill amount_min and amount_max: + Schema::table( + 'bills', function (Blueprint $table) { + $table->string('amount_min_encrypted')->nullable()->after('amount_min'); + $table->string('amount_max_encrypted')->nullable()->after('amount_max'); + } + ); + + // encrypt budget limit amount + Schema::table( + 'budget_limits', function (Blueprint $table) { + $table->string('amount_encrypted')->nullable()->after('amount'); + } + ); + // encrypt limit repetition amount + Schema::table( + 'limit_repetitions', function (Blueprint $table) { + $table->string('amount_encrypted')->nullable()->after('amount'); + } + ); + // encrypt piggy bank event amount + Schema::table( + 'piggy_bank_events', function (Blueprint $table) { + $table->string('amount_encrypted')->nullable()->after('amount'); + } + ); + // encrypt piggy bank repetition currentamount + Schema::table( + 'piggy_bank_repetitions', function (Blueprint $table) { + $table->string('currentamount_encrypted')->nullable()->after('currentamount'); + } + ); + + // encrypt piggy bank targetamount + Schema::table( + 'piggy_banks', function (Blueprint $table) { + $table->string('targetamount_encrypted')->nullable()->after('targetamount'); + } + ); + // encrypt preference name (add field) + // 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'); + } + ); + + // encrypt transaction amount + Schema::table( + 'transactions', function (Blueprint $table) { + $table->string('amount_encrypted')->nullable()->after('amount'); + } + ); + + } + +} diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php index beb5a03eef..c729c8c2da 100644 --- a/database/seeds/TestDataSeeder.php +++ b/database/seeds/TestDataSeeder.php @@ -134,7 +134,9 @@ class TestDataSeeder extends Seeder $acc_a = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Checking account', 'active' => 1]); $acc_b = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Savings account', 'active' => 1]); - $acc_c = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Delete me', 'active' => 1]); + $acc_c = Account::create( + ['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Delete me', 'active' => 1, 'virtual_balance' => 123.45] + ); // create account meta: AccountMeta::create(['account_id' => $acc_a->id, 'name' => 'accountRole', 'data' => 'defaultAsset']); @@ -514,27 +516,6 @@ class TestDataSeeder extends Seeder return null; } - /** - * @param $name - * - * @return PiggyBank|null - */ - protected function findPiggyBank($name) - { - // account - $user = User::whereEmail('thegrumpydictator@gmail.com')->first(); - /** @var Budget $budget */ - foreach (PiggyBank::get() as $piggyBank) { - $account = $piggyBank->account()->first(); - if ($piggyBank->name == $name && $user->id == $account->user_id) { - return $piggyBank; - break; - } - } - - return null; - } - /** * @param $name * @@ -680,5 +661,26 @@ class TestDataSeeder extends Seeder ); } + /** + * @param $name + * + * @return PiggyBank|null + */ + protected function findPiggyBank($name) + { + // account + $user = User::whereEmail('thegrumpydictator@gmail.com')->first(); + /** @var Budget $budget */ + foreach (PiggyBank::get() as $piggyBank) { + $account = $piggyBank->account()->first(); + if ($piggyBank->name == $name && $user->id == $account->user_id) { + return $piggyBank; + break; + } + } + + return null; + } + } diff --git a/tests/helpers/ReportQueryTest.php b/tests/helpers/ReportQueryTest.php deleted file mode 100644 index d4a769b922..0000000000 --- a/tests/helpers/ReportQueryTest.php +++ /dev/null @@ -1,100 +0,0 @@ -object = new ReportQuery; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - public function tearDown() - { - parent::tearDown(); - } - - public function testBalancedTransactionsList() - { - $this->markTestIncomplete(); - } - - public function testBalancedTransactionsSum() - { - $this->markTestIncomplete(); - } - - public function testGetAllAccounts() - { - $this->markTestIncomplete(); - } - - public function testGetBudgetSummary() - { - $this->markTestIncomplete(); - } - - public function testGetTransactionsWithoutBudget() - { - $this->markTestIncomplete(); - } - - public function testIncomeInPeriod() - { - $this->markTestIncomplete(); - } - - public function testJournalsByBudget() - { - $this->markTestIncomplete(); - } - - public function testJournalsByCategory() - { - $this->markTestIncomplete(); - } - - public function testJournalsByExpenseAccount() - { - $this->markTestIncomplete(); - } - - public function testJournalsByRevenueAccount() - { - $this->markTestIncomplete(); - } - - public function testSharedExpenses() - { - $this->markTestIncomplete(); - } - - public function testSharedExpensesByCategory() - { - $this->markTestIncomplete(); - } - -}