Skip to content

Commit

Permalink
handle authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Apr 21, 2023
1 parent 6b81894 commit c4703be
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ public function mapOption(Request $request, Model $model, Model $related): array
];
}

/**
* Get the route parameter name.
*/
public function getParameterName(): string
{
return 'rootField';
}

/**
* The routes that should be registered.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Http/Controllers/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class ActionController extends Controller
{
Expand All @@ -12,6 +13,10 @@ class ActionController extends Controller
*/
public function __invoke(Request $request): RedirectResponse
{
return $request->route('rootAction')->perform($request);
$action = $request->route('rootAction');

Gate::allowIf($action->authorized($request));

return $action->perform($request);
}
}
7 changes: 6 additions & 1 deletion src/Http/Controllers/ExtractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Root\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Inertia\Inertia;
use Inertia\Response;

Expand All @@ -13,9 +14,13 @@ class ExtractController extends Controller
*/
public function __invoke(Request $request): Response
{
$extract = $request->route('rootExtract');

Gate::allowIf($extract->authorized($request));

return Inertia::render(
'Extracts/Index',
$request->route('rootExtract')->toIndex($request)
$extract->toIndex($request)
);
}
}
15 changes: 11 additions & 4 deletions src/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;

class MediaController extends Controller
Expand All @@ -17,7 +18,9 @@ class MediaController extends Controller
*/
public function index(Request $request, Model $model = null): JsonResponse
{
$field = $request->route('rootRelation');
$field = $request->route('rootField');

Gate::allowIf($field->authorized($request, $model));

$model ??= $request->route('rootResource')->getModelInstance();

Expand All @@ -29,9 +32,11 @@ public function index(Request $request, Model $model = null): JsonResponse
*/
public function store(Request $request, Model $model = null): JsonResponse
{
$request->validate(['file' => ['required', 'file']]);
$field = $request->route('rootField');

$field = $request->route('rootRelation');
Gate::allowIf($field->authorized($request, $model));

$request->validate(['file' => ['required', 'file']]);

$model ??= $request->route('rootResource')->getModelInstance();

Expand All @@ -58,7 +63,9 @@ public function store(Request $request, Model $model = null): JsonResponse
*/
public function destroy(Request $request, Model $model = null): JsonResponse
{
$field = $request->route('rootRelation');
$field = $request->route('rootField');

Gate::allowIf($field->authorized($request, $model));

$field->resolveRelatableQuery($request, $model ?: $request->route('rootResource')->getModelInstance())
->find($request->input('models', []))
Expand Down
31 changes: 27 additions & 4 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Redirect;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -18,9 +19,13 @@ class RelationController extends Controller
*/
public function index(Request $request, Model $model): Response
{
$relation = $request->route('rootRelation');

Gate::allowIf($relation->getAbilities($model)['viewAny'] ?? false);

return Inertia::render(
'Relations/Index',
$request->route('rootRelation')->toIndex($request, $model)
$relation->toIndex($request, $model)
);
}

Expand All @@ -29,9 +34,13 @@ public function index(Request $request, Model $model): Response
*/
public function create(Request $request, Model $model): Response
{
$relation = $request->route('rootRelation');

Gate::allowIf($relation->getAbilities($model)['create'] ?? false);

return Inertia::render(
'Resources/Form',
$request->route('rootRelation')->toCreate($request, $model)
$relation->toCreate($request, $model)
);
}

Expand All @@ -42,6 +51,8 @@ public function store(Request $request, Model $model): RedirectResponse
{
$relation = $request->route('rootRelation');

Gate::allowIf($relation->getAbilities($model)['create'] ?? false);

$item = $relation->newItem($model, $relation->getRelation($model)->getRelated());

$fields = $relation->resolveFields($request)
Expand All @@ -63,9 +74,13 @@ public function store(Request $request, Model $model): RedirectResponse
*/
public function show(Request $request, Model $model, Model $related): Response
{
$relation = $request->route('rootRelation');

Gate::allowIf($relation->newItem($model, $related)->getAbilities()['view'] ?? false);

return Inertia::render(
'Resources/Show',
$request->route('rootRelation')->toShow($request, $model, $related)
$relation->toShow($request, $model, $related)
);
}

Expand All @@ -74,9 +89,13 @@ public function show(Request $request, Model $model, Model $related): Response
*/
public function edit(Request $request, Model $model, Model $related): Response
{
$relation = $request->route('rootRelation');

Gate::allowIf($relation->newItem($model, $related)->getAbilities()['update'] ?? false);

return Inertia::render(
'Resources/Form',
$request->route('rootRelation')->toEdit($request, $model, $related)
$related->toEdit($request, $model, $related)
);
}

Expand All @@ -89,6 +108,8 @@ public function update(Request $request, Model $model, Model $related): Redirect

$item = $relation->newItem($model, $related);

Gate::allowIf($item->getAbilities()['update'] ?? false);

$fields = $relation->resolveFields($request)
->authorized($request, $item->model)
->visible(ResourceContext::Update->value);
Expand All @@ -112,6 +133,8 @@ public function destroy(Request $request, Model $model, Model $related): Redirec

$item = $relation->newItem($model, $related);

Gate::allowIf($item->getAbilities()['delete'] ?? false);

$item->model->delete();

return Redirect::to($item->resolveUrl($request))
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Controllers/RelationFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class RelationFieldController extends Controller
{
Expand All @@ -13,7 +14,9 @@ class RelationFieldController extends Controller
*/
public function __invoke(Request $request, Model $model = null): JsonResponse
{
$field = $request->route('rootRelation');
$field = $request->route('rootField');

Gate::allowIf($field->authorized($request, $model));

$model ??= $request->route('rootResource')->getModelInstance();

Expand Down
7 changes: 6 additions & 1 deletion src/Http/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;

class WidgetController extends Controller
{
Expand All @@ -12,6 +13,10 @@ class WidgetController extends Controller
*/
public function __invoke(Request $request): Response
{
return new Response($request->route('rootWidget')->render());
$widget = $request->route('rootWidget');

Gate::allowIf($widget->authorized($request));

return new Response($widget->render());
}
}
2 changes: 1 addition & 1 deletion src/Relations/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getPolicy(): mixed
/**
* Resolve the abilities.
*/
protected function resolveAbilities(): array
public function getAbilities(): array
{
$policy = $this->getPolicy();

Expand Down
2 changes: 1 addition & 1 deletion src/Relations/PivotItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PivotItem extends Item
/**
* Resolve the abilities.
*/
protected function resolveAbilities(): array
public function getAbilities(): array
{
$policy = $this->getPolicy();

Expand Down
4 changes: 2 additions & 2 deletions src/Resources/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getPolicy(): mixed
/**
* Resolve the abilities.
*/
protected function resolveAbilities(): array
public function getAbilities(): array
{
$policy = $this->getPolicy();

Expand All @@ -88,7 +88,7 @@ protected function resolveAbilities(): array
public function toArray(): array
{
return [
'abilities' => $this->resolveAbilities(),
'abilities' => $this->getAbilities(),
'exists' => $this->model->exists,
'id' => $this->model->getKey(),
'trashed' => $this->isTrashed(),
Expand Down
2 changes: 1 addition & 1 deletion src/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Root
*
* @var string
*/
public const VERSION = '2.0.0';
public const VERSION = '1.3.0';

/**
* The registered booting callbacks.
Expand Down

0 comments on commit c4703be

Please sign in to comment.