Skip to content

Firefly III: a personal finances manager

Add some delays, make sure user cannot flush cache to avoid them.

James Cole4bacaa85a340
diff --git a/app/Http/Controllers/DebugController.php b/app/Http/Controllers/DebugController.php
index 0873bbcf59..2ab896ddef 100644
--- a/app/Http/Controllers/DebugController.php
+++ b/app/Http/Controllers/DebugController.php
@@ -39,6 +39,7 @@ use FireflyIII\Support\Facades\Preferences;
use FireflyIII\Support\Facades\Steam;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use FireflyIII\Support\Models\AccountBalanceCalculator;
+use FireflyIII\Support\System\IsOldVersion;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
@@ -64,6 +65,7 @@ use const PHP_SAPI;
*/
final class DebugController extends Controller
{
+ use IsOldVersion;
use GetConfigurationData;
/**
@@ -106,6 +108,9 @@ final class DebugController extends Controller
*/
public function flush(Request $request): RedirectResponse
{
+ if ($this->hasNoTables() || $this->isOldVersionInstalled()) {
+ throw new FireflyException('This is not the moment.');
+ }
Preferences::mark();
$request->session()->forget(['start', 'end', '_previous', 'viewRange', 'range', 'is_custom_range', 'temp-mfa-secret', 'temp-mfa-codes']);
diff --git a/app/Http/Controllers/System/InstallController.php b/app/Http/Controllers/System/InstallController.php
index 349626b78f..bc579da248 100644
--- a/app/Http/Controllers/System/InstallController.php
+++ b/app/Http/Controllers/System/InstallController.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\System;
+use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
@@ -54,7 +55,7 @@ final class InstallController extends Controller
public const string BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.';
public const string FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.';
- public const string OTHER_ERROR = 'An unknown error prevented Firefly III from executing the "<code>%s</code>"-command. Sorry.';
+ public const string OTHER_ERROR = 'An error prevented Firefly III from executing the "<code>%s</code>"-command: ';
private string $lastError = '';
// empty on purpose.
@@ -63,7 +64,7 @@ final class InstallController extends Controller
// Check 4 places: InstallController, Docker image, UpgradeDatabase, composer.json
'firefly-iii:create-database' => [],
'migrate' => ['--seed' => true, '--force' => true],
- 'generate-keys' => [], // an exception :(
+ 'generate-keys' => [],
'firefly-iii:upgrade-database' => [],
'firefly-iii:set-latest-version' => ['--james-is-cool' => true],
'firefly-iii:verify-security-alerts' => [],
@@ -77,14 +78,14 @@ final class InstallController extends Controller
return view('install.index');
}
-
+ Log::debug('Installer has finished, return to home.');
return response()->redirectToRoute('home');
}
/**
* Create specific RSA keys.
*/
- public function keys(): void
+ private function keys(): void
{
if ($this->hasNoTables() || $this->isOldVersionInstalled()) {
$key = RSA::createKey(4096);
@@ -105,6 +106,7 @@ final class InstallController extends Controller
// return response()->json([], 403);
if ($this->hasNoTables() || $this->isOldVersionInstalled()) {
$requestIndex = (int) $request->input('index');
+ $requestIndex = clamp($requestIndex, 0, count($this->upgradeCommands) - 1);
$response = ['hasNextCommand' => false, 'done' => true, 'previous' => null, 'error' => false, 'errorMessage' => null];
Log::debug(sprintf('Will now run commands. Request index is %d', $requestIndex));
@@ -156,22 +158,33 @@ final class InstallController extends Controller
*/
private function executeCommand(string $command, array $args): bool
{
+ $key = hash('sha256', sprintf('Installer - %s - %s', $command, json_encode($args)));
Log::debug(sprintf('Will now call command %s with args.', $command), $args);
-
+ if(Cache::has($key)) {
+ $time = Cache::get($key);
+ $diff = Carbon::now()->timestamp - $time;
+ if($diff < 120) {
+ throw new FireflyException(sprintf('This command was called recently, please wait two minutes before you try again (wait time is another %d sec).', 120- $diff));
+ }
+ }
try {
if ('generate-keys' === $command) {
$this->keys();
}
+ if('firefly-iii:create-database' === $command && !$this->hasNoTables()) {
+ Log::debug('Database already exists, skipping create-database command.');
+ Cache::set($key, Carbon::now()->timestamp);
+ return true;
+ }
if ('generate-keys' !== $command) {
Artisan::call($command, $args);
Log::debug(Artisan::output());
}
} catch (Exception $e) { // intentional generic exception
+ Cache::clear();
throw new FireflyException($e->getMessage(), 0, $e);
}
- // clear cache as well.
- Cache::clear();
- Preferences::mark();
+ Cache::set($key, Carbon::now()->timestamp);
return true;
}
diff --git a/app/Support/System/IsOldVersion.php b/app/Support/System/IsOldVersion.php
index 770f0b5254..e320a90bda 100644
--- a/app/Support/System/IsOldVersion.php
+++ b/app/Support/System/IsOldVersion.php
@@ -112,11 +112,7 @@ trait IsOldVersion
$message = $e->getMessage();
Log::error(sprintf('Error message trying to access users-table: %s', $message));
if ($this->isAccessDenied($message)) {
- throw new FireflyException(
- 'It seems your database configuration is not correct. Please verify the username and password in your .env file.',
- 0,
- $e
- );
+ throw new FireflyException('It seems your database configuration is not correct. Please verify the username and password in your .env file.', 0, $e);
}
if ($this->noTablesExist($message)) {
// redirect to UpdateController
diff --git a/public/v1/js/ff/install/index.js b/public/v1/js/ff/install/index.js
index f9f02ce17b..383b4149ca 100644
--- a/public/v1/js/ff/install/index.js
+++ b/public/v1/js/ff/install/index.js
@@ -18,7 +18,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-
document.addEventListener("DOMContentLoaded", (event) => {
console.log('Starting...');
startRunningCommands(0);
diff --git a/resources/views/install/index.blade.php b/resources/views/install/index.blade.php
index 9ef9ec25be..839e8233b8 100644
--- a/resources/views/install/index.blade.php
+++ b/resources/views/install/index.blade.php
@@ -20,7 +20,6 @@
</div>
@endsection
@section('scripts')
-@yield('scripts')
<script type="text/javascript" nonce="{{ $JS_NONCE }}">
var token = '{{ csrf_token() }}';
var index = 0;
@@ -29,5 +28,3 @@
</script>
<script type="text/javascript" src="v1/js/ff/install/index.js" nonce="{{ $JS_NONCE }}"></script>
@endsection
-</body>
-</html>
diff --git a/resources/views/layout/v3/install.blade.php b/resources/views/layout/v3/install.blade.php
index 9d1cc8687c..406eabcf43 100644
--- a/resources/views/layout/v3/install.blade.php
+++ b/resources/views/layout/v3/install.blade.php
@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
<html lang="{{ __('config.html_language') }}">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>