Files
firefly-iii/app/Console/Commands/UseEncryption.php

95 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2017-08-18 21:08:51 +02:00
/**
* UseEncryption.php
2018-05-11 10:08:34 +02:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
2017-08-18 21:08:51 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-08-18 21:08:51 +02:00
*/
2017-08-12 10:27:45 +02:00
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
namespace FireflyIII\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
2017-08-11 05:36:05 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class UseEncryption.
* @codeCoverageIgnore
2017-08-11 05:36:05 +02:00
*/
class UseEncryption extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'This command will make sure that entries in the database will be encrypted (or not) according to the settings in .env';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly:use-encryption';
/**
* Execute the console command.
*/
2018-07-22 10:05:06 +02:00
public function handle(): int
{
2018-07-05 21:18:53 +02:00
if (true === config('firefly.encryption')) {
2017-12-29 07:25:30 +01:00
$this->info('Firefly III configuration calls for encrypted data.');
}
2018-07-05 21:18:53 +02:00
if (false === config('firefly.encryption')) {
2017-12-29 07:25:30 +01:00
$this->info('Firefly III configuration calls for unencrypted data.');
}
$this->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');
2018-08-06 19:14:30 +02:00
2018-07-22 10:05:06 +02:00
return 0;
}
/**
2017-08-15 17:26:43 +02:00
* Run each object and encrypt them (or not).
*
* @param string $class
* @param string $field
* @param string $indicator
*/
2018-07-05 21:18:53 +02:00
public function handleObjects(string $class, string $field, string $indicator): void
{
$fqn = sprintf('FireflyIII\Models\%s', $class);
2018-07-05 21:18:53 +02:00
$encrypt = true === config('firefly.encryption') ? 0 : 1;
/** @noinspection PhpUndefinedMethodInspection */
$set = $fqn::where($indicator, $encrypt)->get();
foreach ($set as $entry) {
$newName = $entry->$field;
$entry->$field = $newName;
2018-07-05 21:18:53 +02:00
/** @noinspection PhpUndefinedMethodInspection */
$entry->save();
}
2018-07-05 21:18:53 +02:00
/** @noinspection PhpUndefinedMethodInspection */
$this->line(sprintf('Updated %d %s.', $set->count(), strtolower(Str::plural($class))));
}
}