From c7eab7873ea81fb51d930a53d92085b585fde0be Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 18 Jul 2020 08:42:13 +0200 Subject: [PATCH] Clean up requests --- app/Api/V1/Requests/UserStoreRequest.php | 18 +----------------- app/Api/V1/Requests/UserUpdateRequest.php | 16 +--------------- app/Http/Requests/AccountFormRequest.php | 3 +++ app/Http/Requests/AttachmentFormRequest.php | 3 +++ app/Http/Requests/BillStoreRequest.php | 2 ++ app/Http/Requests/BillUpdateRequest.php | 2 ++ app/Http/Requests/BudgetFormStoreRequest.php | 3 +++ app/Http/Requests/BudgetFormUpdateRequest.php | 3 +++ app/Http/Requests/CategoryFormRequest.php | 2 ++ app/Http/Requests/CurrencyFormRequest.php | 2 ++ app/Http/Requests/JournalLinkRequest.php | 2 ++ app/Http/Requests/PiggyBankStoreRequest.php | 2 ++ app/Http/Requests/PiggyBankUpdateRequest.php | 2 ++ .../Requests/ReconciliationStoreRequest.php | 2 ++ app/Http/Requests/RecurrenceFormRequest.php | 3 ++- app/Http/Requests/Request.php | 19 ------------------- app/Http/Requests/RuleFormRequest.php | 2 ++ app/Http/Requests/RuleGroupFormRequest.php | 2 ++ app/Http/Requests/TagFormRequest.php | 3 +++ app/Http/Requests/UserFormRequest.php | 2 ++ app/Support/Request/ConvertsDataTypes.php | 19 +++++++++++++++++++ 21 files changed, 60 insertions(+), 52 deletions(-) diff --git a/app/Api/V1/Requests/UserStoreRequest.php b/app/Api/V1/Requests/UserStoreRequest.php index 16901df1e8..cff3d97e47 100644 --- a/app/Api/V1/Requests/UserStoreRequest.php +++ b/app/Api/V1/Requests/UserStoreRequest.php @@ -24,10 +24,8 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Requests; -use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Rules\IsBoolean; use FireflyIII\Support\Request\ConvertsDataTypes; -use FireflyIII\User; use Illuminate\Foundation\Http\FormRequest; @@ -44,21 +42,7 @@ class UserStoreRequest extends FormRequest */ public function authorize(): bool { - $result = false; - // Only allow authenticated users - if (auth()->check()) { - /** @var User $user */ - $user = auth()->user(); - - /** @var UserRepositoryInterface $repository */ - $repository = app(UserRepositoryInterface::class); - - if ($repository->hasRole($user, 'owner')) { - $result = true; // @codeCoverageIgnore - } - } - - return $result; + return auth()->check() && auth()->user()->hasRole('owner'); } /** diff --git a/app/Api/V1/Requests/UserUpdateRequest.php b/app/Api/V1/Requests/UserUpdateRequest.php index f4f4524bd5..4931700eec 100644 --- a/app/Api/V1/Requests/UserUpdateRequest.php +++ b/app/Api/V1/Requests/UserUpdateRequest.php @@ -44,21 +44,7 @@ class UserUpdateRequest extends FormRequest */ public function authorize(): bool { - $result = false; - // Only allow authenticated users - if (auth()->check()) { - /** @var User $user */ - $user = auth()->user(); - - /** @var UserRepositoryInterface $repository */ - $repository = app(UserRepositoryInterface::class); - - if ($repository->hasRole($user, 'owner')) { - $result = true; // @codeCoverageIgnore - } - } - - return $result; + return auth()->check() && auth()->user()->hasRole('owner'); } /** diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php index a5829d29d0..3dab35d4c3 100644 --- a/app/Http/Requests/AccountFormRequest.php +++ b/app/Http/Requests/AccountFormRequest.php @@ -25,6 +25,8 @@ namespace FireflyIII\Http\Requests; use FireflyIII\Models\Account; use FireflyIII\Models\Location; use FireflyIII\Rules\UniqueIban; +use FireflyIII\Support\Request\AppendsLocationData; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -32,6 +34,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class AccountFormRequest extends FormRequest { + use ConvertsDataTypes, AppendsLocationData; /** * Verify the request. * diff --git a/app/Http/Requests/AttachmentFormRequest.php b/app/Http/Requests/AttachmentFormRequest.php index 3188d39968..5b62e3bf48 100644 --- a/app/Http/Requests/AttachmentFormRequest.php +++ b/app/Http/Requests/AttachmentFormRequest.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -31,6 +32,8 @@ use Illuminate\Foundation\Http\FormRequest; */ class AttachmentFormRequest extends FormRequest { + use ConvertsDataTypes; + /** * Verify the request. * diff --git a/app/Http/Requests/BillStoreRequest.php b/app/Http/Requests/BillStoreRequest.php index 69447bc961..4db4af0805 100644 --- a/app/Http/Requests/BillStoreRequest.php +++ b/app/Http/Requests/BillStoreRequest.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -29,6 +30,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class BillStoreRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/BillUpdateRequest.php b/app/Http/Requests/BillUpdateRequest.php index a895a450e3..e0e713bb35 100644 --- a/app/Http/Requests/BillUpdateRequest.php +++ b/app/Http/Requests/BillUpdateRequest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\Bill; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -30,6 +31,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class BillUpdateRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/BudgetFormStoreRequest.php b/app/Http/Requests/BudgetFormStoreRequest.php index 7f9216f3e8..22aa0733b9 100644 --- a/app/Http/Requests/BudgetFormStoreRequest.php +++ b/app/Http/Requests/BudgetFormStoreRequest.php @@ -22,6 +22,8 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; +use FireflyIII\Support\Request\ConvertsDataTypes; +use FireflyIII\Validation\AutoBudget\ValidatesAutoBudgetRequest; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Validator; @@ -31,6 +33,7 @@ use Illuminate\Validation\Validator; */ class BudgetFormStoreRequest extends FormRequest { + use ConvertsDataTypes, ValidatesAutoBudgetRequest; /** * Verify the request. * diff --git a/app/Http/Requests/BudgetFormUpdateRequest.php b/app/Http/Requests/BudgetFormUpdateRequest.php index 89db4acdf0..64dac08cf4 100644 --- a/app/Http/Requests/BudgetFormUpdateRequest.php +++ b/app/Http/Requests/BudgetFormUpdateRequest.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\Budget; +use FireflyIII\Support\Request\ConvertsDataTypes; +use FireflyIII\Validation\AutoBudget\ValidatesAutoBudgetRequest; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Validator; @@ -32,6 +34,7 @@ use Illuminate\Validation\Validator; */ class BudgetFormUpdateRequest extends FormRequest { + use ConvertsDataTypes, ValidatesAutoBudgetRequest; /** * Verify the request. * diff --git a/app/Http/Requests/CategoryFormRequest.php b/app/Http/Requests/CategoryFormRequest.php index 71ff9b6f9c..54b92655c2 100644 --- a/app/Http/Requests/CategoryFormRequest.php +++ b/app/Http/Requests/CategoryFormRequest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\Category; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -30,6 +31,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class CategoryFormRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/CurrencyFormRequest.php b/app/Http/Requests/CurrencyFormRequest.php index 2556835cb3..fce35b6705 100644 --- a/app/Http/Requests/CurrencyFormRequest.php +++ b/app/Http/Requests/CurrencyFormRequest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -30,6 +31,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class CurrencyFormRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/JournalLinkRequest.php b/app/Http/Requests/JournalLinkRequest.php index da9283aefa..3536814439 100644 --- a/app/Http/Requests/JournalLinkRequest.php +++ b/app/Http/Requests/JournalLinkRequest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\LinkType; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -31,6 +32,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class JournalLinkRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/PiggyBankStoreRequest.php b/app/Http/Requests/PiggyBankStoreRequest.php index 4311a814fc..0df3c35fb2 100644 --- a/app/Http/Requests/PiggyBankStoreRequest.php +++ b/app/Http/Requests/PiggyBankStoreRequest.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -29,6 +30,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class PiggyBankStoreRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/PiggyBankUpdateRequest.php b/app/Http/Requests/PiggyBankUpdateRequest.php index a070cd5337..b606787d62 100644 --- a/app/Http/Requests/PiggyBankUpdateRequest.php +++ b/app/Http/Requests/PiggyBankUpdateRequest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\PiggyBank; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -30,6 +31,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class PiggyBankUpdateRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/ReconciliationStoreRequest.php b/app/Http/Requests/ReconciliationStoreRequest.php index d9fae73ce6..47b3df082f 100644 --- a/app/Http/Requests/ReconciliationStoreRequest.php +++ b/app/Http/Requests/ReconciliationStoreRequest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Rules\ValidJournals; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; use Log; @@ -32,6 +33,7 @@ use Log; */ class ReconciliationStoreRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/RecurrenceFormRequest.php b/app/Http/Requests/RecurrenceFormRequest.php index 2600a0d0d8..180fe2fe86 100644 --- a/app/Http/Requests/RecurrenceFormRequest.php +++ b/app/Http/Requests/RecurrenceFormRequest.php @@ -29,6 +29,7 @@ use FireflyIII\Models\Recurrence; use FireflyIII\Models\TransactionType; use FireflyIII\Rules\ValidRecurrenceRepetitionType; use FireflyIII\Rules\ValidRecurrenceRepetitionValue; +use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Validation\AccountValidator; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Validator; @@ -39,7 +40,7 @@ use Log; */ class RecurrenceFormRequest extends FormRequest { - + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php index 3602945c41..576b83d2dc 100644 --- a/app/Http/Requests/Request.php +++ b/app/Http/Requests/Request.php @@ -38,25 +38,6 @@ use Log; */ class Request extends FormRequest { - use ConvertsDataTypes; - - - /** - * Return floating value. - * - * @param string $field - * - * @return float|null - */ - public function float(string $field): ?float - { - $res = $this->get($field); - if (null === $res) { - return null; - } - - return (float) $res; - } diff --git a/app/Http/Requests/RuleFormRequest.php b/app/Http/Requests/RuleFormRequest.php index c6bab76dab..ba37873a10 100644 --- a/app/Http/Requests/RuleFormRequest.php +++ b/app/Http/Requests/RuleFormRequest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\Rule; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -30,6 +31,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class RuleFormRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/RuleGroupFormRequest.php b/app/Http/Requests/RuleGroupFormRequest.php index ea17138f4c..9aaf2095b8 100644 --- a/app/Http/Requests/RuleGroupFormRequest.php +++ b/app/Http/Requests/RuleGroupFormRequest.php @@ -24,6 +24,7 @@ namespace FireflyIII\Http\Requests; use FireflyIII\Models\RuleGroup; use FireflyIII\Rules\IsBoolean; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -31,6 +32,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class RuleGroupFormRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Http/Requests/TagFormRequest.php b/app/Http/Requests/TagFormRequest.php index eb21b8fb97..91de7bb05f 100644 --- a/app/Http/Requests/TagFormRequest.php +++ b/app/Http/Requests/TagFormRequest.php @@ -24,6 +24,8 @@ namespace FireflyIII\Http\Requests; use FireflyIII\Models\Location; use FireflyIII\Models\Tag; +use FireflyIII\Support\Request\AppendsLocationData; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -31,6 +33,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class TagFormRequest extends FormRequest { + use ConvertsDataTypes, AppendsLocationData; /** * Verify the request. * diff --git a/app/Http/Requests/UserFormRequest.php b/app/Http/Requests/UserFormRequest.php index 1e850aff24..1a34382cff 100644 --- a/app/Http/Requests/UserFormRequest.php +++ b/app/Http/Requests/UserFormRequest.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Requests; +use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** @@ -31,6 +32,7 @@ use Illuminate\Foundation\Http\FormRequest; */ class UserFormRequest extends FormRequest { + use ConvertsDataTypes; /** * Verify the request. * diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php index d8e4a23683..b2f73609f2 100644 --- a/app/Support/Request/ConvertsDataTypes.php +++ b/app/Support/Request/ConvertsDataTypes.php @@ -64,6 +64,25 @@ trait ConvertsDataTypes return (int) $this->get($field); } + + /** + * Return floating value. + * + * @param string $field + * + * @return float|null + */ + protected function float(string $field): ?float + { + $res = $this->get($field); + if (null === $res) { + return null; + } + + return (float) $res; + } + + /** * Parse and clean a string, but keep the newlines. *