mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
2.x
This commit is contained in:
72
app/Web/Traits/BelongsToServers.php
Normal file
72
app/Web/Traits/BelongsToServers.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Traits;
|
||||
|
||||
use App\Models\Server;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
trait BelongsToServers
|
||||
{
|
||||
public static function getUrl(string $name = 'index', array $parameters = [], bool $isAbsolute = true, ?string $panel = null, ?Model $tenant = null): string
|
||||
{
|
||||
if (! isset($parameters['server'])) {
|
||||
$parameters['server'] = request()->route('server') ?? 0;
|
||||
}
|
||||
|
||||
return parent::getUrl($name, $parameters, $isAbsolute, $panel, $tenant);
|
||||
}
|
||||
|
||||
public static function getServerFromRoute(): Server
|
||||
{
|
||||
$server = request()->route('server');
|
||||
|
||||
if (! $server) {
|
||||
$server = Route::getRoutes()->match(Request::create(url()->previous()))->parameter('server');
|
||||
}
|
||||
|
||||
if (! $server instanceof Server) {
|
||||
$server = Server::query()->find($server);
|
||||
}
|
||||
|
||||
if (! $server) {
|
||||
$server = new Server;
|
||||
}
|
||||
|
||||
return $server;
|
||||
}
|
||||
|
||||
public static function canViewAny(): bool
|
||||
{
|
||||
return static::can('viewAny');
|
||||
|
||||
return auth()->user()->can('viewAny', [static::getModel(), static::getServerFromRoute()]);
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
return auth()->user()->can('create', [static::getModel(), static::getServerFromRoute()]);
|
||||
}
|
||||
|
||||
public static function authorizeViewAny(): void
|
||||
{
|
||||
Gate::authorize('viewAny', [static::getModel(), static::getServerFromRoute()]);
|
||||
}
|
||||
|
||||
public static function authorizeCreate(): void
|
||||
{
|
||||
Gate::authorize('create', [static::getModel(), static::getServerFromRoute()]);
|
||||
}
|
||||
|
||||
public static function authorizeEdit(Model $record): void
|
||||
{
|
||||
Gate::authorize('update', [$record, static::getServerFromRoute()]);
|
||||
}
|
||||
|
||||
public static function authorizeView(Model $record): void
|
||||
{
|
||||
Gate::authorize('view', [$record, static::getServerFromRoute()]);
|
||||
}
|
||||
}
|
20
app/Web/Traits/PageHasCluster.php
Normal file
20
app/Web/Traits/PageHasCluster.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Traits;
|
||||
|
||||
trait PageHasCluster
|
||||
{
|
||||
// public function getMaxContentWidth(): ?string
|
||||
// {
|
||||
// return 'full';
|
||||
// }
|
||||
|
||||
public function getSubNavigation(): array
|
||||
{
|
||||
if (filled($cluster = static::getCluster())) {
|
||||
return $this->generateNavigationItems($cluster::getClusteredComponents());
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
61
app/Web/Traits/PageHasServer.php
Normal file
61
app/Web/Traits/PageHasServer.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Traits;
|
||||
|
||||
use App\Models\Site;
|
||||
use App\Web\Pages\Servers\Settings;
|
||||
use App\Web\Pages\Servers\Sites\Index;
|
||||
use App\Web\Pages\Servers\View;
|
||||
use App\Web\Pages\Servers\Widgets\ServerSummary;
|
||||
use Filament\Navigation\NavigationItem;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
|
||||
trait PageHasServer
|
||||
{
|
||||
public function getTitle(): string|Htmlable
|
||||
{
|
||||
return static::$title.' - '.$this->server->name;
|
||||
}
|
||||
|
||||
public function getSubNavigation(): array
|
||||
{
|
||||
$items = [];
|
||||
|
||||
if (auth()->user()?->can('view', $this->server)) {
|
||||
$items[] = NavigationItem::make('Overview')
|
||||
->icon('heroicon-o-chart-pie')
|
||||
->isActiveWhen(fn () => request()->routeIs(View::getRouteName()))
|
||||
->url(View::getUrl(parameters: ['server' => $this->server]));
|
||||
}
|
||||
|
||||
if (auth()->user()?->can('viewAny', [Site::class, $this->server])) {
|
||||
$items[] = NavigationItem::make('Sites')
|
||||
->icon('heroicon-o-globe-alt')
|
||||
->isActiveWhen(fn () => request()->routeIs(Index::getRouteName().'*'))
|
||||
->url(Index::getUrl(parameters: ['server' => $this->server]));
|
||||
}
|
||||
|
||||
if (auth()->user()?->can('update', $this->server)) {
|
||||
$items[] = NavigationItem::make('Settings')
|
||||
->icon('heroicon-o-cog-6-tooth')
|
||||
->isActiveWhen(fn () => request()->routeIs(Settings::getRouteName().'*'))
|
||||
->url(Settings::getUrl(parameters: ['server' => $this->server]));
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
protected function getHeaderWidgets(): array
|
||||
{
|
||||
return [
|
||||
ServerSummary::make([
|
||||
'server' => $this->server,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function getHeaderWidgetsColumns(): int|string|array
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
29
app/Web/Traits/PageHasWidgets.php
Normal file
29
app/Web/Traits/PageHasWidgets.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Traits;
|
||||
|
||||
use Illuminate\View\ComponentAttributeBag;
|
||||
|
||||
trait PageHasWidgets
|
||||
{
|
||||
protected array $extraAttributes = [
|
||||
'wire:poll.5s' => '$dispatch(\'$refresh\')',
|
||||
];
|
||||
|
||||
protected function getExtraAttributes(): array
|
||||
{
|
||||
return $this->extraAttributes;
|
||||
}
|
||||
|
||||
public function getView(): string
|
||||
{
|
||||
return 'web.components.page';
|
||||
}
|
||||
|
||||
public function getExtraAttributesBag(): ComponentAttributeBag
|
||||
{
|
||||
return new ComponentAttributeBag($this->getExtraAttributes());
|
||||
}
|
||||
|
||||
abstract public function getWidgets(): array;
|
||||
}
|
Reference in New Issue
Block a user