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

253 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
2019-08-01 06:22:07 +02:00
use Exception;
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;
2020-03-11 06:50:23 +01:00
use FireflyIII\Models\TransactionType;
2019-08-01 06:22:07 +02:00
use FireflyIII\User;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\RedirectResponse;
2019-08-01 06:22:07 +02:00
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
2019-08-01 06:22:07 +02:00
use Log;
use Symfony\Component\HttpFoundation\Response;
2020-06-06 22:25:52 +02:00
use Throwable;
2019-08-01 06:22:07 +02:00
/**
* Class GracefulNotFoundHandler
*/
class GracefulNotFoundHandler extends ExceptionHandler
{
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Exception $exception
2019-08-17 10:46:55 +02:00
*
2019-08-01 06:22:07 +02:00
* @return mixed
* @throws Exception
2019-08-01 06:22:07 +02:00
*/
2020-06-06 22:25:52 +02:00
public function render($request, Throwable $exception)
2019-08-01 06:22:07 +02:00
{
$route = $request->route();
2019-08-27 06:57:30 +02:00
if (null === $route) {
2019-08-03 10:50:43 +02:00
return parent::render($request, $exception);
}
2019-08-27 06:57:30 +02:00
$name = $route->getName();
2019-08-01 06:22:07 +02:00
if (!auth()->check()) {
return parent::render($request, $exception);
}
switch ($name) {
default:
2019-09-15 08:06:19 +02:00
Log::warning(sprintf('GracefulNotFoundHandler cannot handle route with name "%s"', $name));
2019-08-01 06:22:07 +02:00
return parent::render($request, $exception);
case 'accounts.show':
2020-02-19 07:17:58 +01:00
case 'accounts.show.all':
2019-08-01 06:22:07 +02:00
return $this->handleAccount($request, $exception);
case 'transactions.show':
return $this->handleGroup($request, $exception);
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.
return $this->handleAttachment($request, $exception);
2019-08-01 06:22:07 +02:00
break;
case 'bills.show':
$request->session()->reflash();
return redirect(route('bills.index'));
break;
case 'currencies.show':
$request->session()->reflash();
return redirect(route('currencies.index'));
break;
case 'budgets.show':
2020-02-11 05:34:36 +01:00
case 'budgets.edit':
2019-08-01 06:22:07 +02:00
$request->session()->reflash();
return redirect(route('budgets.index'));
break;
2019-08-01 17:19:32 +02:00
case 'piggy-banks.show':
$request->session()->reflash();
return redirect(route('piggy-banks.index'));
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'));
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'));
2019-08-01 06:22:07 +02:00
case 'categories.show':
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'));
case 'rules.edit':
$request->session()->reflash();
return redirect(route('rules.index'));
2019-09-15 08:06:19 +02:00
case 'transactions.edit':
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
return parent::render($request, $exception);
2019-08-01 06:22:07 +02:00
}
2019-08-01 06:22:07 +02:00
}
/**
* @param Request $request
2020-06-06 22:25:52 +02:00
* @param Throwable $exception
2019-08-01 06:22:07 +02:00
*
2020-03-25 19:25:50 +01:00
* @return Redirector|Response
* @throws Exception
2019-08-01 06:22:07 +02:00
*/
2020-06-06 22:25:52 +02:00
private function handleAccount(Request $request, Throwable $exception)
2019-08-01 06:22:07 +02:00
{
Log::debug('404 page is probably a deleted account. Redirect to overview of account types.');
/** @var User $user */
$user = auth()->user();
$route = $request->route();
2021-03-21 09:15:40 +01:00
$accountId = (int)$route->parameter('account');
2019-08-01 06:22:07 +02:00
/** @var Account $account */
$account = $user->accounts()->with(['accountType'])->withTrashed()->find($accountId);
if (null === $account) {
Log::error(sprintf('Could not find account %d, so give big fat error.', $accountId));
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]));
}
2021-03-21 09:15:40 +01:00
/**
* @param Throwable $request
* @param Exception $exception
*
* @return RedirectResponse|\Illuminate\Http\Response|Redirector|Response
* @throws Exception
*/
private function handleGroup(Request $request, Throwable $exception)
{
Log::debug('404 page is probably a deleted group. Redirect to overview of group types.');
/** @var User $user */
$user = auth()->user();
$route = $request->route();
$groupId = (int)$route->parameter('transactionGroup');
/** @var TransactionGroup $group */
$group = $user->transactionGroups()->withTrashed()->find($groupId);
if (null === $group) {
Log::error(sprintf('Could not find group %d, so give big fat error.', $groupId));
return parent::render($request, $exception);
}
/** @var TransactionJournal $journal */
$journal = $group->transactionJournals()->withTrashed()->first();
if (null === $journal) {
Log::error(sprintf('Could not find journal for group %d, so give big fat error.', $groupId));
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)]));
}
2020-03-25 19:25:50 +01:00
/**
* @param Request $request
2020-06-06 22:25:52 +02:00
* @param Throwable $exception
2020-03-25 19:25:50 +01:00
*
* @return RedirectResponse|Redirector|Response
* @throws Exception
2020-03-25 19:25:50 +01:00
*/
2020-06-06 22:25:52 +02:00
private function handleAttachment(Request $request, Throwable $exception)
2019-08-03 06:27:56 +02:00
{
Log::debug('404 page is probably a deleted attachment. Redirect to parent object.');
/** @var User $user */
$user = auth()->user();
$route = $request->route();
2021-03-21 09:15:40 +01:00
$attachmentId = (int)$route->parameter('attachment');
2019-08-03 06:27:56 +02:00
/** @var Attachment $attachment */
$attachment = $user->attachments()->withTrashed()->find($attachmentId);
if (null === $attachment) {
Log::error(sprintf('Could not find attachment %d, so give big fat error.', $attachmentId));
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]));
}
}
Log::error(sprintf('Could not redirect attachment %d, its linked to a %s.', $attachmentId, $attachment->attachable_type));
return parent::render($request, $exception);
}
2019-08-17 12:09:03 +02:00
}