2019-07-31 23:22:07 -05:00
|
|
|
<?php
|
2019-10-01 23:37:26 -05:00
|
|
|
/**
|
|
|
|
* GracefulNotFoundHandler.php
|
2020-01-28 01:44:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-10-01 23:37:26 -05:00
|
|
|
*
|
|
|
|
* 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 05:09:03 -05:00
|
|
|
declare(strict_types=1);
|
2019-07-31 23:22:07 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Exceptions;
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2019-08-02 23:27:56 -05:00
|
|
|
use FireflyIII\Models\Attachment;
|
|
|
|
use FireflyIII\Models\Bill;
|
2019-07-31 23:22:07 -05:00
|
|
|
use FireflyIII\Models\TransactionGroup;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2020-03-11 00:50:23 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2019-07-31 23:22:07 -05:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
|
|
use Illuminate\Http\Request;
|
2020-03-17 08:57:04 -05:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2019-07-31 23:22:07 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class GracefulNotFoundHandler
|
|
|
|
*/
|
|
|
|
class GracefulNotFoundHandler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2023-12-20 12:35:52 -06:00
|
|
|
* @param Request $request
|
2019-08-17 03:46:55 -05:00
|
|
|
*
|
2023-12-20 12:35:52 -06:00
|
|
|
* @throws \Throwable
|
2023-12-22 13:12:38 -06:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2019-07-31 23:22:07 -05:00
|
|
|
*/
|
2023-12-20 12:35:52 -06:00
|
|
|
public function render($request, \Throwable $e): Response
|
2019-07-31 23:22:07 -05:00
|
|
|
{
|
|
|
|
$route = $request->route();
|
2019-08-26 23:57:30 -05:00
|
|
|
if (null === $route) {
|
2021-04-26 23:42:07 -05:00
|
|
|
return parent::render($request, $e);
|
2019-08-03 03:50:43 -05:00
|
|
|
}
|
2024-01-01 07:43:56 -06:00
|
|
|
$name = $route->getName();
|
2019-07-31 23:22:07 -05:00
|
|
|
if (!auth()->check()) {
|
2021-04-26 23:42:07 -05:00
|
|
|
return parent::render($request, $e);
|
2019-07-31 23:22:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ($name) {
|
|
|
|
default:
|
2022-10-30 08:44:49 -05:00
|
|
|
app('log')->warning(sprintf('GracefulNotFoundHandler cannot handle route with name "%s"', $name));
|
2019-07-31 23:22:07 -05:00
|
|
|
|
2021-04-26 23:42:07 -05:00
|
|
|
return parent::render($request, $e);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'accounts.show':
|
2022-03-06 13:08:55 -06:00
|
|
|
case 'accounts.edit':
|
2020-02-19 00:17:58 -06:00
|
|
|
case 'accounts.show.all':
|
2021-04-26 23:42:07 -05:00
|
|
|
return $this->handleAccount($request, $e);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'transactions.show':
|
2022-04-01 00:37:45 -05:00
|
|
|
case 'transactions.edit':
|
2021-04-26 23:42:07 -05:00
|
|
|
return $this->handleGroup($request, $e);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'attachments.show':
|
2019-08-02 23:27:56 -05:00
|
|
|
case 'attachments.edit':
|
2020-10-20 23:24:16 -05:00
|
|
|
case 'attachments.download':
|
|
|
|
case 'attachments.view':
|
2019-08-02 23:27:56 -05:00
|
|
|
// redirect to original attachment holder.
|
2021-04-26 23:42:07 -05:00
|
|
|
return $this->handleAttachment($request, $e);
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'bills.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('bills.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'currencies.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'budgets.show':
|
2020-02-10 22:34:36 -06:00
|
|
|
case 'budgets.edit':
|
2022-01-02 00:22:04 -06:00
|
|
|
case 'budgets.show.limit':
|
2019-07-31 23:22:07 -05:00
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('budgets.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-08-01 10:19:32 -05:00
|
|
|
case 'piggy-banks.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('piggy-banks.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-08-01 10:19:32 -05:00
|
|
|
case 'recurring.show':
|
2021-02-02 23:31:14 -06:00
|
|
|
case 'recurring.edit':
|
2019-08-01 10:19:32 -05:00
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('recurring.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-08-26 23:57:30 -05:00
|
|
|
case 'tags.show.all':
|
2019-08-01 10:19:32 -05:00
|
|
|
case 'tags.show':
|
2019-11-09 08:01:48 -06:00
|
|
|
case 'tags.edit':
|
2019-08-01 10:19:32 -05:00
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('tags.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'categories.show':
|
2020-12-01 23:28:34 -06:00
|
|
|
case 'categories.show.all':
|
2019-07-31 23:22:07 -05:00
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('categories.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'rules.edit':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('rules.index'));
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'transactions.mass.edit':
|
|
|
|
case 'transactions.mass.delete':
|
|
|
|
case 'transactions.bulk.edit':
|
2020-09-23 13:28:20 -05:00
|
|
|
if ('POST' === $request->method()) {
|
|
|
|
$request->session()->reflash();
|
2021-03-21 03:15:40 -05:00
|
|
|
|
2020-09-23 13:28:20 -05:00
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
|
2021-04-26 23:42:07 -05:00
|
|
|
return parent::render($request, $e);
|
2019-07-31 23:22:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 12:35:52 -06:00
|
|
|
* @throws \Throwable
|
2019-07-31 23:22:07 -05:00
|
|
|
*/
|
2023-12-20 12:35:52 -06:00
|
|
|
private function handleAccount(Request $request, \Throwable $exception): Response
|
2019-07-31 23:22:07 -05:00
|
|
|
{
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug('404 page is probably a deleted account. Redirect to overview of account types.');
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
/** @var User $user */
|
2023-12-09 23:45:59 -06:00
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
|
|
|
$param = $route->parameter('account');
|
2023-11-26 05:10:42 -06:00
|
|
|
$accountId = 0;
|
2022-10-30 23:53:36 -05:00
|
|
|
if ($param instanceof Account) {
|
2023-11-05 12:41:37 -06:00
|
|
|
$accountId = $param->id;
|
2022-10-30 23:50:44 -05:00
|
|
|
}
|
2023-11-26 05:10:42 -06:00
|
|
|
if (!($param instanceof Account) && !is_object($param)) {
|
2022-12-29 12:41:57 -06:00
|
|
|
$accountId = (int)$param;
|
2022-10-30 23:50:44 -05:00
|
|
|
}
|
2023-12-20 12:35:52 -06:00
|
|
|
|
|
|
|
/** @var null|Account $account */
|
2024-01-01 07:43:56 -06:00
|
|
|
$account = $user->accounts()->with(['accountType'])->withTrashed()->find($accountId);
|
2019-07-31 23:22:07 -05:00
|
|
|
if (null === $account) {
|
2023-10-29 00:32:00 -05:00
|
|
|
app('log')->error(sprintf('Could not find account %d, so give big fat error.', $accountId));
|
2019-07-31 23:22:07 -05: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 13:25:50 -05:00
|
|
|
/**
|
2023-06-21 05:34:58 -05:00
|
|
|
* @return Response
|
2023-12-20 12:35:52 -06:00
|
|
|
*
|
|
|
|
* @throws \Throwable
|
2023-06-21 05:34:58 -05:00
|
|
|
*/
|
2023-12-20 12:35:52 -06:00
|
|
|
private function handleGroup(Request $request, \Throwable $exception)
|
2023-06-21 05:34:58 -05:00
|
|
|
{
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug('404 page is probably a deleted group. Redirect to overview of group types.');
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2023-11-26 05:10:42 -06:00
|
|
|
$param = $route->parameter('transactionGroup');
|
|
|
|
$groupId = !is_object($param) ? (int)$param : 0;
|
2023-06-21 05:34:58 -05:00
|
|
|
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|TransactionGroup $group */
|
2024-01-01 07:43:56 -06:00
|
|
|
$group = $user->transactionGroups()->withTrashed()->find($groupId);
|
2023-06-21 05:34:58 -05:00
|
|
|
if (null === $group) {
|
2023-10-29 00:32:00 -05:00
|
|
|
app('log')->error(sprintf('Could not find group %d, so give big fat error.', $groupId));
|
2023-06-21 05:34:58 -05:00
|
|
|
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2023-12-20 12:35:52 -06:00
|
|
|
|
|
|
|
/** @var null|TransactionJournal $journal */
|
2023-06-21 05:34:58 -05:00
|
|
|
$journal = $group->transactionJournals()->withTrashed()->first();
|
|
|
|
if (null === $journal) {
|
2023-10-29 00:32:00 -05:00
|
|
|
app('log')->error(sprintf('Could not find journal for group %d, so give big fat error.', $groupId));
|
2023-06-21 05:34:58 -05:00
|
|
|
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2024-01-01 07:43:56 -06:00
|
|
|
$type = $journal->transactionType->type;
|
2023-06-21 05:34:58 -05:00
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
if (TransactionType::RECONCILIATION === $type) {
|
|
|
|
return redirect(route('accounts.index', ['asset']));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('transactions.index', [strtolower($type)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-02 23:48:53 -06:00
|
|
|
* @return Response
|
2023-12-20 12:35:52 -06:00
|
|
|
*
|
|
|
|
* @throws \Throwable
|
2020-03-25 13:25:50 -05:00
|
|
|
*/
|
2023-12-20 12:35:52 -06:00
|
|
|
private function handleAttachment(Request $request, \Throwable $exception)
|
2019-08-02 23:27:56 -05:00
|
|
|
{
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug('404 page is probably a deleted attachment. Redirect to parent object.');
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2019-08-02 23:27:56 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2023-11-26 05:10:42 -06:00
|
|
|
$param = $route->parameter('attachment');
|
|
|
|
$attachmentId = is_object($param) ? 0 : (int)$param;
|
2023-12-20 12:35:52 -06:00
|
|
|
|
|
|
|
/** @var null|Attachment $attachment */
|
2024-01-01 07:43:56 -06:00
|
|
|
$attachment = $user->attachments()->withTrashed()->find($attachmentId);
|
2019-08-02 23:27:56 -05:00
|
|
|
if (null === $attachment) {
|
2023-10-29 00:32:00 -05:00
|
|
|
app('log')->error(sprintf('Could not find attachment %d, so give big fat error.', $attachmentId));
|
2019-08-02 23:27:56 -05: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 12:35:52 -06:00
|
|
|
/** @var null|TransactionJournal $journal */
|
2019-08-02 23:27:56 -05: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 12:35:52 -06:00
|
|
|
/** @var null|Bill $bill */
|
2019-08-02 23:27:56 -05:00
|
|
|
$bill = $user->bills()->withTrashed()->find($attachment->attachable_id);
|
|
|
|
if (null !== $bill) {
|
|
|
|
return redirect(route('bills.show', [$bill->id]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 00:32:00 -05:00
|
|
|
app('log')->error(sprintf('Could not redirect attachment %d, its linked to a %s.', $attachmentId, $attachment->attachable_type));
|
2019-08-02 23:27:56 -05:00
|
|
|
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2019-08-17 05:09:03 -05:00
|
|
|
}
|