Skip to content

Commit

Permalink
add permission to access panel
Browse files Browse the repository at this point in the history
  • Loading branch information
rupadana committed Sep 8, 2023
1 parent f9bf211 commit 1f2362a
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 19 deletions.
4 changes: 4 additions & 0 deletions config/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@

'page' => 'page',
'widget' => 'widget',
'panel' => 'panel'
],

'entities' => [
'pages' => true,
'widgets' => true,
'resources' => true,
'panels' => true,
'custom_permissions' => false,
],

Expand All @@ -70,6 +72,8 @@
],

'resources' => [],

'panels' => [] // Using Panel ID
],

'register_role_policy' => [
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ar/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'المصادر',
'widgets' => 'الملحقات',
'pages' => 'الصفحات',
'panels' => 'لوحات',
'custom' => 'أذونات مخصصة',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/de/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Ressourcen',
'widgets' => 'Widgets',
'pages' => 'Seiten',
'panels' => 'Panels',
'custom' => 'benutzerdefinierte Berechtigungen',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Resources',
'widgets' => 'Widgets',
'pages' => 'Pages',
'panels' => 'Panels',
'custom' => 'Custom Permissions',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/es/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Recursos',
'widgets' => 'Widgets',
'pages' => 'Páginas',
'panels' => 'Paneles',
'custom' => 'Permisos personalizados',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/fa/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'منابع',
'widgets' => 'ویجت‌ها',
'pages' => 'صفحات',
'panels' => 'پانل ها',
'custom' => 'دسترسی‌های سفارشی',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/fr/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Ressources',
'widgets' => 'Widgets',
'pages' => 'Pages',
'panels' => 'Panneaux',
'custom' => 'Permissions personnalisées',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/hu/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Erőforrások',
'widgets' => 'Widgetek',
'pages' => 'Oldalak',
'panels' => 'Panelek',
'custom' => 'Egyedi jogosultságok',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/id/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Sumber Daya',
'widgets' => 'Widget',
'pages' => 'Halaman',
'panels' => 'Panel',
'custom' => 'Izin Kustom',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/it/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'Resources',
'widgets' => 'Widgets',
'pages' => 'Pages',
'panels' => 'Pannelli',
'custom' => 'Permessi Personalizzati',

/*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ja/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'resources' => 'リソース',
'widgets' => 'ウィジェット',
'pages' => 'ページ',
'panels' => 'パネル',
'custom' => 'カスタムパーミッション',

/*
Expand Down
45 changes: 38 additions & 7 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function generateForWidget(string $widget): void

protected static function giveSuperAdminPermission(string | array | Collection $permissions): void
{
if (! Utils::isSuperAdminDefinedViaGate()) {
if (!Utils::isSuperAdminDefinedViaGate()) {
$superAdmin = static::createRole();

$superAdmin->givePermissionTo($permissions);
Expand Down Expand Up @@ -118,7 +118,7 @@ public function getResources(): ?array
->unique()
->filter(function ($resource) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(
return !in_array(
Str::of($resource)->afterLast('\\'),
Utils::getExcludedResouces()
);
Expand Down Expand Up @@ -173,7 +173,7 @@ public static function getPages(): ?array
return collect(Filament::getPages())
->filter(function ($page) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(Str::afterLast($page, '\\'), Utils::getExcludedPages());
return !in_array(Str::afterLast($page, '\\'), Utils::getExcludedPages());
}

return true;
Expand All @@ -200,9 +200,9 @@ public static function getLocalizedPageLabel(string $page): string
$pageObject = invade(new $object());

return $pageObject->getTitle()
?? $pageObject->getHeading()
?? $pageObject->getNavigationLabel()
?? '';
?? $pageObject->getHeading()
?? $pageObject->getNavigationLabel()
?? '';
}

/**
Expand All @@ -215,7 +215,7 @@ public static function getWidgets(): ?array
return collect(Filament::getWidgets())
->filter(function ($widget) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(Str::afterLast($widget, '\\'), Utils::getExcludedWidgets());
return !in_array(Str::afterLast($widget, '\\'), Utils::getExcludedWidgets());
}

return true;
Expand All @@ -232,6 +232,7 @@ public static function getWidgets(): ?array
->toArray();
}


/**
* Get localized widget label
*
Expand Down Expand Up @@ -261,6 +262,13 @@ public static function getLocalizedWidgetLabel(string $widget): string
};
}

public static function getLocalizedPanelLabel(string $panel)
{
return Str::of($panel)
->after(Utils::getPanelPermissionPrefix() . '_')
->headline();
}

protected static function transformClassString(string $string, bool $isPageClass = true): string
{
return (string) collect($isPageClass ? Filament::getPages() : Filament::getWidgets())
Expand All @@ -286,4 +294,27 @@ protected function getDefaultPermissionIdentifier(string $resource): string
->snake()
->replace('_', '::');
}


public static function getPanels(): ?array
{
return collect(Filament::getPanels())
->filter(function ($panel) {
if (Utils::isGeneralExcludeEnabled()) {
return !in_array($panel->getId(), Utils::getExcludedWidgets());
}

return true;
})
->reduce(function ($panels, $panel) {
$prepend = Str::of(Utils::getPanelPermissionPrefix())->append('_');
$name = Str::of($panel->getId())
->prepend($prepend);

$panels["{$name}"] = "{$name}";

return $panels;
}, collect())
->toArray();
}
}
45 changes: 45 additions & 0 deletions src/Middleware/PanelAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace BezhanSalleh\FilamentShield\Middleware;

use BezhanSalleh\FilamentShield\Support\Utils;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Str;

class PanelAccess
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$currentPanel = filament()->getCurrentPanel();
$panels = filament()->getPanels();


if (count($panels) > 1) {
if (!$this->hasAccessTo($currentPanel)) {
foreach ($panels as $key => $panel) {
if ($this->hasAccessTo($panel)) {
return redirect($panel->getPath());
}
}
}
}

return $next($request);
}

protected function hasAccessTo($panel)
{
$prepend = Str::of(Utils::getPanelPermissionPrefix())->append('_');
$permissionName = Str::of($panel->getId())
->prepend($prepend);

return auth()->user()->can($permissionName);
}
}
Loading

0 comments on commit 1f2362a

Please sign in to comment.