Skip to content

Commit

Permalink
[Feature] Add show-related method to the authorizer interface
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
lindyhopchris committed Jun 18, 2021
1 parent b8faaca commit 4c5b636
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
All notable changes to this project will be documented in this file. This project adheres to
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).

## Unreleased

### Added

- [#6](https://github.com/laravel-json-api/core/issues/6) The authorizer contract now has a `showRelated` method to
authorize the show-related controller action. Previously the `showRelationship` method was used to authorize both the
show-related and show-relationship controller actions. This change means that authorizers can implement different
authorization logic if needed. However, our default authorizer (the `Auth\Authorizer` class) remains unchanged in that
both actions expect there to be a `view<RelationshipName>` method on the policy to authorize these actions.

### Changed

- The `Auth\Authorizer` class is no longer `final` and can now be extended if needed.

## [1.0.0-beta.4] - 2021-06-02

### Changed
Expand Down
12 changes: 11 additions & 1 deletion src/Contracts/Auth/Authorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ public function update(Request $request, object $model): bool;
public function destroy(Request $request, object $model): bool;

/**
* Authorize the show-related and show-relationship controller action.
* Authorize the show-related controller action.
*
* @param Request $request
* @param object $model
* @param string $fieldName
* @return bool
*/
public function showRelated(Request $request, object $model, string $fieldName): bool;

/**
* Authorize the show-relationship controller action.
*
* @param Request $request
* @param object $model
Expand Down
12 changes: 10 additions & 2 deletions src/Core/Auth/Authorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use LaravelJsonApi\Core\Store\LazyRelation;
use LaravelJsonApi\Core\Support\Str;

final class Authorizer implements AuthorizerContract
class Authorizer implements AuthorizerContract
{

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ public function destroy(Request $request, object $model): bool
/**
* @inheritDoc
*/
public function showRelationship(Request $request, object $model, string $fieldName): bool
public function showRelated(Request $request, object $model, string $fieldName): bool
{
if ($this->mustAuthorize()) {
return $this->gate->check(
Expand All @@ -142,6 +142,14 @@ public function showRelationship(Request $request, object $model, string $fieldN
return true;
}

/**
* @inheritDoc
*/
public function showRelationship(Request $request, object $model, string $fieldName): bool
{
return $this->showRelated($request, $model, $fieldName);
}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit 4c5b636

Please sign in to comment.