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;
|
2021-09-18 03:20:19 -05:00
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
2019-07-31 23:22:07 -05:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2021-09-18 03:20:19 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-03-17 08:57:04 -05:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2019-07-31 23:22:07 -05:00
|
|
|
use Illuminate\Http\Request;
|
2020-03-17 08:57:04 -05:00
|
|
|
use Illuminate\Routing\Redirector;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2020-06-06 15:25:52 -05:00
|
|
|
use Throwable;
|
2019-07-31 23:22:07 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class GracefulNotFoundHandler
|
|
|
|
*/
|
|
|
|
class GracefulNotFoundHandler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2023-06-21 05:34:58 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Throwable $e
|
2019-08-17 03:46:55 -05:00
|
|
|
*
|
2021-09-18 03:20:19 -05:00
|
|
|
* @return Application|JsonResponse|\Illuminate\Http\Response|Redirector|RedirectResponse|Response
|
2021-04-26 23:42:07 -05:00
|
|
|
* @throws Throwable
|
2019-07-31 23:22:07 -05:00
|
|
|
*/
|
2021-04-26 23:42:07 -05:00
|
|
|
public function render($request, Throwable $e)
|
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
|
|
|
}
|
2019-08-26 23:57:30 -05: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);
|
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);
|
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);
|
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);
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'bills.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('bills.index'));
|
|
|
|
case 'currencies.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
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'));
|
2019-08-01 10:19:32 -05:00
|
|
|
case 'piggy-banks.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('piggy-banks.index'));
|
|
|
|
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'));
|
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'));
|
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'));
|
|
|
|
case 'rules.edit':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('rules.index'));
|
|
|
|
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-06-21 05:34:58 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Throwable $exception
|
2019-07-31 23:22:07 -05:00
|
|
|
*
|
2023-01-02 23:48:53 -06:00
|
|
|
* @return Response
|
2021-04-26 23:42:07 -05:00
|
|
|
* @throws Throwable
|
2019-07-31 23:22:07 -05:00
|
|
|
*/
|
2020-06-06 15:25:52 -05:00
|
|
|
private function handleAccount(Request $request, Throwable $exception)
|
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.');
|
2019-07-31 23:22:07 -05:00
|
|
|
/** @var User $user */
|
2022-12-29 12:41:57 -06:00
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2022-10-30 23:50:44 -05:00
|
|
|
$param = $route->parameter('account');
|
2022-10-30 23:53:36 -05:00
|
|
|
if ($param instanceof Account) {
|
2022-12-29 12:41:57 -06:00
|
|
|
$accountId = (int)$param->id;
|
2022-10-30 23:50:44 -05:00
|
|
|
}
|
2022-10-30 23:53:36 -05:00
|
|
|
if (!($param instanceof Account)) {
|
2022-12-29 12:41:57 -06:00
|
|
|
$accountId = (int)$param;
|
2022-10-30 23:50:44 -05:00
|
|
|
}
|
2023-01-02 23:48:53 -06:00
|
|
|
/** @var Account|null $account */
|
2019-07-31 23:22:07 -05:00
|
|
|
$account = $user->accounts()->with(['accountType'])->withTrashed()->find($accountId);
|
|
|
|
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
|
|
|
* @param Request $request
|
|
|
|
* @param Throwable $exception
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
private function handleGroup(Request $request, Throwable $exception)
|
|
|
|
{
|
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-06-21 05:34:58 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
|
|
|
$groupId = (int)$route->parameter('transactionGroup');
|
|
|
|
|
|
|
|
/** @var TransactionGroup|null $group */
|
|
|
|
$group = $user->transactionGroups()->withTrashed()->find($groupId);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
/** @var TransactionJournal|null $journal */
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
$type = $journal->transactionType->type;
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
if (TransactionType::RECONCILIATION === $type) {
|
|
|
|
return redirect(route('accounts.index', ['asset']));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('transactions.index', [strtolower($type)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param Throwable $exception
|
2020-03-25 13:25:50 -05:00
|
|
|
*
|
2023-01-02 23:48:53 -06:00
|
|
|
* @return Response
|
2021-04-26 23:42:07 -05:00
|
|
|
* @throws Throwable
|
2020-03-25 13:25:50 -05:00
|
|
|
*/
|
2020-06-06 15:25:52 -05: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.');
|
2019-08-02 23:27:56 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2022-12-29 12:41:57 -06:00
|
|
|
$attachmentId = (int)$route->parameter('attachment');
|
2023-02-12 00:23:57 -06:00
|
|
|
/** @var Attachment|null $attachment */
|
2019-08-02 23:27:56 -05:00
|
|
|
$attachment = $user->attachments()->withTrashed()->find($attachmentId);
|
|
|
|
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)
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$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.
|
|
|
|
/** @var Bill $bill */
|
|
|
|
$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
|
|
|
}
|