Skip to content

Commit

Permalink
Merge pull request #910 from cosmastech/patch-2
Browse files Browse the repository at this point in the history
Use container to call `Data::authorize()` to allow for dependencies
  • Loading branch information
rubenvanassche authored Jan 17, 2025
2 parents 6f23159 + 114a1ec commit c7807b7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DataPipes/AuthorizedDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});

0 comments on commit c7807b7

Please sign in to comment.