. */ declare(strict_types=1); namespace FireflyIII\Rules; use Closure; use FireflyIII\Models\Account; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\Category; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\Tag; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface; use Illuminate\Contracts\Validation\ValidationRule; /** * Class IsValidAttachmentModel */ class IsValidAttachmentModel implements ValidationRule { /** @var string */ private string $model; /** * IsValidAttachmentModel constructor. * * * @param string $model */ public function __construct(string $model) { $model = $this->normalizeModel($model); $this->model = $model; } /** * @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); } /** * @param string $attribute * @param mixed $value * @param Closure $fail * * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { if (!auth()->check()) { $fail('validation.model_id_invalid')->translate(); return; } $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', ]; if (!array_key_exists($this->model, $methods)) { app('log')->error(sprintf('Cannot validate model "%s" in %s.', substr($this->model, 0, 20), __METHOD__)); $fail('validation.model_id_invalid')->translate(); return; } $method = $methods[$this->model]; $result = $this->$method((int)$value); // @phpstan-ignore-line if(false === $result) { $fail('validation.model_id_invalid')->translate(); } } /** * @param int $value * * @return bool */ private function validateAccount(int $value): bool { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validateBill(int $value): bool { /** @var BillRepositoryInterface $repository */ $repository = app(BillRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validateBudget(int $value): bool { /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validateCategory(int $value): bool { /** @var CategoryRepositoryInterface $repository */ $repository = app(CategoryRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validateJournal(int $value): bool { $repository = app(JournalRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validatePiggyBank(int $value): bool { /** @var PiggyBankRepositoryInterface $repository */ $repository = app(PiggyBankRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validateTag(int $value): bool { /** @var TagRepositoryInterface $repository */ $repository = app(TagRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->find($value); } /** * @param int $value * * @return bool */ private function validateTransaction(int $value): bool { /** @var JournalAPIRepositoryInterface $repository */ $repository = app(JournalAPIRepositoryInterface::class); $repository->setUser(auth()->user()); return null !== $repository->findTransaction($value); } }