diff --git a/src/DataPipes/AuthorizedDataPipe.php b/src/DataPipes/AuthorizedDataPipe.php index 14b4d018..e1041c0e 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(); } } diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 19efbfc4..e2733ff7 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']); +});