From 881fe3e6e7cc97d3b518b3b33601bca6888219b9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 29 Jan 2023 07:10:11 +0100 Subject: [PATCH] Switch to validation style error in API --- .../Models/TransactionCurrency/DestroyController.php | 11 ++++++++--- .../Models/TransactionLinkType/StoreController.php | 6 +++++- .../Models/TransactionLinkType/UpdateController.php | 5 ++++- .../V1/Controllers/System/ConfigurationController.php | 5 ++++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/Api/V1/Controllers/Models/TransactionCurrency/DestroyController.php b/app/Api/V1/Controllers/Models/TransactionCurrency/DestroyController.php index 3effd85202..ee90fdba7d 100644 --- a/app/Api/V1/Controllers/Models/TransactionCurrency/DestroyController.php +++ b/app/Api/V1/Controllers/Models/TransactionCurrency/DestroyController.php @@ -31,6 +31,7 @@ use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; use Illuminate\Http\JsonResponse; +use Validator; /** * Class DestroyController @@ -75,16 +76,20 @@ class DestroyController extends Controller { /** @var User $admin */ $admin = auth()->user(); + $rules = ['currency_code' => 'required']; if (!$this->userRepository->hasRole($admin, 'owner')) { // access denied: - throw new FireflyException('200005: You need the "owner" role to do this.'); + $messages = ['currency_code' => '200005: You need the "owner" role to do this.']; + Validator::make([], $rules, $messages)->validate(); } if ($this->repository->currencyInUse($currency)) { - throw new FireflyException('200006: Currency in use.'); + $messages = ['currency_code' => '200006: Currency in use.']; + Validator::make([], $rules, $messages)->validate(); } if ($this->repository->isFallbackCurrency($currency)) { - throw new FireflyException('200026: Currency is fallback.'); + $messages = ['currency_code' => '200026: Currency is fallback.']; + Validator::make([], $rules, $messages)->validate(); } $this->repository->destroy($currency); diff --git a/app/Api/V1/Controllers/Models/TransactionLinkType/StoreController.php b/app/Api/V1/Controllers/Models/TransactionLinkType/StoreController.php index abd1a748e7..5b54dae645 100644 --- a/app/Api/V1/Controllers/Models/TransactionLinkType/StoreController.php +++ b/app/Api/V1/Controllers/Models/TransactionLinkType/StoreController.php @@ -34,6 +34,7 @@ use FireflyIII\Transformers\LinkTypeTransformer; use FireflyIII\User; use Illuminate\Http\JsonResponse; use League\Fractal\Resource\Item; +use Validator; /** * Class StoreController @@ -81,9 +82,12 @@ class StoreController extends Controller { /** @var User $admin */ $admin = auth()->user(); + $rules = ['name' => 'required']; if (!$this->userRepository->hasRole($admin, 'owner')) { - throw new FireflyException('200005: You need the "owner" role to do this.'); + // access denied: + $messages = ['name' => '200005: You need the "owner" role to do this.']; + Validator::make([], $rules, $messages)->validate(); } $data = $request->getAll(); // if currency ID is 0, find the currency by the code: diff --git a/app/Api/V1/Controllers/Models/TransactionLinkType/UpdateController.php b/app/Api/V1/Controllers/Models/TransactionLinkType/UpdateController.php index ddd55e6688..e48fa2ac6d 100644 --- a/app/Api/V1/Controllers/Models/TransactionLinkType/UpdateController.php +++ b/app/Api/V1/Controllers/Models/TransactionLinkType/UpdateController.php @@ -35,6 +35,7 @@ use FireflyIII\Transformers\LinkTypeTransformer; use FireflyIII\User; use Illuminate\Http\JsonResponse; use League\Fractal\Resource\Item; +use Validator; /** * Class UpdateController @@ -87,9 +88,11 @@ class UpdateController extends Controller /** @var User $admin */ $admin = auth()->user(); + $rules = ['name' => 'required']; if (!$this->userRepository->hasRole($admin, 'owner')) { - throw new FireflyException('200005: You need the "owner" role to do this.'); + $messages = ['name' => '200005: You need the "owner" role to do this.']; + Validator::make([], $rules, $messages)->validate(); } $data = $request->getAll(); diff --git a/app/Api/V1/Controllers/System/ConfigurationController.php b/app/Api/V1/Controllers/System/ConfigurationController.php index eb5f4437fd..09a8dff8b7 100644 --- a/app/Api/V1/Controllers/System/ConfigurationController.php +++ b/app/Api/V1/Controllers/System/ConfigurationController.php @@ -32,6 +32,7 @@ use Illuminate\Http\JsonResponse; use Log; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Validator; /** * Class ConfigurationController @@ -174,8 +175,10 @@ class ConfigurationController extends Controller */ public function update(UpdateRequest $request, string $name): JsonResponse { + $rules = ['value' => 'required']; if (!$this->repository->hasRole(auth()->user(), 'owner')) { - throw new FireflyException('200005: You need the "owner" role to do this.'); + $messages = ['value' => '200005: You need the "owner" role to do this.']; + Validator::make([], $rules, $messages)->validate(); } $data = $request->getAll(); $shortName = str_replace('configuration.', '', $name);