From 49e6d90a5c2d30fd0a1a08b0b4542a7a69780441 Mon Sep 17 00:00:00 2001 From: Christopher Gammie <4464333+lindyhopchris@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:16:42 +0000 Subject: [PATCH] feat!: support auth responses on authorizer contract (#21) This reverts commit 9ab61d564811003eeee260fa3b80e1a64b9bd635. --- composer.json | 1 + src/Contracts/Auth/Authorizer.php | 41 ++++++++++++++++--------------- src/Core/Auth/Authorizer.php | 39 +++++++++++++++-------------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/composer.json b/composer.json index 16e6df1..0098432 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "require": { "php": "^8.2", "ext-json": "*", + "illuminate/auth": "^11.33", "illuminate/contracts": "^11.0", "illuminate/http": "^11.0", "illuminate/support": "^11.0" diff --git a/src/Contracts/Auth/Authorizer.php b/src/Contracts/Auth/Authorizer.php index 8c83b4d..2378e30 100644 --- a/src/Contracts/Auth/Authorizer.php +++ b/src/Contracts/Auth/Authorizer.php @@ -11,6 +11,7 @@ namespace LaravelJsonApi\Contracts\Auth; +use Illuminate\Auth\Access\Response; use Illuminate\Http\Request; interface Authorizer @@ -20,45 +21,45 @@ interface Authorizer * * @param Request $request * @param string $modelClass - * @return bool + * @return bool|Response */ - public function index(Request $request, string $modelClass): bool; + public function index(Request $request, string $modelClass): bool|Response; /** * Authorize the store controller action. * * @param Request $request * @param string $modelClass - * @return bool + * @return bool|Response */ - public function store(Request $request, string $modelClass): bool; + public function store(Request $request, string $modelClass): bool|Response; /** * Authorize the show controller action. * * @param Request $request * @param object $model - * @return bool + * @return bool|Response */ - public function show(Request $request, object $model): bool; + public function show(Request $request, object $model): bool|Response; /** * Authorize the update controller action. * * @param object $model * @param Request $request - * @return bool + * @return bool|Response */ - public function update(Request $request, object $model): bool; + public function update(Request $request, object $model): bool|Response; /** * Authorize the destroy controller action. * * @param Request $request * @param object $model - * @return bool + * @return bool|Response */ - public function destroy(Request $request, object $model): bool; + public function destroy(Request $request, object $model): bool|Response; /** * Authorize the show-related controller action. @@ -66,9 +67,9 @@ public function destroy(Request $request, object $model): bool; * @param Request $request * @param object $model * @param string $fieldName - * @return bool + * @return bool|Response */ - public function showRelated(Request $request, object $model, string $fieldName): bool; + public function showRelated(Request $request, object $model, string $fieldName): bool|Response; /** * Authorize the show-relationship controller action. @@ -76,9 +77,9 @@ public function showRelated(Request $request, object $model, string $fieldName): * @param Request $request * @param object $model * @param string $fieldName - * @return bool + * @return bool|Response */ - public function showRelationship(Request $request, object $model, string $fieldName): bool; + public function showRelationship(Request $request, object $model, string $fieldName): bool|Response; /** * Authorize the update-relationship controller action. @@ -86,9 +87,9 @@ public function showRelationship(Request $request, object $model, string $fieldN * @param Request $request * @param object $model * @param string $fieldName - * @return bool + * @return bool|Response */ - public function updateRelationship(Request $request, object $model, string $fieldName): bool; + public function updateRelationship(Request $request, object $model, string $fieldName): bool|Response; /** * Authorize the attach-relationship controller action. @@ -96,9 +97,9 @@ public function updateRelationship(Request $request, object $model, string $fiel * @param Request $request * @param object $model * @param string $fieldName - * @return bool + * @return bool|Response */ - public function attachRelationship(Request $request, object $model, string $fieldName): bool; + public function attachRelationship(Request $request, object $model, string $fieldName): bool|Response; /** * Authorize the detach-relationship controller action. @@ -106,7 +107,7 @@ public function attachRelationship(Request $request, object $model, string $fiel * @param Request $request * @param object $model * @param string $fieldName - * @return bool + * @return bool|Response */ - public function detachRelationship(Request $request, object $model, string $fieldName): bool; + public function detachRelationship(Request $request, object $model, string $fieldName): bool|Response; } diff --git a/src/Core/Auth/Authorizer.php b/src/Core/Auth/Authorizer.php index 31b4a8d..2347752 100644 --- a/src/Core/Auth/Authorizer.php +++ b/src/Core/Auth/Authorizer.php @@ -12,6 +12,7 @@ namespace LaravelJsonApi\Core\Auth; use Illuminate\Contracts\Auth\Access\Gate; +use Illuminate\Auth\Access\Response; use Illuminate\Http\Request; use LaravelJsonApi\Contracts\Auth\Authorizer as AuthorizerContract; use LaravelJsonApi\Contracts\Schema\Schema; @@ -47,10 +48,10 @@ public function __construct(Gate $gate, JsonApiService $service) /** * @inheritDoc */ - public function index(Request $request, string $modelClass): bool + public function index(Request $request, string $modelClass): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'viewAny', $modelClass ); @@ -62,10 +63,10 @@ public function index(Request $request, string $modelClass): bool /** * @inheritDoc */ - public function store(Request $request, string $modelClass): bool + public function store(Request $request, string $modelClass): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'create', $modelClass ); @@ -77,10 +78,10 @@ public function store(Request $request, string $modelClass): bool /** * @inheritDoc */ - public function show(Request $request, object $model): bool + public function show(Request $request, object $model): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'view', $model ); @@ -92,10 +93,10 @@ public function show(Request $request, object $model): bool /** * @inheritDoc */ - public function update(Request $request, object $model): bool + public function update(Request $request, object $model): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'update', $model ); @@ -107,10 +108,10 @@ public function update(Request $request, object $model): bool /** * @inheritDoc */ - public function destroy(Request $request, object $model): bool + public function destroy(Request $request, object $model): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'delete', $model ); @@ -122,10 +123,10 @@ public function destroy(Request $request, object $model): bool /** * @inheritDoc */ - public function showRelated(Request $request, object $model, string $fieldName): bool + public function showRelated(Request $request, object $model, string $fieldName): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'view' . Str::classify($fieldName), $model ); @@ -137,7 +138,7 @@ public function showRelated(Request $request, object $model, string $fieldName): /** * @inheritDoc */ - public function showRelationship(Request $request, object $model, string $fieldName): bool + public function showRelationship(Request $request, object $model, string $fieldName): bool|Response { return $this->showRelated($request, $model, $fieldName); } @@ -145,10 +146,10 @@ public function showRelationship(Request $request, object $model, string $fieldN /** * @inheritDoc */ - public function updateRelationship(Request $request, object $model, string $fieldName): bool + public function updateRelationship(Request $request, object $model, string $fieldName): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'update' . Str::classify($fieldName), [$model, $this->createRelation($request, $fieldName)] ); @@ -160,10 +161,10 @@ public function updateRelationship(Request $request, object $model, string $fiel /** * @inheritDoc */ - public function attachRelationship(Request $request, object $model, string $fieldName): bool + public function attachRelationship(Request $request, object $model, string $fieldName): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'attach' . Str::classify($fieldName), [$model, $this->createRelation($request, $fieldName)] ); @@ -175,10 +176,10 @@ public function attachRelationship(Request $request, object $model, string $fiel /** * @inheritDoc */ - public function detachRelationship(Request $request, object $model, string $fieldName): bool + public function detachRelationship(Request $request, object $model, string $fieldName): bool|Response { if ($this->mustAuthorize()) { - return $this->gate->check( + return $this->gate->inspect( 'detach' . Str::classify($fieldName), [$model, $this->createRelation($request, $fieldName)] );