Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -62,10 +62,10 @@ class Authenticate
* @param Closure $next
* @param string[] ...$guards
*
* @throws AuthenticationException
* @throws FireflyException
* @return mixed
*
* @throws FireflyException
* @throws AuthenticationException
*/
public function handle($request, Closure $next, ...$guards)
{
@@ -81,9 +81,9 @@ class Authenticate
* @param $request
* @param array $guards
*
* @throws AuthenticationException
* @throws FireflyException
* @return mixed
* @throws FireflyException
* @throws AuthenticationException
*/
protected function authenticate($request, array $guards)
{
@@ -97,10 +97,10 @@ class Authenticate
// do an extra check on user object.
/** @noinspection PhpUndefinedMethodInspection */
$user = $this->auth->authenticate();
if (1 === (int) $user->blocked) {
$message = (string) trans('firefly.block_account_logout');
if (1 === (int)$user->blocked) {
$message = (string)trans('firefly.block_account_logout');
if ('email_changed' === $user->blocked_code) {
$message = (string) trans('firefly.email_changed_logout');
$message = (string)trans('firefly.email_changed_logout');
}
app('session')->flash('logoutMessage', $message);
/** @noinspection PhpUndefinedMethodInspection */

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Http\Middleware;
use Closure;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\System\GeneratesInstallationId;
use Illuminate\Http\Request;
/**
*
@@ -39,7 +40,7 @@ class InstallationId
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Request $request
* @param Closure $next
*
* @return mixed
@@ -51,7 +52,6 @@ class InstallationId
{
$this->generateInstallationId();
return $next($request);

View File

@@ -44,12 +44,12 @@ class Installer
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
*
* @throws FireflyException
* @param Closure $next
*
* @return mixed
*
* @throws FireflyException
*
*/
public function handle($request, Closure $next)
{
@@ -81,35 +81,11 @@ class Installer
return $next($request);
}
/**
* Is access denied error.
*
* @param string $message
*
* @return bool
*/
protected function isAccessDenied(string $message): bool
{
return false !== stripos($message, 'Access denied');
}
/**
* Is no tables exist error.
*
* @param string $message
*
* @return bool
*/
protected function noTablesExist(string $message): bool
{
return false !== stripos($message, 'Base table or view not found');
}
/**
* Check if the tables are created and accounted for.
*
* @throws FireflyException
* @return bool
* @throws FireflyException
*/
private function hasNoTables(): bool
{
@@ -136,6 +112,30 @@ class Installer
return false;
}
/**
* Is access denied error.
*
* @param string $message
*
* @return bool
*/
protected function isAccessDenied(string $message): bool
{
return false !== stripos($message, 'Access denied');
}
/**
* Is no tables exist error.
*
* @param string $message
*
* @return bool
*/
protected function noTablesExist(string $message): bool
{
return false !== stripos($message, 'Base table or view not found');
}
/**
* Check if the "db_version" variable is correct.
*
@@ -144,8 +144,8 @@ class Installer
private function oldDBVersion(): bool
{
// older version in config than database?
$configVersion = (int) config('firefly.db_version');
$dbVersion = (int) app('fireflyconfig')->getFresh('db_version', 1)->data;
$configVersion = (int)config('firefly.db_version');
$dbVersion = (int)app('fireflyconfig')->getFresh('db_version', 1)->data;
if ($configVersion > $dbVersion) {
Log::warning(
sprintf(
@@ -170,8 +170,8 @@ class Installer
private function oldVersion(): bool
{
// version compare thing.
$configVersion = (string) config('firefly.version');
$dbVersion = (string) app('fireflyconfig')->getFresh('ff3_version', '1.0')->data;
$configVersion = (string)config('firefly.version');
$dbVersion = (string)app('fireflyconfig')->getFresh('ff3_version', '1.0')->data;
if (1 === version_compare($configVersion, $dbVersion)) {
Log::warning(
sprintf(

View File

@@ -58,6 +58,15 @@ class InterestingMessage
return $next($request);
}
/**
* @return bool
*/
private function testing(): bool
{
// ignore middleware in test environment.
return 'testing' === config('app.env') || !auth()->check();
}
/**
* @param Request $request
*
@@ -84,7 +93,7 @@ class InterestingMessage
// send message about newly created transaction group.
/** @var TransactionGroup $group */
$group = auth()->user()->transactionGroups()->with(['transactionJournals', 'transactionJournals.transactionType'])->find((int) $transactionGroupId);
$group = auth()->user()->transactionGroups()->with(['transactionJournals', 'transactionJournals.transactionType'])->find((int)$transactionGroupId);
if (null === $group) {
return;
@@ -100,21 +109,12 @@ class InterestingMessage
$title = $count > 1 ? $group->title : $journal->description;
if ('created' === $message) {
session()->flash('success_uri', route('transactions.show', [$transactionGroupId]));
session()->flash('success', (string) trans('firefly.stored_journal', ['description' => $title]));
session()->flash('success', (string)trans('firefly.stored_journal', ['description' => $title]));
}
if ('updated' === $message) {
$type = strtolower($journal->transactionType->type);
session()->flash('success_uri', route('transactions.show', [$transactionGroupId]));
session()->flash('success', (string) trans(sprintf('firefly.updated_%s', $type), ['description' => $title]));
session()->flash('success', (string)trans(sprintf('firefly.updated_%s', $type), ['description' => $title]));
}
}
/**
* @return bool
*/
private function testing(): bool
{
// ignore middleware in test environment.
return 'testing' === config('app.env') || !auth()->check();
}
}

View File

@@ -53,7 +53,7 @@ class IsDemoUser
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole($user, 'demo')) {
Log::info('User is a demo user.');
$request->session()->flash('info', (string) trans('firefly.not_available_demo_user'));
$request->session()->flash('info', (string)trans('firefly.not_available_demo_user'));
$current = $request->url();
$previous = $request->session()->previousUrl();
if ($current !== $previous) {

View File

@@ -36,6 +36,7 @@ use Log;
class Range
{
use RequestInformation;
/**
* Handle an incoming request.
*
@@ -61,51 +62,6 @@ class Range
return $next($request);
}
/**
* Configure the list length.
*/
private function configureList(): void
{
$pref = app('preferences')->get('list-length', config('firefly.list_length', 10))->data;
app('view')->share('listLength', $pref);
}
/**
* Configure the user's view.
*/
private function configureView(): void
{
// get locale preference:
$language = app('steam')->getLanguage();
$locale = app('steam')->getLocale();
App::setLocale($language);
Carbon::setLocale(substr($locale, 0, 2));
$localeArray = app('steam')->getLocaleArray($locale);
setlocale(LC_TIME, $localeArray);
$moneyResult = setlocale(LC_MONETARY, $localeArray);
// send error to view if could not set money format
if (false === $moneyResult) {
Log::error('Could not set locale. The following array doesnt work: ', $localeArray);
app('view')->share('invalidMonetaryLocale', true); // @codeCoverageIgnore
}
// save some formats:
$monthAndDayFormat = (string) trans('config.month_and_day', [], $locale);
$dateTimeFormat = (string) trans('config.date_time', [], $locale);
$defaultCurrency = app('amount')->getDefaultCurrency();
// also format for moment JS:
$madMomentJS = (string) trans('config.month_and_day_moment_js', [], $locale);
app('view')->share('madMomentJS', $madMomentJS);
app('view')->share('monthAndDayFormat', $monthAndDayFormat);
app('view')->share('dateTimeFormat', $dateTimeFormat);
app('view')->share('defaultCurrency', $defaultCurrency);
}
/**
* Set the range for the current view.
*/
@@ -133,4 +89,49 @@ class Range
app('session')->put('first', $first);
}
}
/**
* Configure the user's view.
*/
private function configureView(): void
{
// get locale preference:
$language = app('steam')->getLanguage();
$locale = app('steam')->getLocale();
App::setLocale($language);
Carbon::setLocale(substr($locale, 0, 2));
$localeArray = app('steam')->getLocaleArray($locale);
setlocale(LC_TIME, $localeArray);
$moneyResult = setlocale(LC_MONETARY, $localeArray);
// send error to view if could not set money format
if (false === $moneyResult) {
Log::error('Could not set locale. The following array doesnt work: ', $localeArray);
app('view')->share('invalidMonetaryLocale', true); // @codeCoverageIgnore
}
// save some formats:
$monthAndDayFormat = (string)trans('config.month_and_day', [], $locale);
$dateTimeFormat = (string)trans('config.date_time', [], $locale);
$defaultCurrency = app('amount')->getDefaultCurrency();
// also format for moment JS:
$madMomentJS = (string)trans('config.month_and_day_moment_js', [], $locale);
app('view')->share('madMomentJS', $madMomentJS);
app('view')->share('monthAndDayFormat', $monthAndDayFormat);
app('view')->share('dateTimeFormat', $dateTimeFormat);
app('view')->share('defaultCurrency', $defaultCurrency);
}
/**
* Configure the list length.
*/
private function configureList(): void
{
$pref = app('preferences')->get('list-length', config('firefly.list_length', 10))->data;
app('view')->share('listLength', $pref);
}
}

View File

@@ -39,8 +39,8 @@ class SecureHeaders
* @param Request $request
* @param Closure $next
*
* @throws Exception
* @return mixed
* @throws Exception
*/
public function handle(Request $request, Closure $next)
{
@@ -48,9 +48,9 @@ class SecureHeaders
$nonce = base64_encode(random_bytes(16));
app('view')->share('JS_NONCE', $nonce);
$response = $next($request);
$response = $next($request);
$trackingScriptSrc = $this->getTrackingScriptSource();
$csp = [
$csp = [
"default-src 'none'",
"object-src 'self'",
sprintf("script-src 'unsafe-inline' 'nonce-%1s' %2s", $nonce, $trackingScriptSrc),
@@ -58,7 +58,10 @@ class SecureHeaders
"base-uri 'self'",
"font-src 'self' data:",
"connect-src 'self'",
sprintf("img-src 'self' data: https://a.tile.openstreetmap.org https://b.tile.openstreetmap.org https://c.tile.openstreetmap.org https://api.tiles.mapbox.com %s", $trackingScriptSrc),
sprintf(
"img-src 'self' data: https://a.tile.openstreetmap.org https://b.tile.openstreetmap.org https://c.tile.openstreetmap.org https://api.tiles.mapbox.com %s",
$trackingScriptSrc
),
"manifest-src 'self'",
];
@@ -106,8 +109,8 @@ class SecureHeaders
*/
private function getTrackingScriptSource(): string
{
if ('' !== (string) config('firefly.tracker_site_id') && '' !== (string) config('firefly.tracker_url')) {
return (string) config('firefly.tracker_url');
if ('' !== (string)config('firefly.tracker_site_id') && '' !== (string)config('firefly.tracker_url')) {
return (string)config('firefly.tracker_url');
}
return '';

View File

@@ -25,7 +25,6 @@ namespace FireflyIII\Http\Middleware;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\Request;
use Illuminate\Session\Middleware\StartSession;
use Log;
/**
* Class StartFireflySession.

View File

@@ -43,7 +43,7 @@ class TrustProxies extends Middleware
*/
public function __construct(Repository $config)
{
$trustedProxies = (string) config('firefly.trusted_proxies');
$trustedProxies = (string)config('firefly.trusted_proxies');
$this->proxies = explode(',', $trustedProxies);
if ('**' === $trustedProxies) {
$this->proxies = '**';