firefly-iii/app/Rules/IsValidAttachmentModel.php

227 lines
6.4 KiB
PHP
Raw Normal View History

2018-06-23 23:51:22 -05:00
<?php
/**
* IsValidAttachmentModel.php
2020-02-16 06:56:25 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-23 23:51:22 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-23 23:51:22 -05:00
*
* 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.
2018-06-23 23:51:22 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-06-23 23:51:22 -05:00
* 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.
2018-06-23 23:51:22 -05:00
*
* 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/>.
2018-06-23 23:51:22 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Rules;
2023-10-29 11:41:14 -05:00
use Closure;
use FireflyIII\Models\Account;
2018-12-21 03:11:18 -06:00
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Tag;
2018-12-21 03:11:18 -06:00
use FireflyIII\Models\Transaction;
2018-06-23 23:51:22 -05:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-12-21 03:11:18 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
2018-06-23 23:51:22 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2023-10-29 11:41:14 -05:00
use Illuminate\Contracts\Validation\ValidationRule;
2018-06-23 23:51:22 -05:00
/**
* Class IsValidAttachmentModel
*/
2023-10-29 11:41:14 -05:00
class IsValidAttachmentModel implements ValidationRule
2018-06-23 23:51:22 -05:00
{
/** @var string */
2023-10-29 11:41:14 -05:00
private string $model;
2018-06-23 23:51:22 -05:00
/**
* IsValidAttachmentModel constructor.
*
2019-07-31 09:53:09 -05:00
*
2023-06-21 05:34:58 -05:00
* @param string $model
2018-06-23 23:51:22 -05:00
*/
public function __construct(string $model)
{
2019-07-31 09:53:09 -05:00
$model = $this->normalizeModel($model);
2018-06-23 23:51:22 -05:00
$this->model = $model;
}
2023-06-21 05:34:58 -05:00
/**
* @param string $model
*
* @return string
*/
private function normalizeModel(string $model): string
{
$search = ['FireflyIII\Models\\'];
$replace = '';
$model = str_replace($search, $replace, $model);
return sprintf('FireflyIII\Models\%s', $model);
}
2018-06-23 23:51:22 -05:00
/**
2023-10-29 11:41:14 -05:00
* @param string $attribute
* @param mixed $value
* @param Closure $fail
2021-03-21 03:15:40 -05:00
*
2023-10-29 11:41:14 -05:00
* @return void
2018-06-23 23:51:22 -05:00
*/
2023-10-29 11:41:14 -05:00
public function validate(string $attribute, mixed $value, Closure $fail): void
2018-06-23 23:51:22 -05:00
{
if (!auth()->check()) {
2023-10-29 11:41:14 -05:00
$fail('validation.model_id_invalid')->translate();
return;
2018-06-23 23:51:22 -05:00
}
$methods = [
Account::class => 'validateAccount',
Bill::class => 'validateBill',
Budget::class => 'validateBudget',
Category::class => 'validateCategory',
PiggyBank::class => 'validatePiggyBank',
Tag::class => 'validateTag',
Transaction::class => 'validateTransaction',
TransactionJournal::class => 'validateJournal',
];
2021-04-06 11:48:02 -05:00
if (!array_key_exists($this->model, $methods)) {
2023-10-29 00:32:00 -05:00
app('log')->error(sprintf('Cannot validate model "%s" in %s.', substr($this->model, 0, 20), __METHOD__));
2018-06-23 23:51:22 -05:00
2023-10-29 11:41:14 -05:00
$fail('validation.model_id_invalid')->translate();
return;
}
$method = $methods[$this->model];
2019-07-31 09:53:09 -05:00
2023-11-05 01:15:17 -06:00
$result = $this->$method((int)$value); // @phpstan-ignore-line
2023-10-29 11:41:14 -05:00
if(false === $result) {
$fail('validation.model_id_invalid')->translate();
}
}
2018-12-21 03:11:18 -06:00
2023-05-29 06:56:55 -05:00
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
2021-03-21 03:15:40 -05:00
private function validateAccount(int $value): bool
{
2021-03-21 03:15:40 -05:00
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$repository->setUser(auth()->user());
2018-12-21 03:11:18 -06:00
2021-06-29 23:17:38 -05:00
return null !== $repository->find($value);
}
2018-12-21 03:11:18 -06:00
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
2021-03-21 03:15:40 -05:00
private function validateBill(int $value): bool
{
2021-03-21 03:15:40 -05:00
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser(auth()->user());
2018-12-21 03:11:18 -06:00
2021-03-21 03:15:40 -05:00
return null !== $repository->find($value);
}
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
private function validateBudget(int $value): bool
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$repository->setUser(auth()->user());
2018-12-21 03:11:18 -06:00
2021-06-29 23:17:38 -05:00
return null !== $repository->find($value);
}
2018-07-25 23:10:17 -05:00
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
private function validateCategory(int $value): bool
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$repository->setUser(auth()->user());
2018-07-25 23:10:17 -05:00
2021-06-29 23:17:38 -05:00
return null !== $repository->find($value);
}
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
2021-03-21 03:15:40 -05:00
private function validateJournal(int $value): bool
{
2021-03-21 03:15:40 -05:00
$repository = app(JournalRepositoryInterface::class);
$repository->setUser(auth()->user());
2021-06-29 23:17:38 -05:00
return null !== $repository->find($value);
}
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
2021-03-21 03:15:40 -05:00
private function validatePiggyBank(int $value): bool
{
2021-03-21 03:15:40 -05:00
/** @var PiggyBankRepositoryInterface $repository */
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser(auth()->user());
2021-06-29 23:17:38 -05:00
return null !== $repository->find($value);
}
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
2021-03-21 03:15:40 -05:00
private function validateTag(int $value): bool
{
2021-03-21 03:15:40 -05:00
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$repository->setUser(auth()->user());
2018-07-25 23:10:17 -05:00
2021-06-29 23:17:38 -05:00
return null !== $repository->find($value);
}
/**
2023-06-21 05:34:58 -05:00
* @param int $value
*
* @return bool
*/
2021-03-21 03:15:40 -05:00
private function validateTransaction(int $value): bool
{
2021-03-21 03:15:40 -05:00
/** @var JournalAPIRepositoryInterface $repository */
$repository = app(JournalAPIRepositoryInterface::class);
$repository->setUser(auth()->user());
2023-11-05 01:15:17 -06:00
return null !== $repository->findTransaction($value);
2019-07-31 09:53:09 -05:00
}
}