From a60ee0fdcdefbec816f5dcdec3d71b156ab4d004 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:58:09 -0500 Subject: [PATCH 1/2] call authorize() via the container --- src/DataPipes/AuthorizedDataPipe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataPipes/AuthorizedDataPipe.php b/src/DataPipes/AuthorizedDataPipe.php index 14b4d0182..e1041c0eb 100644 --- a/src/DataPipes/AuthorizedDataPipe.php +++ b/src/DataPipes/AuthorizedDataPipe.php @@ -27,7 +27,7 @@ public function handle( protected function ensureRequestIsAuthorized(string $class): void { /** @psalm-suppress UndefinedMethod */ - if (method_exists($class, 'authorize') && $class::authorize() === false) { + if (method_exists($class, 'authorize') && app()->call([$class, 'authorize']) === false) { throw new AuthorizationException(); } } From 114a1ec171d76542edc0a29bc63ab985962d738e Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Thu, 5 Dec 2024 14:19:32 -0500 Subject: [PATCH 2/2] adds test --- tests/RequestTest.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 19efbfc4b..e2733ff72 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -143,3 +143,39 @@ public static function fromRequest(Request $request) ->assertJson(['name' => 'Rick Astley']); } ); + +it('can build authorize parameters from the container', function (): void { + class SomeDependency + { + public function __construct(public string $street) + { + } + } + + app()->bind(SomeDependency::class, fn () => new SomeDependency('Sesame')); + class AuthorizeFromContainerRequest extends Data + { + public static string $street; + + public function __construct(public string $name) + { + } + + public static function authorize(SomeDependency $dependency): bool + { + self::$street = $dependency->street; + + return $dependency->street === 'Sesame'; + } + } + + Route::post('/route-with-authorization-dependencies', function (\AuthorizeFromContainerRequest $data) { + return ['name' => $data->name, 'street' => \AuthorizeFromContainerRequest::$street]; + }); + + postJson('/route-with-authorization-dependencies', [ + 'name' => 'Rick Astley', + ]) + ->assertOk() + ->assertJson(['name' => 'Rick Astley', 'street' => 'Sesame']); +});