Files
firefly-iii/app/Exceptions/GracefulNotFoundHandler.php

263 lines
8.8 KiB
PHP
Raw Normal View History

2019-08-01 06:22:07 +02:00
<?php
/**
* GracefulNotFoundHandler.php
2020-01-28 08:44:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
2019-08-01 06:22:07 +02:00
namespace FireflyIII\Exceptions;
2021-03-28 11:46:23 +02:00
2025-01-03 09:11:20 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2019-08-01 06:22:07 +02:00
use FireflyIII\Models\Account;
2019-08-03 06:27:56 +02:00
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Bill;
2019-08-01 06:22:07 +02:00
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
2019-08-01 06:22:07 +02:00
/**
* Class GracefulNotFoundHandler
*/
class GracefulNotFoundHandler extends ExceptionHandler
{
/**
* Render an exception into an HTTP response.
*
2023-12-20 19:35:52 +01:00
* @param Request $request
2019-08-17 10:46:55 +02:00
*
2023-12-20 19:35:52 +01:00
* @throws \Throwable
2023-12-22 20:12:38 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
2019-08-01 06:22:07 +02:00
*/
2023-12-20 19:35:52 +01:00
public function render($request, \Throwable $e): Response
2019-08-01 06:22:07 +02:00
{
$route = $request->route();
2019-08-27 06:57:30 +02:00
if (null === $route) {
2021-04-27 06:42:07 +02:00
return parent::render($request, $e);
2019-08-03 10:50:43 +02:00
}
$name = $route->getName();
2019-08-01 06:22:07 +02:00
if (!auth()->check()) {
2021-04-27 06:42:07 +02:00
return parent::render($request, $e);
2019-08-01 06:22:07 +02:00
}
switch ($name) {
default:
2022-10-30 14:44:49 +01:00
app('log')->warning(sprintf('GracefulNotFoundHandler cannot handle route with name "%s"', $name));
2019-08-01 06:22:07 +02:00
2021-04-27 06:42:07 +02:00
return parent::render($request, $e);
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'accounts.show':
2022-03-06 20:08:55 +01:00
case 'accounts.edit':
2020-02-19 07:17:58 +01:00
case 'accounts.show.all':
2021-04-27 06:42:07 +02:00
return $this->handleAccount($request, $e);
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'transactions.show':
case 'transactions.edit':
2021-04-27 06:42:07 +02:00
return $this->handleGroup($request, $e);
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'attachments.show':
2019-08-03 06:27:56 +02:00
case 'attachments.edit':
2020-10-21 06:24:16 +02:00
case 'attachments.download':
case 'attachments.view':
2019-08-03 06:27:56 +02:00
// redirect to original attachment holder.
2021-04-27 06:42:07 +02:00
return $this->handleAttachment($request, $e);
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'bills.show':
$request->session()->reflash();
return redirect(route('bills.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'currencies.show':
$request->session()->reflash();
return redirect(route('currencies.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'budgets.show':
2020-02-11 05:34:36 +01:00
case 'budgets.edit':
2022-01-02 07:22:04 +01:00
case 'budgets.show.limit':
2019-08-01 06:22:07 +02:00
$request->session()->reflash();
return redirect(route('budgets.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 17:19:32 +02:00
case 'piggy-banks.show':
$request->session()->reflash();
return redirect(route('piggy-banks.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 17:19:32 +02:00
case 'recurring.show':
2021-02-03 06:31:14 +01:00
case 'recurring.edit':
2019-08-01 17:19:32 +02:00
$request->session()->reflash();
return redirect(route('recurring.index'));
2023-12-20 19:35:52 +01:00
2019-08-27 06:57:30 +02:00
case 'tags.show.all':
2019-08-01 17:19:32 +02:00
case 'tags.show':
2019-11-09 15:01:48 +01:00
case 'tags.edit':
2019-08-01 17:19:32 +02:00
$request->session()->reflash();
return redirect(route('tags.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'categories.show':
case 'categories.edit':
2020-12-02 06:28:34 +01:00
case 'categories.show.all':
2019-08-01 06:22:07 +02:00
$request->session()->reflash();
return redirect(route('categories.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'rules.edit':
$request->session()->reflash();
return redirect(route('rules.index'));
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
case 'transactions.mass.edit':
case 'transactions.mass.delete':
case 'transactions.bulk.edit':
if ('POST' === $request->method()) {
$request->session()->reflash();
2021-03-21 09:15:40 +01:00
return redirect(route('index'));
}
2021-03-21 09:15:40 +01:00
2021-04-27 06:42:07 +02:00
return parent::render($request, $e);
2019-08-01 06:22:07 +02:00
}
}
/**
2023-12-20 19:35:52 +01:00
* @throws \Throwable
2019-08-01 06:22:07 +02:00
*/
2023-12-20 19:35:52 +01:00
private function handleAccount(Request $request, \Throwable $exception): Response
2019-08-01 06:22:07 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('404 page is probably a deleted account. Redirect to overview of account types.');
2023-12-20 19:35:52 +01:00
2019-08-01 06:22:07 +02:00
/** @var User $user */
2023-12-10 06:45:59 +01:00
$user = auth()->user();
$route = $request->route();
$param = $route->parameter('account');
2023-11-26 12:10:42 +01:00
$accountId = 0;
2022-10-31 05:53:36 +01:00
if ($param instanceof Account) {
2023-11-05 19:41:37 +01:00
$accountId = $param->id;
2022-10-31 05:50:44 +01:00
}
2023-11-26 12:10:42 +01:00
if (!($param instanceof Account) && !is_object($param)) {
2024-12-22 08:43:12 +01:00
$accountId = (int) $param;
2022-10-31 05:50:44 +01:00
}
2023-12-20 19:35:52 +01:00
/** @var null|Account $account */
$account = $user->accounts()->with(['accountType'])->withTrashed()->find($accountId);
2019-08-01 06:22:07 +02:00
if (null === $account) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not find account %d, so give big fat error.', $accountId));
2019-08-01 06:22:07 +02:00
return parent::render($request, $exception);
}
$type = $account->accountType;
$shortType = config(sprintf('firefly.shortNamesByFullName.%s', $type->type));
$request->session()->reflash();
return redirect(route('accounts.index', [$shortType]));
}
2020-03-25 19:25:50 +01:00
/**
2023-06-21 12:34:58 +02:00
* @return Response
2023-12-20 19:35:52 +01:00
*
* @throws \Throwable
2023-06-21 12:34:58 +02:00
*/
2023-12-20 19:35:52 +01:00
private function handleGroup(Request $request, \Throwable $exception)
2023-06-21 12:34:58 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('404 page is probably a deleted group. Redirect to overview of group types.');
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var User $user */
$user = auth()->user();
$route = $request->route();
2023-11-26 12:10:42 +01:00
$param = $route->parameter('transactionGroup');
2024-12-22 08:43:12 +01:00
$groupId = !is_object($param) ? (int) $param : 0;
2023-06-21 12:34:58 +02:00
2023-12-20 19:35:52 +01:00
/** @var null|TransactionGroup $group */
$group = $user->transactionGroups()->withTrashed()->find($groupId);
2023-06-21 12:34:58 +02:00
if (null === $group) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not find group %d, so give big fat error.', $groupId));
2023-06-21 12:34:58 +02:00
return parent::render($request, $exception);
}
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $journal */
2023-06-21 12:34:58 +02:00
$journal = $group->transactionJournals()->withTrashed()->first();
if (null === $journal) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not find journal for group %d, so give big fat error.', $groupId));
2023-06-21 12:34:58 +02:00
return parent::render($request, $exception);
}
$type = $journal->transactionType->type;
2023-06-21 12:34:58 +02:00
$request->session()->reflash();
2025-01-03 09:11:20 +01:00
if (TransactionTypeEnum::RECONCILIATION->value === $type) {
2023-06-21 12:34:58 +02:00
return redirect(route('accounts.index', ['asset']));
}
return redirect(route('transactions.index', [strtolower($type)]));
}
/**
2023-01-03 06:48:53 +01:00
* @return Response
2023-12-20 19:35:52 +01:00
*
* @throws \Throwable
2020-03-25 19:25:50 +01:00
*/
2023-12-20 19:35:52 +01:00
private function handleAttachment(Request $request, \Throwable $exception)
2019-08-03 06:27:56 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('404 page is probably a deleted attachment. Redirect to parent object.');
2023-12-20 19:35:52 +01:00
2019-08-03 06:27:56 +02:00
/** @var User $user */
$user = auth()->user();
$route = $request->route();
2023-11-26 12:10:42 +01:00
$param = $route->parameter('attachment');
2024-12-22 08:43:12 +01:00
$attachmentId = is_object($param) ? 0 : (int) $param;
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $attachment */
$attachment = $user->attachments()->withTrashed()->find($attachmentId);
2019-08-03 06:27:56 +02:00
if (null === $attachment) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not find attachment %d, so give big fat error.', $attachmentId));
2019-08-03 06:27:56 +02:00
return parent::render($request, $exception);
}
// get bindable.
if (TransactionJournal::class === $attachment->attachable_type) {
// is linked to journal, get group of journal (if not also deleted)
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $journal */
2019-08-03 06:27:56 +02:00
$journal = $user->transactionJournals()->withTrashed()->find($attachment->attachable_id);
if (null !== $journal) {
return redirect(route('transactions.show', [$journal->transaction_group_id]));
}
}
if (Bill::class === $attachment->attachable_type) {
// is linked to bill.
2023-12-20 19:35:52 +01:00
/** @var null|Bill $bill */
2019-08-03 06:27:56 +02:00
$bill = $user->bills()->withTrashed()->find($attachment->attachable_id);
if (null !== $bill) {
return redirect(route('bills.show', [$bill->id]));
}
}
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not redirect attachment %d, its linked to a %s.', $attachmentId, $attachment->attachable_type));
2019-08-03 06:27:56 +02:00
return parent::render($request, $exception);
}
2019-08-17 12:09:03 +02:00
}