Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Commands/stubs/policy.stub
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ class DummyClass
* @param \App\User $user
* @return mixed
*/
public function showEvery(User $user = null)
{
//
}

/**
* Determine whether the user is authorized to access the repository uriKey
*
* @param \App\User $user
* @return mixed
*/
public function showAny(User $user = null)
{
//
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function repository($key = null)
}

if (! $repository::authorizedToShowAny($this)) {
throw new UnauthorizedException(__('Unauthorized to view repository :name.', [
throw new UnauthorizedException(__('Unauthorized to view repository :name. See "showAny" policy.', [
'name' => $repository,
]), 403);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Repositories/Crudable.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function index(RestifyRequest $request)
});

try {
$this->allowToViewAny($request, $items);
$this->allowToShowEvery($request, $items);
} catch (UnauthorizedException | AuthorizationException $e) {
return $this->response()->forbidden()->addError($e->getMessage());
}
Expand Down Expand Up @@ -203,9 +203,9 @@ public function allowToShow($request)
* @param Collection $items
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function allowToViewAny($request, Collection $items)
public function allowToShowEvery($request, Collection $items)
{
$this->authorizeToShowAny($request);
$this->authorizeToShowEvery($request);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Traits/AuthorizableModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ public static function authorizedToShowAny(Request $request)
: true;
}

/**
* Determine if the resource should be available for the given request (.
*
* @param \Illuminate\Http\Request $request
* @return void
* @throws AuthorizationException
*/
public function authorizeToShowEvery(Request $request)
{
if (! static::authorizable()) {
return;
}

if (method_exists(Gate::getPolicyFor(static::newModel()), 'showEvery')) {
$this->authorizeTo($request, 'showEvery');
}
}

/**
* Determine if the resource should be available for the given request.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
public static function authorizedToShowEvery(Request $request)
{
if (! static::authorizable()) {
return true;
}

return method_exists(Gate::getPolicyFor(static::newModel()), 'showEvery')
? Gate::check('showEvery', get_class(static::newModel()))
: true;
}

/**
* Determine if the current user can view the given resource or throw.
*
Expand Down