Skip to content

Commit f9e45fb

Browse files
committed
Adding filter every policy
1 parent f2f375c commit f9e45fb

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

src/Commands/stubs/policy.stub

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ class DummyClass
1616
* @param \App\User $user
1717
* @return mixed
1818
*/
19+
public function showEvery(User $user = null)
20+
{
21+
//
22+
}
23+
24+
/**
25+
* Determine whether the user is authorized to access the repository uriKey
26+
*
27+
* @param \App\User $user
28+
* @return mixed
29+
*/
1930
public function showAny(User $user = null)
2031
{
2132
//

src/Http/Requests/InteractWithRepositories.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public function repository($key = null)
4343
]), 404);
4444
}
4545

46-
if (! $repository::authorizedToShowRepository($this)) {
47-
throw new UnauthorizedException(__('Unauthorized to view repository :name.', [
46+
if (! $repository::authorizedToShowAny($this)) {
47+
throw new UnauthorizedException(__('Unauthorized to view repository :name. See "showAny" policy.', [
4848
'name' => $repository,
4949
]), 403);
5050
}

src/Repositories/Crudable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function index(RestifyRequest $request)
4747
});
4848

4949
try {
50-
$this->allowToViewAny($request, $items);
50+
$this->allowToShowEvery($request, $items);
5151
} catch (UnauthorizedException | AuthorizationException $e) {
5252
return $this->response()->forbidden()->addError($e->getMessage());
5353
}
@@ -203,9 +203,9 @@ public function allowToShow($request)
203203
* @param Collection $items
204204
* @throws \Illuminate\Auth\Access\AuthorizationException
205205
*/
206-
public function allowToViewAny($request, Collection $items)
206+
public function allowToShowEvery($request, Collection $items)
207207
{
208-
$this->authorizeToShowAny($request);
208+
$this->authorizeToShowEvery($request);
209209
}
210210

211211
/**

src/Traits/AuthorizableModels.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,37 @@ public static function authorizedToShowAny(Request $request)
7070
}
7171

7272
/**
73-
* Determine if the repository url is available
73+
* Determine if the resource should be available for the given request (
74+
*
75+
* @param \Illuminate\Http\Request $request
76+
* @return void
77+
* @throws AuthorizationException
78+
*/
79+
public function authorizeToShowEvery(Request $request)
80+
{
81+
if (! static::authorizable()) {
82+
return;
83+
}
84+
85+
if (method_exists(Gate::getPolicyFor(static::newModel()), 'showEvery')) {
86+
$this->authorizeTo($request, 'showEvery');
87+
}
88+
}
89+
90+
/**
91+
* Determine if the resource should be available for the given request.
7492
*
7593
* @param \Illuminate\Http\Request $request
7694
* @return bool
7795
*/
78-
public static function authorizedToShowRepository(Request $request)
96+
public static function authorizedToShowEvery(Request $request)
7997
{
8098
if (! static::authorizable()) {
8199
return true;
82100
}
83101

84-
return method_exists(Gate::getPolicyFor(static::newModel()), 'showRepository')
85-
? Gate::check('showRepository', get_class(static::newModel()))
102+
return method_exists(Gate::getPolicyFor(static::newModel()), 'showEvery')
103+
? Gate::check('showEvery', get_class(static::newModel()))
86104
: true;
87105
}
88106

0 commit comments

Comments
 (0)