Skip to content

Commit a2b00d6

Browse files
author
Lupacescu Eduard
authored
Merge pull request #123 from binaryk/authorize-repository
Authorize repository
2 parents 255c72c + 702aa07 commit a2b00d6

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function repository($key = null)
4444
}
4545

4646
if (! $repository::authorizedToShowAny($this)) {
47-
throw new UnauthorizedException(__('Unauthorized to view repository :name.', [
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ public static function authorizedToShowAny(Request $request)
6969
: true;
7070
}
7171

72+
/**
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.
92+
*
93+
* @param \Illuminate\Http\Request $request
94+
* @return bool
95+
*/
96+
public static function authorizedToShowEvery(Request $request)
97+
{
98+
if (! static::authorizable()) {
99+
return true;
100+
}
101+
102+
return method_exists(Gate::getPolicyFor(static::newModel()), 'showEvery')
103+
? Gate::check('showEvery', get_class(static::newModel()))
104+
: true;
105+
}
106+
72107
/**
73108
* Determine if the current user can view the given resource or throw.
74109
*

0 commit comments

Comments
 (0)