diff --git a/app/Http/Controllers/Budget/ShowController.php b/app/Http/Controllers/Budget/ShowController.php index 7d666382fb..72013035ba 100644 --- a/app/Http/Controllers/Budget/ShowController.php +++ b/app/Http/Controllers/Budget/ShowController.php @@ -103,6 +103,8 @@ class ShowController extends Controller * @param JournalRepositoryInterface $repository * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + * + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function noBudgetAll(Request $request, JournalRepositoryInterface $repository) { diff --git a/app/Http/Controllers/Chart/BudgetReportController.php b/app/Http/Controllers/Chart/BudgetReportController.php index 3d2f2aba80..386c910663 100644 --- a/app/Http/Controllers/Chart/BudgetReportController.php +++ b/app/Http/Controllers/Chart/BudgetReportController.php @@ -131,6 +131,9 @@ class BudgetReportController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function mainChart(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end): JsonResponse { diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index f7b020d66c..a942eb8125 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -60,6 +60,8 @@ class CategoryController extends Controller * @param Category $category * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function all(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Category $category): JsonResponse { @@ -69,13 +71,8 @@ class CategoryController extends Controller if ($cache->has()) { return response()->json($cache->get()); // @codeCoverageIgnore } - - $start = $repository->firstUseDate($category); - - if (null === $start) { - $start = new Carbon; // @codeCoverageIgnore - } - + $start = $repository->firstUseDate($category); + $start = $start ?? new Carbon; $range = app('preferences')->get('viewRange', '1M')->data; $start = app('navigation')->startOfPeriod($start, $range); $end = new Carbon; @@ -83,19 +80,15 @@ class CategoryController extends Controller $chartData = [ [ 'label' => (string)trans('firefly.spent'), - 'entries' => [], - 'type' => 'bar', + 'entries' => [], 'type' => 'bar', ], [ 'label' => (string)trans('firefly.earned'), - 'entries' => [], - 'type' => 'bar', + 'entries' => [], 'type' => 'bar', ], [ 'label' => (string)trans('firefly.sum'), - 'entries' => [], - 'type' => 'line', - 'fill' => false, + 'entries' => [], 'type' => 'line', 'fill' => false, ], ]; @@ -167,6 +160,8 @@ class CategoryController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function reportPeriod(Category $category, Collection $accounts, Carbon $start, Carbon $end): JsonResponse { @@ -225,6 +220,8 @@ class CategoryController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function reportPeriodNoCategory(Collection $accounts, Carbon $start, Carbon $end): JsonResponse { @@ -297,6 +294,8 @@ class CategoryController extends Controller * @param Carbon $end * * @return array + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ private function makePeriodChart(Category $category, Carbon $start, Carbon $end): array { diff --git a/app/Http/Controllers/Chart/CategoryReportController.php b/app/Http/Controllers/Chart/CategoryReportController.php index ea2d1f22ce..1b5d8f6099 100644 --- a/app/Http/Controllers/Chart/CategoryReportController.php +++ b/app/Http/Controllers/Chart/CategoryReportController.php @@ -72,6 +72,8 @@ class CategoryReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function accountExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -94,6 +96,8 @@ class CategoryReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function accountIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -119,6 +123,8 @@ class CategoryReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function categoryExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -144,6 +150,7 @@ class CategoryReportController extends Controller * @param string $others * * @return JsonResponse + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function categoryIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -169,6 +176,10 @@ class CategoryReportController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function mainChart(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse { @@ -276,6 +287,8 @@ class CategoryReportController extends Controller * @param Carbon $end * * @return Collection + * + * */ private function getExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Collection { diff --git a/app/Http/Controllers/Chart/ExpenseReportController.php b/app/Http/Controllers/Chart/ExpenseReportController.php index 3bb7d264fc..40576c6ef6 100644 --- a/app/Http/Controllers/Chart/ExpenseReportController.php +++ b/app/Http/Controllers/Chart/ExpenseReportController.php @@ -64,6 +64,7 @@ class ExpenseReportController extends Controller ); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $expense @@ -71,6 +72,10 @@ class ExpenseReportController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ public function mainChart(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): JsonResponse { diff --git a/app/Http/Controllers/Chart/PiggyBankController.php b/app/Http/Controllers/Chart/PiggyBankController.php index fa0fa29105..f9b2aba5db 100644 --- a/app/Http/Controllers/Chart/PiggyBankController.php +++ b/app/Http/Controllers/Chart/PiggyBankController.php @@ -59,6 +59,9 @@ class PiggyBankController extends Controller * @param PiggyBank $piggyBank * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function history(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank): JsonResponse { diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 53aaefee34..492013e393 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -94,6 +94,8 @@ class ReportController extends Controller * @param Carbon $end * * @return \Illuminate\Http\JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse { @@ -123,7 +125,6 @@ class ReportController extends Controller 'entries' => [], ], ]; - foreach ($source['earned'] as $date => $amount) { $carbon = new Carbon($date); $label = $carbon->formatLocalized($format); @@ -151,6 +152,9 @@ class ReportController extends Controller * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function sum(Collection $accounts, Carbon $start, Carbon $end): JsonResponse { @@ -238,6 +242,8 @@ class ReportController extends Controller * @param Carbon $end * * @return array + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ private function getChartData(Collection $accounts, Carbon $start, Carbon $end): array { diff --git a/app/Http/Controllers/Chart/TagReportController.php b/app/Http/Controllers/Chart/TagReportController.php index 8488049c85..09b1583aec 100644 --- a/app/Http/Controllers/Chart/TagReportController.php +++ b/app/Http/Controllers/Chart/TagReportController.php @@ -57,6 +57,7 @@ class TagReportController extends Controller $this->generator = app(GeneratorInterface::class); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -65,6 +66,8 @@ class TagReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function accountExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -81,6 +84,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -89,6 +93,8 @@ class TagReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function accountIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -105,6 +111,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -112,6 +119,8 @@ class TagReportController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function budgetExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): JsonResponse { @@ -128,6 +137,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -135,6 +145,8 @@ class TagReportController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function categoryExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): JsonResponse { @@ -151,6 +163,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -158,6 +171,10 @@ class TagReportController extends Controller * @param Carbon $end * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function mainChart(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): JsonResponse { @@ -257,6 +274,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -265,6 +283,8 @@ class TagReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function tagExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -281,6 +301,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -289,6 +310,8 @@ class TagReportController extends Controller * @param string $others * * @return JsonResponse + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function tagIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse { @@ -305,6 +328,7 @@ class TagReportController extends Controller return response()->json($data); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -312,6 +336,8 @@ class TagReportController extends Controller * @param Carbon $end * * @return Collection + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ private function getExpenses(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): Collection { @@ -327,6 +353,7 @@ class TagReportController extends Controller return $collector->getJournals(); } + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param Collection $accounts * @param Collection $tags @@ -334,6 +361,8 @@ class TagReportController extends Controller * @param Carbon $end * * @return Collection + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ private function getIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): Collection { diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 4107419f72..5ca4b3f940 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -37,6 +37,8 @@ use URL; /** * Class Controller. + * + * @SuppressWarnings(PHPMD.NumberOfChildren) */ class Controller extends BaseController { @@ -78,26 +80,9 @@ class Controller extends BaseController // get shown-intro-preference: if (auth()->check()) { - // some routes have a "what" parameter, which indicates a special page: - $specificPage = null === Route::current()->parameter('what') ? '' : '_' . Route::current()->parameter('what'); - $page = str_replace('.', '_', Route::currentRouteName()); - - // indicator if user has seen the help for this page ( + special page): - $key = 'shown_demo_' . $page . $specificPage; - // is there an intro for this route? - $intro = config('intro.' . $page); - $specialIntro = config('intro.' . $page . $specificPage); - $shownDemo = true; - - // either must be array and either must be > 0 - if ((\is_array($intro) || \is_array($specialIntro)) && (\count($intro) > 0 || \count($specialIntro) > 0)) { - $shownDemo = app('preferences')->get($key, false)->data; - Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %d', $key, $shownDemo)); - } - - // share language - $language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data; - + $language = $this->getLanguage(); + $page = $this->getPageName(); + $shownDemo = $this->hasSeenDemo(); app('view')->share('language', $language); app('view')->share('shownDemo', $shownDemo); app('view')->share('current_route_name', $page); @@ -174,4 +159,56 @@ class Controller extends BaseController { session()->put($identifier, URL::previous()); } + + /** + * @return string + */ + private function getLanguage(): string + { + /** @var string $language */ + $language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data; + + return $language; + } + + /** + * @return string + */ + private function getPageName(): string + { + return str_replace('.', '_', Route::currentRouteName()); + } + + /** + * @return string + */ + private function getSpecificPageName(): string + { + return null === Route::current()->parameter('what') ? '' : '_' . Route::current()->parameter('what'); + } + + /** + * @return bool + */ + private function hasSeenDemo(): bool + { + $page = $this->getPageName(); + $specificPage = $this->getSpecificPageName(); + + // indicator if user has seen the help for this page ( + special page): + $key = 'shown_demo_' . $page . $specificPage; + // is there an intro for this route? + $intro = config('intro.' . $page) ?? []; + $specialIntro = config('intro.' . $page . $specificPage) ?? []; + // some routes have a "what" parameter, which indicates a special page: + + $shownDemo = true; + // both must be array and either must be > 0 + if (\count($intro) > 0 || \count($specialIntro) > 0) { + $shownDemo = app('preferences')->get($key, false)->data; + Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %d', $key, $shownDemo)); + } + + return $shownDemo; + } } diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php index a655adfdf6..67172255b2 100644 --- a/app/Http/Controllers/CurrencyController.php +++ b/app/Http/Controllers/CurrencyController.php @@ -241,6 +241,8 @@ class CurrencyController extends Controller * @param CurrencyFormRequest $request * * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function store(CurrencyFormRequest $request) { @@ -270,7 +272,6 @@ class CurrencyController extends Controller } if (null === $currency) { $request->session()->flash('error', (string)trans('firefly.could_not_store_currency')); - } return $redirect; diff --git a/app/Http/Controllers/DebugController.php b/app/Http/Controllers/DebugController.php index 989b4c2ea6..7a70eb50cd 100644 --- a/app/Http/Controllers/DebugController.php +++ b/app/Http/Controllers/DebugController.php @@ -37,6 +37,8 @@ use Route as RouteFacade; /** * Class DebugController + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class DebugController extends Controller { @@ -100,6 +102,9 @@ class DebugController extends Controller * @param Request $request * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function index(Request $request) { @@ -191,9 +196,8 @@ class DebugController extends Controller $return = ' '; /** @var Route $route */ foreach ($set as $route) { - $name = $route->getName(); - if (null !== $name && \strlen($name) > 0 && \in_array('GET', $route->methods(), true)) { - + $name = (string)$route->getName(); + if (\in_array('GET', $route->methods(), true)) { $found = false; foreach ($ignore as $string) { if (!(false === stripos($name, $string))) { diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index d3ca0e6c55..51e954ebe5 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -82,6 +82,9 @@ class Authenticate * @return mixed * @throws \Illuminate\Auth\AuthenticationException * @throws FireflyException + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function authenticate(array $guards) { diff --git a/app/Http/Middleware/AuthenticateTwoFactor.php b/app/Http/Middleware/AuthenticateTwoFactor.php index b47affcf0f..078261add4 100644 --- a/app/Http/Middleware/AuthenticateTwoFactor.php +++ b/app/Http/Middleware/AuthenticateTwoFactor.php @@ -52,17 +52,17 @@ class AuthenticateTwoFactor } - /** @noinspection PhpUnusedParameterInspection */ /** * @param $request * @param Closure $next - * @param array ...$guards * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed * @throws \Psr\Container\NotFoundExceptionInterface * @throws \Psr\Container\ContainerExceptionInterface + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function handle($request, Closure $next, ...$guards) + public function handle($request, Closure $next) { /** @noinspection PhpUndefinedMethodInspection */ if ($this->auth->guest()) { diff --git a/app/Http/Middleware/Binder.php b/app/Http/Middleware/Binder.php index e6e702a564..323dcd4a57 100644 --- a/app/Http/Middleware/Binder.php +++ b/app/Http/Middleware/Binder.php @@ -60,12 +60,11 @@ class Binder * * @param \Illuminate\Http\Request $request * @param \Closure $next - * @param string[] ...$guards * * @return mixed * */ - public function handle($request, Closure $next, ...$guards) + public function handle($request, Closure $next) { foreach ($request->route()->parameters() as $key => $value) { if (isset($this->binders[$key])) { diff --git a/app/Http/Middleware/Installer.php b/app/Http/Middleware/Installer.php index 11dfbae057..b78e966a23 100644 --- a/app/Http/Middleware/Installer.php +++ b/app/Http/Middleware/Installer.php @@ -45,6 +45,9 @@ class Installer * @param \Closure $next * * @return mixed + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function handle($request, Closure $next) { diff --git a/app/Http/Middleware/IsSandStormUser.php b/app/Http/Middleware/IsSandStormUser.php index 227151199b..03590d4b64 100644 --- a/app/Http/Middleware/IsSandStormUser.php +++ b/app/Http/Middleware/IsSandStormUser.php @@ -49,7 +49,7 @@ class IsSandStormUser } if (1 === (int)getenv('SANDSTORM')) { - Session::flash('warning', (string)trans('firefly.sandstorm_not_available')); + app('session')->flash('warning', (string)trans('firefly.sandstorm_not_available')); return response()->redirectTo(route('index')); } diff --git a/app/Http/Middleware/Range.php b/app/Http/Middleware/Range.php index 03aaa1aade..4f7314fa3f 100644 --- a/app/Http/Middleware/Range.php +++ b/app/Http/Middleware/Range.php @@ -27,7 +27,6 @@ use Carbon\Carbon; use Closure; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Http\Request; -use Session; /** * Class SessionFilter. @@ -114,25 +113,21 @@ class Range } /** - * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function setRange(): void { // ignore preference. set the range to be the current month: - if (!Session::has('start') && !Session::has('end')) { + if (!app('session')->has('start') && !app('session')->has('end')) { $viewRange = app('preferences')->get('viewRange', '1M')->data; - if (null === $viewRange) { - $viewRange = '1M'; - app('preferences')->set('viewRange', '1M'); - } - $start = new Carbon; - $start = app('navigation')->updateStartDate($viewRange, $start); - $end = app('navigation')->updateEndDate($viewRange, $start); + $start = new Carbon; + $start = app('navigation')->updateStartDate($viewRange, $start); + $end = app('navigation')->updateEndDate($viewRange, $start); - Session::put('start', $start); - Session::put('end', $end); + app('session')->put('start', $start); + app('session')->put('end', $end); } - if (!Session::has('first')) { + if (!app('session')->has('first')) { /** @var JournalRepositoryInterface $repository */ $repository = app(JournalRepositoryInterface::class); $journal = $repository->firstNull(); @@ -141,7 +136,7 @@ class Range if (null !== $journal) { $first = $journal->date ?? $first; } - Session::put('first', $first); + app('session')->put('first', $first); } } } diff --git a/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php b/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php index c585018bf3..f6bcefab69 100644 --- a/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php +++ b/app/Http/Middleware/RedirectIfTwoFactorAuthenticated.php @@ -38,16 +38,15 @@ class RedirectIfTwoFactorAuthenticated * @param string|null $guard * * @return mixed + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { $is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data; - $has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret'); - - // grab 2auth information from cookie. - $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); + $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); if ($is2faEnabled && $has2faSecret && $is2faAuthed) { return response()->redirectTo(route('index')); diff --git a/app/Http/Middleware/Sandstorm.php b/app/Http/Middleware/Sandstorm.php index 208ee3f08b..5614c7adb2 100644 --- a/app/Http/Middleware/Sandstorm.php +++ b/app/Http/Middleware/Sandstorm.php @@ -62,73 +62,39 @@ class Sandstorm /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); $userId = (string)$request->header('X-Sandstorm-User-Id'); - Log::debug(sprintf('Sandstorm user ID is "%s"', $userId)); - $count = $repository->count(); + // catch anonymous: + $userId = $userId === '' ? 'anonymous' : $userId; + $email = $userId . '@firefly'; + $user = $repository->findByEmail($email) ?? $this->createUser($email); + Log::debug(sprintf('Sandstorm user email is "%s"', $email)); - // if there already is one user in this instance, we assume this is - // the "main" user. Firefly's nature does not allow other users to - // access the same data so we have no choice but to simply login - // the new user to the same account and just forget about Bob and Alice - // and any other differences there may be between these users. - if (1 === $count && \strlen($userId) > 0) { - // login as first user user. - $user = $repository->first(); - /** @noinspection NullPointerExceptionInspection */ - Auth::guard($guard)->login($user); - app('view')->share('SANDSTORM_ANON', false); - - return $next($request); - } - - if (1 === $count && '' === $userId) { - // login but indicate anonymous - $user = User::first(); - /** @noinspection NullPointerExceptionInspection */ - Auth::guard($guard)->login($user); - app('view')->share('SANDSTORM_ANON', true); - - return $next($request); - } - - if (0 === $count && \strlen($userId) > 0) { - // create new user. - $email = $userId . '@firefly'; - /** @var User $user */ - $user = $repository->store( - [ - 'blocked' => false, - 'blocked_code' => null, - 'email' => $email, - ] - ); - Auth::guard($guard)->login($user); - - // also make the user an admin - $repository->attachRole($user, 'owner'); - - // share value. - app('view')->share('SANDSTORM_ANON', false); - - return $next($request); - } - - if (0 === $count && '' === $userId) { - throw new FireflyException('The first visit to a new Firefly III administration cannot be by a guest user.'); - } - - if ($count > 1) { - throw new FireflyException('Your Firefly III installation has more than one user, which is weird.'); - } + Auth::guard($guard)->login($user); + $repository->attachRole($user, 'owner'); + app('view')->share('SANDSTORM_ANON', false); } - // if in Sandstorm, user logged in, still must check if user is anon. - $userId = (string)$request->header('X-Sandstorm-User-Id'); - if ('' === $userId) { - app('view')->share('SANDSTORM_ANON', true); - - return $next($request); - } - app('view')->share('SANDSTORM_ANON', false); return $next($request); } + + + /** + * @param string $email + * + * @return User + */ + private function createUser(string $email): User + { + $repository = app(UserRepositoryInterface::class); + + return $repository->store( + [ + 'blocked' => false, + 'blocked_code' => null, + 'email' => $email, + ] + ); + + } } + +