Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes and improvements #310

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Commands/MakeShieldGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ protected function generatablePages(): ?array
return collect(FilamentShield::getPages())
->filter(function ($page) {
if ($this->excludePages) {
return ! in_array($page, $this->pages);
return ! in_array($page['class'], $this->pages);
}

if ($this->onlyPages) {
return in_array($page, $this->pages);
return in_array($page['class'], $this->pages);
}

return true;
Expand All @@ -169,11 +169,11 @@ protected function generatableWidgets(): ?array
return collect(FilamentShield::getWidgets())
->filter(function ($widget) {
if ($this->excludeWidgets) {
return ! in_array($widget, $this->widgets);
return ! in_array($widget['class'], $this->widgets);
}

if ($this->onlyWidgets) {
return in_array($widget, $this->widgets);
return in_array($widget['class'], $this->widgets);
}

return true;
Expand Down Expand Up @@ -213,14 +213,14 @@ protected function generateForPages(array $pages): Collection
{
return collect($pages)
->values()
->each(fn (string $page) => FilamentShield::generateForPage($page));
->each(fn (array $page) => FilamentShield::generateForPage($page['permission']));
}

protected function generateForWidgets(array $widgets): Collection
{
return collect($widgets)
->values()
->each(fn (string $widget) => FilamentShield::generateForWidget($widget));
->each(fn (array $widget) => FilamentShield::generateForWidget($widget['permission']));
}

protected function resourceInfo(array $resources): void
Expand Down Expand Up @@ -261,8 +261,8 @@ protected function pageInfo(array $pages): void
collect($pages)->map(function ($page, $key) {
return [
'#' => $key + 1,
'Page' => Str::replace(config('filament-shield.permission_prefixes.page') . '_', '', $page),
'Permission' => $page,
'Page' => $page['class'],
'Permission' => $page['permission'],
];
})
);
Expand All @@ -280,8 +280,8 @@ protected function widgetInfo(array $widgets): void
collect($widgets)->map(function ($widget, $key) {
return [
'#' => $key + 1,
'Widget' => Str::replace(config('filament-shield.permission_prefixes.widget') . '_', '', $widget),
'Permission' => $widget,
'Widget' => $widget['class'],
'Permission' => $widget['permission'],
];
})
);
Expand Down
131 changes: 54 additions & 77 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Support\Concerns\EvaluatesClosures;
use Filament\Widgets\TableWidget;
use Filament\Widgets\Widget;
use Filament\Widgets\WidgetConfiguration;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -124,28 +125,25 @@ public function getResources(): ?array
}

return collect($resources)
->unique()
->filter(function ($resource) {
->reject(function ($resource) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(
return in_array(
Str::of($resource)->afterLast('\\'),
Utils::getExcludedResouces()
);
}

return true;
})
->reduce(function ($resources, $resource) {
->mapWithKeys(function ($resource) {
$name = $this->getPermissionIdentifier($resource);

$resources["{$name}"] = [
'resource' => "{$name}",
'model' => Str::of($resource::getModel())->afterLast('\\'),
'fqcn' => $resource,
return [
$name => [
'resource' => "{$name}",
'model' => Str::of($resource::getModel())->afterLast('\\'),
'fqcn' => $resource,
],
];

return $resources;
}, collect())
})
->sortKeys()
->toArray();
}
Expand Down Expand Up @@ -195,27 +193,27 @@ public static function getPages(): ?array
}

return collect($pages)
->filter(function ($page) {
->reject(function ($page) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(Str::afterLast($page, '\\'), Utils::getExcludedPages());
return in_array(Str::afterLast($page, '\\'), Utils::getExcludedPages());
}

return true;
})
->reduce(function ($pages, $page) {

$name = Str::of(class_basename($page))
->mapWithKeys(function ($page) {
$permission = Str::of(class_basename($page))
->prepend(
Str::of(Utils::getPagePermissionPrefix())
->append('_')
->toString()
)
->toString();

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

return $pages;
}, collect())
return [
$permission => [
'class' => $page,
'permission' => $permission,
],
];
})
->toArray();
}

Expand All @@ -224,13 +222,11 @@ public static function getPages(): ?array
*/
public static function getLocalizedPageLabel(string $page): string
{
$object = static::transformClassString($page);

$pageObject = new $object();
$pageInstance = app()->make($page);

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

Expand All @@ -249,27 +245,34 @@ public static function getWidgets(): ?array
}

return collect($widgets)
->filter(function ($widget) {
->reject(function ($widget) {
if (Utils::isGeneralExcludeEnabled()) {
return ! in_array(Str::afterLast($widget, '\\'), Utils::getExcludedWidgets());
return in_array(
needle: str(
static::getWidgetInstanceFromWidgetConfiguration($widget)
)
->afterLast('\\')
->toString(),
haystack: Utils::getExcludedWidgets()
);
}

return true;
})
->reduce(function ($widgets, $widget) {

$name = Str::of(class_basename($widget))
->mapWithKeys(function ($widget) {
$permission = Str::of(class_basename(static::getWidgetInstanceFromWidgetConfiguration($widget)))
->prepend(
Str::of(Utils::getWidgetPermissionPrefix())
->append('_')
->toString()
)
->toString();

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

return $widgets;
}, collect())
return [
$permission => [
'class' => static::getWidgetInstanceFromWidgetConfiguration($widget),
'permission' => $permission,
],
];
})
->toArray();
}

Expand All @@ -278,51 +281,18 @@ public static function getWidgets(): ?array
*/
public static function getLocalizedWidgetLabel(string $widget): string
{
$class = static::transformClassString($widget, false);

$widgetInstance = app()->make($class);
$widgetInstance = app()->make($widget);

return match (true) {
$widgetInstance instanceof TableWidget => (string) invade($widgetInstance)->makeTable()->getHeading(),
! ($widgetInstance instanceof TableWidget) && $widgetInstance instanceof Widget && method_exists($widgetInstance, 'getHeading') => (string) invade($widgetInstance)->getHeading(),
default => Str::of($widget)
->after(Utils::getWidgetPermissionPrefix() . '_')
default => str($widget)
->afterLast('\\')
->headline()
->toString(),
};
}

protected static function transformClassString(string $string, bool $isPageClass = true): string
{
$pages = Filament::getPages();
if (Utils::discoverAllPages()) {
$pages = [];
foreach (Filament::getPanels() as $panel) {
$pages = array_merge($pages, $panel->getPages());
}
$pages = array_unique($pages);
}

$widgets = Filament::getWidgets();
if (Utils::discoverAllWidgets()) {
$widgets = [];
foreach (Filament::getPanels() as $panel) {
$widgets = array_merge($widgets, $panel->getWidgets());
}
$widgets = array_unique($widgets);
}

$prefix = Str::of($isPageClass ? Utils::getPagePermissionPrefix() : Utils::getWidgetPermissionPrefix())->append('_');

return (string) collect($isPageClass ? $pages : $widgets)
->first(fn ($item) => class_basename($item) == Str::of($string)->after($prefix)->studly());
}

protected static function hasHeadingForShield(object | string $class): bool
{
return method_exists($class, 'getHeadingForShield');
}

protected function getDefaultPermissionIdentifier(string $resource): string
{
return Str::of($resource)
Expand All @@ -332,4 +302,11 @@ protected function getDefaultPermissionIdentifier(string $resource): string
->snake()
->replace('_', '::');
}

protected static function getWidgetInstanceFromWidgetConfiguration(string | WidgetConfiguration $widget): string
{
return $widget instanceof WidgetConfiguration
? $widget->widget
: $widget;
}
}
Loading
Loading