Skip to content

Commit 2d9ea73

Browse files
committed
Validate arguments before passing to authorize
Fixes #407
1 parent 850a701 commit 2d9ea73

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ CHANGELOG
4343
- Replace global helper `is_lumen` with static class call `\Rebing\GraphQL\Helpers::isLumen`
4444

4545
### Fixed
46+
- Arguments are now validation before they're passed to `authorize()` [\#413](https://github.com/rebing/graphql-laravel/pull/413)
4647
- File uploads now correctly work with batched requests [\#397](https://github.com/rebing/graphql-laravel/pull/397)
4748
- Path multi-level support for Schemas works again [\#358](https://github.com/rebing/graphql-laravel/pull/358)
4849
- SelectFields correctly passes field arguments to the custom query [\#327](https://github.com/rebing/graphql-laravel/pull/327)

src/Support/Field.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,6 @@ protected function getResolver(): ?Closure
167167
$arguments[1] = array_merge($arguments[1], $arguments[2]);
168168
}
169169

170-
// Authorize
171-
if (call_user_func($authorize, $arguments[1]) != true) {
172-
throw new AuthorizationError('Unauthorized');
173-
}
174-
175170
// Validate mutation arguments
176171
if (method_exists($this, 'getRules')) {
177172
$args = Arr::get($arguments, 1, []);
@@ -188,6 +183,11 @@ protected function getResolver(): ?Closure
188183
}
189184
}
190185

186+
// Authorize
187+
if (call_user_func($authorize, $arguments[1]) != true) {
188+
throw new AuthorizationError('Unauthorized');
189+
}
190+
191191
// Add the 'selects and relations' feature as 5th arg
192192
if (isset($arguments[3])) {
193193
$arguments[] = function () use ($arguments): SelectFields {

tests/Unit/ValidationAuthorizationTests/ValidationAuthorizationTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rebing\GraphQL\Tests\Unit\ValidationAuthorizationTests;
66

7+
use Illuminate\Support\MessageBag;
78
use Rebing\GraphQL\Tests\TestCase;
89

910
class ValidationAuthorizationTest extends TestCase
@@ -23,7 +24,16 @@ public function testAuthorizeArgumentsInvalid(): void
2324
],
2425
]);
2526

26-
$this->assertSame('Unauthorized', $result['errors'][0]['message']);
27+
$this->assertSame('validation', $result['errors'][0]['message']);
28+
29+
/** @var MessageBag $messageBag */
30+
$messageBag = $result['errors'][0]['extensions']['validation'];
31+
$expectedErrors = [
32+
'arg1' => [
33+
'The selected arg1 is invalid.',
34+
],
35+
];
36+
$this->assertSame($expectedErrors, $messageBag->messages());
2737
}
2838

2939
public function testAuthorizeArgumentsValid(): void

0 commit comments

Comments
 (0)