diff --git a/src/Folklore/GraphQL/Support/Field.php b/src/Folklore/GraphQL/Support/Field.php index b730b0e3..aa3509d1 100644 --- a/src/Folklore/GraphQL/Support/Field.php +++ b/src/Folklore/GraphQL/Support/Field.php @@ -7,7 +7,6 @@ class Field extends Fluent { - /** * Override this in your queries or mutations * to provide custom authorization @@ -26,6 +25,26 @@ public function authenticated($root, $args, $context) return true; } + /** + * Message of unauthorized error + * + * @return string + */ + protected function unauthorized() + { + return 'Unauthorized'; + } + + /** + * Message of unauthenticated error + * + * @return string + */ + protected function unauthenticated() + { + return 'Unauthenticated'; + } + public function attributes() { return []; @@ -56,12 +75,12 @@ protected function getResolver() // Authenticated if (call_user_func_array($authenticate, $args) !== true) { - throw new AuthorizationError('Unauthenticated'); + throw new AuthorizationError($this->unauthenticated()); } // Authorize if (call_user_func_array($authorize, $args) !== true) { - throw new AuthorizationError('Unauthorized'); + throw new AuthorizationError($this->unauthorized()); } return call_user_func_array($resolver, $args); diff --git a/tests/GraphQLQueryTest.php b/tests/GraphQLQueryTest.php index f8a71a99..22ef5e32 100644 --- a/tests/GraphQLQueryTest.php +++ b/tests/GraphQLQueryTest.php @@ -114,6 +114,18 @@ public function testQueryAndReturnResultWithAuthorize() $this->assertEquals('Unauthorized', $result['errors'][0]['message']); } + /** + * Test query with custom authorize msg + * + * @test + */ + public function testQueryAndReturnResultWithCustomAuthorize() + { + $result = GraphQL::query($this->queries['examplesWithCustomAuthorize']); + $this->assertNull($result['data']['examplesCustomAuthorize']); + $this->assertEquals('custom', $result['errors'][0]['message']); + } + /** * Test query with authorize * @@ -126,6 +138,18 @@ public function testQueryAndReturnResultWithAuthenticated() $this->assertEquals('Unauthenticated', $result['errors'][0]['message']); } + /** + * Test query with authorize + * + * @test + */ + public function testQueryAndReturnResultWithCustomAuthenticated() + { + $result = GraphQL::query($this->queries['examplesWithCustomAuthenticated']); + $this->assertNull($result['data']['examplesCustomAuthenticated']); + $this->assertEquals('custom', $result['errors'][0]['message']); + } + /** * Test query with schema * diff --git a/tests/Objects/ExamplesCustomAuthenticatedQuery.php b/tests/Objects/ExamplesCustomAuthenticatedQuery.php new file mode 100644 index 00000000..1d1d477e --- /dev/null +++ b/tests/Objects/ExamplesCustomAuthenticatedQuery.php @@ -0,0 +1,21 @@ + 'Examples authenticate query' + ]; + + public function authenticated($root, $args, $context) + { + return false; + } + + protected function unauthenticated() + { + return 'custom'; + } +} diff --git a/tests/Objects/ExamplesCustomAuthorizeQuery.php b/tests/Objects/ExamplesCustomAuthorizeQuery.php new file mode 100644 index 00000000..d7f6badf --- /dev/null +++ b/tests/Objects/ExamplesCustomAuthorizeQuery.php @@ -0,0 +1,21 @@ + 'Examples authorize query' + ]; + + public function authorize($root, $args) + { + return false; + } + + protected function unauthorized() + { + return 'custom'; + } +} diff --git a/tests/Objects/queries.php b/tests/Objects/queries.php index 0bcebcb2..720ed1af 100644 --- a/tests/Objects/queries.php +++ b/tests/Objects/queries.php @@ -49,6 +49,14 @@ } ", + 'examplesWithCustomAuthorize' => " + query QueryExamplesCustomAuthorize { + examplesCustomAuthorize { + test + } + } + ", + 'examplesWithAuthenticated' => " query QueryExamplesAuthenticated { examplesAuthenticated { @@ -57,6 +65,14 @@ } ", + 'examplesWithCustomAuthenticated' => " + query QueryExamplesCustomAuthenticated { + examplesCustomAuthenticated { + test + } + } + ", + 'examplesWithRoot' => " query QueryExamplesRoot { examplesRoot { diff --git a/tests/TestCase.php b/tests/TestCase.php index 3ac5406b..6583fcae 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -26,7 +26,9 @@ protected function getEnvironmentSetUp($app) 'examplesContext' => ExamplesContextQuery::class, 'examplesRoot' => ExamplesRootQuery::class, 'examplesAuthorize' => ExamplesAuthorizeQuery::class, + 'examplesCustomAuthorize' => ExamplesCustomAuthorizeQuery::class, 'examplesAuthenticated' => ExamplesAuthenticatedQuery::class, + 'examplesCustomAuthenticated' => ExamplesCustomAuthenticatedQuery::class, 'examplesPagination' => ExamplesPaginationQuery::class, ], 'mutation' => [