Another fix for #2125

This commit is contained in:
James Cole 2019-02-27 18:55:56 +01:00
parent 95720673d2
commit fe738fd321

View File

@ -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);