From 8f98be32f959f09bcc08964b8baae038f2d37918 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 30 Mar 2022 06:54:59 +0200 Subject: [PATCH] Fix various sonatype issues. --- .../Data/Bulk/MoveTransactionsRequest.php | 61 ++++++++----- app/Http/Controllers/CurrencyController.php | 88 +++++++++---------- .../Currency/CurrencyRepository.php | 22 ++++- .../Currency/CurrencyRepositoryInterface.php | 6 +- resources/views/auth/lost-two-factor.twig | 4 +- resources/views/debug.twig | 3 +- resources/views/emails/header-html.twig | 4 +- resources/views/errors/404.twig | 4 +- resources/views/errors/500.twig | 4 +- resources/views/errors/503.twig | 4 +- resources/views/errors/FireflyException.twig | 4 +- resources/views/errors/offline.twig | 2 +- resources/views/layout/default.twig | 2 +- resources/views/layout/empty.twig | 2 +- resources/views/layout/guest.twig | 2 +- resources/views/layout/install.twig | 2 +- 16 files changed, 123 insertions(+), 91 deletions(-) diff --git a/app/Api/V1/Requests/Data/Bulk/MoveTransactionsRequest.php b/app/Api/V1/Requests/Data/Bulk/MoveTransactionsRequest.php index 4cd1b85675..47ddfb65f9 100644 --- a/app/Api/V1/Requests/Data/Bulk/MoveTransactionsRequest.php +++ b/app/Api/V1/Requests/Data/Bulk/MoveTransactionsRequest.php @@ -74,32 +74,45 @@ class MoveTransactionsRequest extends FormRequest // validate start before end only if both are there. $data = $validator->getData(); if (array_key_exists('original_account', $data) && array_key_exists('destination_account', $data)) { - $repository = app(AccountRepositoryInterface::class); - $repository->setUser(auth()->user()); - $original = $repository->find((int) $data['original_account']); - $destination = $repository->find((int) $data['destination_account']); - if ($original->accountType->type !== $destination->accountType->type) { - $validator->errors()->add('title', (string) trans('validation.same_account_type')); - - return; - } - // get currency pref: - $originalCurrency = $repository->getAccountCurrency($original); - $destinationCurrency = $repository->getAccountCurrency($destination); - if (null === $originalCurrency xor null === $destinationCurrency) { - $validator->errors()->add('title', (string) trans('validation.same_account_currency')); - - return; - } - if (null === $originalCurrency && null === $destinationCurrency) { - // this is OK - return; - } - if ($originalCurrency->code !== $destinationCurrency->code) { - $validator->errors()->add('title', (string) trans('validation.same_account_currency')); - } + $this->validateMove($validator); } } ); } + + /** + * @param Validator $validator + * @return void + */ + private function validateMove(Validator $validator): void { + $data = $validator->getData(); + $repository = app(AccountRepositoryInterface::class); + $repository->setUser(auth()->user()); + $original = $repository->find((int) $data['original_account']); + $destination = $repository->find((int) $data['destination_account']); + + // not the same type: + if ($original->accountType->type !== $destination->accountType->type) { + $validator->errors()->add('title', (string) trans('validation.same_account_type')); + + return; + } + // get currency pref: + $originalCurrency = $repository->getAccountCurrency($original); + $destinationCurrency = $repository->getAccountCurrency($destination); + + // check different scenario's. + if (null === $originalCurrency xor null === $destinationCurrency) { + $validator->errors()->add('title', (string) trans('validation.same_account_currency')); + + return; + } + if (null === $originalCurrency && null === $destinationCurrency) { + // this is OK + return; + } + if ($originalCurrency->code !== $destinationCurrency->code) { + $validator->errors()->add('title', (string) trans('validation.same_account_currency')); + } + } } diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php index 82b03a8c3f..f8b7c71e50 100644 --- a/app/Http/Controllers/CurrencyController.php +++ b/app/Http/Controllers/CurrencyController.php @@ -215,56 +215,50 @@ class CurrencyController extends Controller public function disableCurrency(Request $request): JsonResponse { $currencyId = (int) $request->get('id'); - if ($currencyId > 0) { - // valid currency? - $currency = $this->repository->find($currencyId); - if (null !== $currency) { - app('preferences')->mark(); + $currency = $this->repository->find($currencyId); - /** @var User $user */ - $user = auth()->user(); - if (!$this->userRepository->hasRole($user, 'owner')) { - - $request->session()->flash('error', (string) trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))])); - Log::channel('audit')->info(sprintf('Tried to disable currency %s but is not site owner.', $currency->code)); - return response()->json([]); - - } - - if ($this->repository->currencyInUse($currency)) { - - $location = $this->repository->currencyInUseAt($currency); - $message = (string) trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]); - - $request->session()->flash('error', $message); - Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code)); - return response()->json([]); - } - - $this->repository->disable($currency); - Log::channel('audit')->info(sprintf('Disabled currency %s.', $currency->code)); - // if no currencies are enabled, enable the first one in the DB (usually the EUR) - if (0 === $this->repository->get()->count()) { - /** @var TransactionCurrency $first */ - $first = $this->repository->getAll()->first(); - if (null === $first) { - throw new FireflyException('No currencies found.'); - } - Log::channel('audit')->info(sprintf('Auto-enabled currency %s.', $first->code)); - $this->repository->enable($first); - app('preferences')->set('currencyPreference', $first->code); - app('preferences')->mark(); - } - - if ('EUR' === $currency->code) { - session()->flash('warning', (string) trans('firefly.disable_EUR_side_effects')); - } - - session()->flash('success', (string) trans('firefly.currency_is_now_disabled', ['name' => $currency->name])); - } + // valid currency? + if (null === $currency) { + return response()->json([]); } - return response()->json([]); + app('preferences')->mark(); + + // user must be "owner" + /** @var User $user */ + $user = auth()->user(); + if (!$this->userRepository->hasRole($user, 'owner')) { + + $request->session()->flash('error', (string) trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))])); + Log::channel('audit')->info(sprintf('Tried to disable currency %s but is not site owner.', $currency->code)); + return response()->json([]); + + } + + // currency cannot be in use. + if ($this->repository->currencyInUse($currency)) { + + $location = $this->repository->currencyInUseAt($currency); + $message = (string) trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]); + + $request->session()->flash('error', $message); + Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code)); + return response()->json([]); + } + + // currency disabled! + $this->repository->disable($currency); + Log::channel('audit')->info(sprintf('Disabled currency %s.', $currency->code)); + + $this->repository->ensureMinimalEnabledCurrencies(); + + // extra warning + if ('EUR' === $currency->code) { + session()->flash('warning', (string) trans('firefly.disable_EUR_side_effects')); + } + + session()->flash('success', (string) trans('firefly.currency_is_now_disabled', ['name' => $currency->name])); + } /** diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index c93b81b22f..4619703cfc 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -39,6 +39,7 @@ use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService; use FireflyIII\Services\Internal\Update\CurrencyUpdateService; use FireflyIII\User; use Illuminate\Support\Collection; +use JsonException; use Log; /** @@ -200,6 +201,25 @@ class CurrencyRepository implements CurrencyRepositoryInterface $currency->save(); } + /** + * @inheritDoc + */ + public function ensureMinimalEnabledCurrencies(): void + { + // if no currencies are enabled, enable the first one in the DB (usually the EUR) + if (0 === $this->get()->count()) { + /** @var TransactionCurrency $first */ + $first = $this->getAll()->first(); + if (null === $first) { + throw new FireflyException('No currencies found. You broke Firefly III'); + } + Log::channel('audit')->info(sprintf('Auto-enabled currency %s.', $first->code)); + $this->enable($first); + app('preferences')->set('currencyPreference', $first->code); + app('preferences')->mark(); + } + } + /** * Find by currency code, return NULL if unfound. * Used in Import Currency! @@ -274,7 +294,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface * * @return TransactionCurrency * @throws FireflyException - * @throws \JsonException + * @throws JsonException */ public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency { diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.php index 7c50d812de..35efcb3b76 100644 --- a/app/Repositories/Currency/CurrencyRepositoryInterface.php +++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php @@ -35,7 +35,6 @@ use Illuminate\Support\Collection; */ interface CurrencyRepositoryInterface { - /** * @param TransactionCurrency $currency * @@ -80,6 +79,11 @@ interface CurrencyRepositoryInterface */ public function enable(TransactionCurrency $currency): void; + /** + * @return void + */ + public function ensureMinimalEnabledCurrencies(): void; + /** * Find by ID, return NULL if not found. * diff --git a/resources/views/auth/lost-two-factor.twig b/resources/views/auth/lost-two-factor.twig index 07089c0f5e..530a6cf94f 100644 --- a/resources/views/auth/lost-two-factor.twig +++ b/resources/views/auth/lost-two-factor.twig @@ -1,5 +1,5 @@ - + @@ -18,7 +18,7 @@
diff --git a/resources/views/debug.twig b/resources/views/debug.twig index c770abad9e..6f1de2a4f0 100644 --- a/resources/views/debug.twig +++ b/resources/views/debug.twig @@ -1,4 +1,5 @@ - + + {{ trans('firefly.debug_page') }} diff --git a/resources/views/emails/header-html.twig b/resources/views/emails/header-html.twig index d4e89e3255..42583e2128 100644 --- a/resources/views/emails/header-html.twig +++ b/resources/views/emails/header-html.twig @@ -1,8 +1,8 @@ - + - + Message

diff --git a/resources/views/errors/404.twig b/resources/views/errors/404.twig index 28845e45f8..ae4c2f0dd0 100644 --- a/resources/views/errors/404.twig +++ b/resources/views/errors/404.twig @@ -1,5 +1,5 @@ - + @@ -29,7 +29,7 @@

-

FireflyIII 404 Not Found :(

+

FireflyIII 404 Not Found :(

diff --git a/resources/views/errors/500.twig b/resources/views/errors/500.twig index f01b0f0f95..34d7b3a6da 100644 --- a/resources/views/errors/500.twig +++ b/resources/views/errors/500.twig @@ -1,5 +1,5 @@ - + @@ -29,7 +29,7 @@
diff --git a/resources/views/errors/503.twig b/resources/views/errors/503.twig index cb206bebb4..d669d11821 100644 --- a/resources/views/errors/503.twig +++ b/resources/views/errors/503.twig @@ -1,5 +1,5 @@ - + @@ -25,7 +25,7 @@
diff --git a/resources/views/errors/FireflyException.twig b/resources/views/errors/FireflyException.twig index df85b70dff..824bee2605 100644 --- a/resources/views/errors/FireflyException.twig +++ b/resources/views/errors/FireflyException.twig @@ -1,5 +1,5 @@ - + @@ -29,7 +29,7 @@
diff --git a/resources/views/errors/offline.twig b/resources/views/errors/offline.twig index b1130978ee..6e98cb6943 100644 --- a/resources/views/errors/offline.twig +++ b/resources/views/errors/offline.twig @@ -1,5 +1,5 @@ - + diff --git a/resources/views/layout/default.twig b/resources/views/layout/default.twig index 02edacd333..a558b85f6f 100644 --- a/resources/views/layout/default.twig +++ b/resources/views/layout/default.twig @@ -62,7 +62,7 @@ {# mini logo for sidebar mini 50x50 pixels #} FF {# logo for regular state and mobile devices #} - FireflyIII + FireflyIII