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;
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
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;
|
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;
|
2019-07-31 23:22:07 -05:00
|
|
|
use Log;
|
2020-03-17 08:57:04 -05:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Exception $exception
|
2019-08-17 03:46:55 -05:00
|
|
|
*
|
2019-07-31 23:22:07 -05:00
|
|
|
* @return mixed
|
2020-09-23 13:28:20 -05:00
|
|
|
* @throws Exception
|
2019-07-31 23:22:07 -05:00
|
|
|
*/
|
2020-06-06 15:25:52 -05:00
|
|
|
public function render($request, Throwable $exception)
|
2019-07-31 23:22:07 -05:00
|
|
|
{
|
|
|
|
$route = $request->route();
|
2019-08-26 23:57:30 -05:00
|
|
|
if (null === $route) {
|
2019-08-03 03:50:43 -05:00
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2019-08-26 23:57:30 -05:00
|
|
|
$name = $route->getName();
|
2019-07-31 23:22:07 -05:00
|
|
|
if (!auth()->check()) {
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($name) {
|
|
|
|
default:
|
2019-09-15 01:06:19 -05:00
|
|
|
Log::warning(sprintf('GracefulNotFoundHandler cannot handle route with name "%s"', $name));
|
2019-07-31 23:22:07 -05:00
|
|
|
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
case 'accounts.show':
|
2020-02-19 00:17:58 -06:00
|
|
|
case 'accounts.show.all':
|
2019-07-31 23:22:07 -05:00
|
|
|
return $this->handleAccount($request, $exception);
|
|
|
|
case 'transactions.show':
|
|
|
|
return $this->handleGroup($request, $exception);
|
|
|
|
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.
|
|
|
|
return $this->handleAttachment($request, $exception);
|
2019-07-31 23:22:07 -05: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-10 22:34:36 -06:00
|
|
|
case 'budgets.edit':
|
2019-07-31 23:22:07 -05:00
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('budgets.index'));
|
|
|
|
break;
|
2019-08-01 10:19:32 -05:00
|
|
|
case 'piggy-banks.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('piggy-banks.index'));
|
|
|
|
break;
|
|
|
|
case 'recurring.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('recurring.index'));
|
|
|
|
break;
|
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'));
|
|
|
|
break;
|
2019-07-31 23:22:07 -05:00
|
|
|
case 'categories.show':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('categories.index'));
|
|
|
|
case 'rules.edit':
|
|
|
|
$request->session()->reflash();
|
|
|
|
|
|
|
|
return redirect(route('rules.index'));
|
2019-09-15 01:06:19 -05:00
|
|
|
case 'transactions.edit':
|
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();
|
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
|
|
|
return parent::render($request, $exception);
|
2019-07-31 23:22:07 -05:00
|
|
|
}
|
2020-09-23 13:28:20 -05:00
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2020-06-06 15:25:52 -05:00
|
|
|
* @param Throwable $exception
|
2019-07-31 23:22:07 -05:00
|
|
|
*
|
2020-03-25 13:25:50 -05:00
|
|
|
* @return Redirector|Response
|
2020-09-23 13:28:20 -05:00
|
|
|
* @throws Exception
|
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
|
|
|
{
|
|
|
|
Log::debug('404 page is probably a deleted account. Redirect to overview of account types.');
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2020-03-17 08:57:04 -05:00
|
|
|
$accountId = (int) $route->parameter('account');
|
2019-07-31 23:22:07 -05: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]));
|
|
|
|
}
|
|
|
|
|
2020-03-25 13:25:50 -05:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
2020-06-06 15:25:52 -05:00
|
|
|
* @param Throwable $exception
|
2020-03-25 13:25:50 -05:00
|
|
|
*
|
|
|
|
* @return RedirectResponse|Redirector|Response
|
2020-09-23 13:28:20 -05:00
|
|
|
* @throws Exception
|
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
|
|
|
{
|
|
|
|
Log::debug('404 page is probably a deleted attachment. Redirect to parent object.');
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2020-03-17 08:57:04 -05:00
|
|
|
$attachmentId = (int) $route->parameter('attachment');
|
2019-08-02 23:27:56 -05: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-07-31 23:22:07 -05:00
|
|
|
/**
|
2020-09-23 13:28:20 -05:00
|
|
|
* @param Throwable $request
|
2019-07-31 23:22:07 -05:00
|
|
|
* @param Exception $exception
|
|
|
|
*
|
2020-03-17 08:57:04 -05:00
|
|
|
* @return RedirectResponse|\Illuminate\Http\Response|Redirector|Response
|
2020-09-23 13:28:20 -05:00
|
|
|
* @throws Exception
|
2019-07-31 23:22:07 -05:00
|
|
|
*/
|
2020-06-06 15:25:52 -05:00
|
|
|
private function handleGroup(Request $request, Throwable $exception)
|
2019-07-31 23:22:07 -05:00
|
|
|
{
|
|
|
|
Log::debug('404 page is probably a deleted group. Redirect to overview of group types.');
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$route = $request->route();
|
2020-03-17 08:57:04 -05:00
|
|
|
$groupId = (int) $route->parameter('transactionGroup');
|
2019-07-31 23:22:07 -05:00
|
|
|
|
|
|
|
/** @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();
|
|
|
|
|
2020-03-11 00:50:23 -05:00
|
|
|
if (TransactionType::RECONCILIATION === $type) {
|
|
|
|
return redirect(route('accounts.index', ['asset']));
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:22:07 -05:00
|
|
|
return redirect(route('transactions.index', [strtolower($type)]));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:09:03 -05:00
|
|
|
}
|