Files
firefly-iii/app/Console/Commands/Integrity/RestoreOAuthKeys.php

132 lines
3.1 KiB
PHP
Raw Normal View History

2019-09-21 11:03:00 +02:00
<?php
2020-06-30 19:05:35 +02:00
2019-09-21 11:03:00 +02:00
/**
* RestoreOAuthKeys.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
2019-09-21 11:03:00 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-09-21 11:03:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-09-21 11:03:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-09-21 11:03:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-09-21 11:03:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-09-21 11:03:00 +02:00
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2019-09-21 11:03:00 +02:00
namespace FireflyIII\Console\Commands\Integrity;
use FireflyIII\Support\System\OAuthKeys;
2019-09-21 11:03:00 +02:00
use Illuminate\Console\Command;
2020-05-23 08:14:13 +02:00
use Log;
2019-09-21 11:03:00 +02:00
/**
* Class RestoreOAuthKeys
*/
class RestoreOAuthKeys extends Command
{
/**
* 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();
2020-03-21 15:43:41 +01:00
2019-09-21 11:03:00 +02:00
return 0;
}
/**
*
*/
private function generateKeys(): void
{
OAuthKeys::generateKeys();
2019-09-21 11:03:00 +02:00
}
/**
* @return bool
*/
private function keysInDatabase(): bool
{
return OAuthKeys::keysInDatabase();
2019-09-21 11:03:00 +02:00
}
/**
* @return bool
*/
private function keysOnDrive(): bool
{
return OAuthKeys::hasKeyFiles();
2019-09-21 11:03:00 +02:00
}
/**
*
*/
private function restoreKeysFromDB(): void
{
OAuthKeys::restoreKeysFromDB();
2019-09-21 11:03:00 +02:00
}
/**
*
*/
private function restoreOAuthKeys(): void
{
2020-05-23 08:14:13 +02:00
Log::debug('Going to restoreOAuthKeys()');
2019-09-21 11:03:00 +02:00
if (!$this->keysInDatabase() && !$this->keysOnDrive()) {
2020-05-23 08:14:13 +02:00
Log::debug('Keys are not in DB and keys are not on the drive.');
2019-09-21 11:03:00 +02:00
$this->generateKeys();
$this->storeKeysInDB();
$this->line('Generated and stored new keys.');
return;
}
if ($this->keysInDatabase() && !$this->keysOnDrive()) {
2020-05-23 08:14:13 +02:00
Log::debug('Keys are in DB and keys are not on the drive. Restore.');
2019-09-21 11:03:00 +02:00
$this->restoreKeysFromDB();
$this->line('Restored OAuth keys from database.');
return;
}
if (!$this->keysInDatabase() && $this->keysOnDrive()) {
2020-05-23 08:14:13 +02:00
Log::debug('Keys are not in DB and keys are on the drive. Save in DB.');
2019-09-21 11:03:00 +02:00
$this->storeKeysInDB();
$this->line('Stored OAuth keys in database.');
return;
}
$this->line('OAuth keys are OK');
}
/**
*
*/
private function storeKeysInDB(): void
{
OAuthKeys::storeKeysInDB();
2019-09-21 11:03:00 +02:00
}
2020-03-15 08:16:16 +01:00
}