This commit is contained in:
James Cole
2019-09-21 11:03:00 +02:00
parent 7067e36421
commit 9ea1a495b8
8 changed files with 157 additions and 3 deletions

View File

@@ -71,7 +71,8 @@ class CorrectDatabase extends Command
'firefly-iii:fix-account-types',
'firefly-iii:rename-meta-fields',
'firefly-iii:fix-ob-currencies',
'firefly-iii:fix-long-descriptions'
'firefly-iii:fix-long-descriptions',
'firefly-iii:restore-oauth-keys'
];
foreach ($commands as $command) {
$this->line(sprintf('Now executing %s', $command));

View File

@@ -51,6 +51,7 @@ class FixLongDescriptions extends Command
*/
public function handle(): int
{
$start = microtime(true);
$journals = TransactionJournal::get(['id', 'description']);
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
@@ -70,7 +71,8 @@ class FixLongDescriptions extends Command
$this->line(sprintf('Truncated description of transaction group #%d', $group->id));
}
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified all transaction group and journal title lengths in %s seconds.', $end));
return 0;
}
}

View File

@@ -0,0 +1,137 @@
<?php
/**
* RestoreOAuthKeys.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Console\Commands\Integrity;
use Artisan;
use Crypt;
use Illuminate\Console\Command;
/**
* Class RestoreOAuthKeys
*/
class RestoreOAuthKeys extends Command
{
private const PRIVATE_KEY = 'oauth_private_key';
private const PUBLIC_KEY = 'oauth_public_key';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Will restore the OAuth keys generated for the system.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:restore-oauth-keys';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->restoreOAuthKeys();
return 0;
}
/**
*
*/
private function generateKeys(): void
{
Artisan::call('passport:keys');
}
/**
* @return bool
*/
private function keysInDatabase(): bool
{
return app('fireflyconfig')->has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY);
}
/**
* @return bool
*/
private function keysOnDrive(): bool
{
$private = storage_path('oauth-private.key');
$public = storage_path('oauth-public.key');
return file_exists($private) && file_exists($public);
}
/**
*
*/
private function restoreKeysFromDB(): void
{
$privateContent = Crypt::decrypt(app('fireflyconfig')->get(self::PRIVATE_KEY)->data);
$publicContent = Crypt::decrypt(app('fireflyconfig')->get(self::PUBLIC_KEY)->data);
$private = storage_path('oauth-private.key');
$public = storage_path('oauth-public.key');
file_put_contents($private, $privateContent);
file_put_contents($public, $publicContent);
}
/**
*
*/
private function restoreOAuthKeys(): void
{
if (!$this->keysInDatabase() && !$this->keysOnDrive()) {
$this->generateKeys();
$this->storeKeysInDB();
$this->line('Generated and stored new keys.');
return;
}
if ($this->keysInDatabase() && !$this->keysOnDrive()) {
$this->restoreKeysFromDB();
$this->line('Restored OAuth keys from database.');
return;
}
if (!$this->keysInDatabase() && $this->keysOnDrive()) {
$this->storeKeysInDB();
$this->line('Stored OAuth keys in database.');
return;
}
$this->line('OAuth keys are OK');
}
/**
*
*/
private function storeKeysInDB(): void
{
$private = storage_path('oauth-private.key');
$public = storage_path('oauth-public.key');
app('fireflyconfig')->set(self::PRIVATE_KEY, Crypt::encrypt(file_get_contents($private)));
app('fireflyconfig')->set(self::PUBLIC_KEY, Crypt::encrypt(file_get_contents($public)));
}
}

View File

@@ -93,6 +93,7 @@ class UpgradeDatabase extends Command
// two report commands
'firefly-iii:report-empty-objects',
'firefly-iii:report-sum',
'firefly-iii:restore-oauth-keys',
// instructions
'firefly:instructions update',