From fe738fd321b166d5df8ddf39fba99387800aea51 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 27 Feb 2019 18:55:56 +0100 Subject: [PATCH] Another fix for #2125 --- app/Support/Amount.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/Support/Amount.php b/app/Support/Amount.php index bfa19e8bc2..a29381a460 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -22,10 +22,13 @@ declare(strict_types=1); namespace FireflyIII\Support; +use Crypt; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; +use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Support\Collection; +use Log; use Preferences as Prefs; /** @@ -221,6 +224,23 @@ class Amount return $this->getDefaultCurrencyByUser($user); } + /** + * @param string $value + * + * @return string + */ + private function tryDecrypt(string $value): string + { + try { + $value = Crypt::decrypt($value); + } catch (DecryptException $e) { + Log::debug(sprintf('Could not decrypt. %s', $e->getMessage())); + } + + return $value; + } + + /** * @param User $user * @@ -237,9 +257,12 @@ class Amount return $cache->get(); // @codeCoverageIgnore } $currencyPreference = Prefs::getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR')); - $currency = TransactionCurrency::where('code', $currencyPreference->data)->first(); + + // at this point the currency preference could be encrypted, if coming from an old version. + $currencyCode = $this->tryDecrypt((string)$currencyPreference->data); + $currency = TransactionCurrency::where('code', $currencyCode)->first(); if (null === $currency) { - throw new FireflyException(sprintf('No currency found with code "%s"', $currencyPreference->data)); + throw new FireflyException(sprintf('No currency found with code "%s"', $currencyCode)); } $cache->store($currency);