Skip to content

Commit

Permalink
Add authorization to page & panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasnayeen committed Jul 17, 2024
1 parent b761ac3 commit d5e40f2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
33 changes: 32 additions & 1 deletion src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hasnayeen\Xumina\Facades\Xumina;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use Inertia\Inertia;

Expand All @@ -21,6 +22,8 @@ public function __construct(
protected Content $content,
protected Layout $layout,
) {
Gate::allowIf(call_user_func(Xumina::getCurrentPanel()->getAuthorizationCallback(), auth()->user()));
$this->authorize();
Inertia::share('title', static::getPageTitle());
Xumina::getCurrentPanel()->currentPage($this);
}
Expand All @@ -29,6 +32,34 @@ abstract public function outline(): array;

abstract public static function routes(): array;

protected function authorize()
{
$action = request()->route()->getActionMethod();
$model = static::$model;
$record = request()->route($model::getRouteKeyName());

$policyMethod = $this->mapMethodToPolicy($action);

if ($record) {
Gate::authorize($policyMethod, $record);
} else {
Gate::authorize($policyMethod, $model);
}
}

protected function mapMethodToPolicy($method)
{
$map = [
'index' => 'viewAny',
'show' => 'view',
'store' => 'create',
'update' => 'update',
'destroy' => 'delete',
];

return $map[$method] ?? $method;
}

public static function getMiddlewares(): array
{
return [];
Expand Down Expand Up @@ -102,7 +133,7 @@ public function breadcrumb(): array
return [
[
'text' => $title = Xumina::getCurrentPanel()->getRootPage()::getPageTitle(),
'url' => route('xumina.'.Str::kebab(Xumina::getCurrentPanel()->getName()).'.'.Str::kebab($title)),
'url' => route('xumina.' . Str::kebab(Xumina::getCurrentPanel()->getName()) . '.' . Str::kebab($title)),
],
];
}
Expand Down
20 changes: 17 additions & 3 deletions src/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hasnayeen\Xumina;

use Closure;
use Hasnayeen\Xumina\Components\Icon;
use Hasnayeen\Xumina\Contracts\Layout;
use Hasnayeen\Xumina\Contracts\Theme;
Expand Down Expand Up @@ -33,8 +34,21 @@ public function __construct(
protected ?string $logoText = null,
protected ?string $theme = DefaultTheme::class,
protected array $navigations = [],
protected ?Closure $authorizationCallback = null,
) {}

public function authorize(Closure $callback): static
{
$this->authorizationCallback = $callback;

return $this;
}

public function getAuthorizationCallback()
{
return $this->authorizationCallback;
}

public function getName(): string
{
return $this->name;
Expand All @@ -59,7 +73,7 @@ public function getPrefix(): ?string

public function getPath(): string
{
return $this->path ?? app_path('Xumina/'.Str::studly($this->getName()));
return $this->path ?? app_path('Xumina/' . Str::studly($this->getName()));
}

/**
Expand Down Expand Up @@ -109,7 +123,7 @@ public function rootPage(string $rootPage): static

public function getRootPage(): string
{
return $this->rootPage ?? $this->getPages()->filter(fn ($page) => class_basename($page) === 'Dashboard')->first();
return $this->rootPage ?? $this->getPages()->filter(fn($page) => class_basename($page) === 'Dashboard')->first();
}

public function layout(string $layout): static
Expand Down Expand Up @@ -198,7 +212,7 @@ public function getNavigations(): array
->items(
Xumina::getCurrentPanel()
->getPages()
->filter(fn ($page) => ! Str::contains($page, 'Auth'))
->filter(fn($page) => ! Str::contains($page, 'Auth'))
->concat(Xumina::getCurrentPanel()->getResources())
->map(function ($item) {
if ($item::showInNavigation()) {
Expand Down

0 comments on commit d5e40f2

Please sign in to comment.