migrating tests (Application, Console and Cronjob)

This commit is contained in:
Saeed Vaziry 2024-10-08 22:15:50 +02:00
parent 974af959f1
commit 0da21f40bd
39 changed files with 254 additions and 2684 deletions

View File

@ -34,6 +34,15 @@ class CronJob extends AbstractModel
'hidden' => 'boolean', 'hidden' => 'boolean',
]; ];
public static array $statusColors = [
CronjobStatus::CREATING => 'warning',
CronjobStatus::READY => 'success',
CronjobStatus::DELETING => 'danger',
CronjobStatus::ENABLING => 'warning',
CronjobStatus::DISABLING => 'warning',
CronjobStatus::DISABLED => 'gray',
];
public function server(): BelongsTo public function server(): BelongsTo
{ {
return $this->belongsTo(Server::class); return $this->belongsTo(Server::class);
@ -75,4 +84,14 @@ public function frequencyLabel(): string
return $this->frequency; return $this->frequency;
} }
public function isEnabled(): bool
{
return $this->status === CronjobStatus::READY;
}
public function isDisabled(): bool
{
return $this->status === CronjobStatus::DISABLED;
}
} }

View File

@ -4,6 +4,7 @@
use App\Models\NotificationChannel; use App\Models\NotificationChannel;
use App\Notifications\NotificationInterface; use App\Notifications\NotificationInterface;
use App\Web\Pages\Settings\NotificationChannels\Index;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
class Discord extends AbstractNotificationChannel class Discord extends AbstractNotificationChannel
@ -38,7 +39,7 @@ public function connect(): bool
__('Congratulations! 🎉'), __('Congratulations! 🎉'),
__("You've connected your Discord to :app", ['app' => config('app.name')])."\n". __("You've connected your Discord to :app", ['app' => config('app.name')])."\n".
__('Manage your notification channels')."\n". __('Manage your notification channels')."\n".
route('settings.notification-channels') Index::getUrl()
); );
if (! $connect) { if (! $connect) {

View File

@ -4,6 +4,7 @@
use App\Models\NotificationChannel; use App\Models\NotificationChannel;
use App\Notifications\NotificationInterface; use App\Notifications\NotificationInterface;
use App\Web\Pages\Settings\NotificationChannels\Index;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
class Slack extends AbstractNotificationChannel class Slack extends AbstractNotificationChannel
@ -38,7 +39,7 @@ public function connect(): bool
__('Congratulations! 🎉'), __('Congratulations! 🎉'),
__("You've connected your Slack to :app", ['app' => config('app.name')])."\n". __("You've connected your Slack to :app", ['app' => config('app.name')])."\n".
__('Manage your notification channels')."\n". __('Manage your notification channels')."\n".
route('settings.notification-channels') Index::getUrl()
); );
if (! $connect) { if (! $connect) {

View File

@ -34,6 +34,6 @@ public function delete(User $user, Server $server): bool
public function manage(User $user, Server $server): bool public function manage(User $user, Server $server): bool
{ {
return $user->isAdmin() || $server->project->users->contains($user); return ($user->isAdmin() || $server->project->users->contains($user)) && $server->isReady();
} }
} }

View File

@ -51,7 +51,7 @@ public function boot(): void
]); ]);
FilamentColor::register([ FilamentColor::register([
'slate' => Color::Slate, 'slate' => Color::Slate,
'gray' => Color::Gray, 'gray' => Color::Zinc,
'red' => Color::Red, 'red' => Color::Red,
'orange' => Color::Orange, 'orange' => Color::Orange,
'amber' => Color::Amber, 'amber' => Color::Amber,

View File

@ -1,15 +0,0 @@
<?php
namespace App\Web\Pages;
use Filament\Widgets\AccountWidget;
class Dashboard extends \Filament\Pages\Dashboard
{
public function getWidgets(): array
{
return [
AccountWidget::class,
];
}
}

View File

@ -12,11 +12,11 @@ class Index extends Page
protected static ?string $slug = 'servers/{server}/console'; protected static ?string $slug = 'servers/{server}/console';
protected static ?string $title = 'Console'; protected static ?string $title = 'Headless Console';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('update', static::getServerFromRoute()) ?? false; $this->authorize('manage', $this->server);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -20,9 +20,9 @@ class Index extends Page
protected $listeners = ['$refresh']; protected $listeners = ['$refresh'];
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [CronJob::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [CronJob::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -3,6 +3,8 @@
namespace App\Web\Pages\Servers\CronJobs\Widgets; namespace App\Web\Pages\Servers\CronJobs\Widgets;
use App\Actions\CronJob\DeleteCronJob; use App\Actions\CronJob\DeleteCronJob;
use App\Actions\CronJob\DisableCronJob;
use App\Actions\CronJob\EnableCronJob;
use App\Models\CronJob; use App\Models\CronJob;
use App\Models\Server; use App\Models\Server;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
@ -33,6 +35,17 @@ protected function getTableColumns(): array
->tooltip(fn (CronJob $cronJob) => $cronJob->command) ->tooltip(fn (CronJob $cronJob) => $cronJob->command)
->searchable() ->searchable()
->copyable(), ->copyable(),
TextColumn::make('frequency')
->formatStateUsing(fn (CronJob $cronJob) => $cronJob->frequencyLabel())
->searchable()
->sortable()
->copyable(),
TextColumn::make('status')
->label('Status')
->badge()
->color(fn (CronJob $record) => CronJob::$statusColors[$record->status])
->searchable()
->sortable(),
TextColumn::make('created_at') TextColumn::make('created_at')
->formatStateUsing(fn (CronJob $cronJob) => $cronJob->created_at_by_timezone) ->formatStateUsing(fn (CronJob $cronJob) => $cronJob->created_at_by_timezone)
->sortable(), ->sortable(),
@ -43,6 +56,30 @@ public function getTable(): Table
{ {
return $this->table return $this->table
->actions([ ->actions([
Action::make('enable')
->hiddenLabel()
->tooltip('Enable')
->icon('heroicon-o-play')
->requiresConfirmation()
->authorize(fn (CronJob $record) => auth()->user()->can('update', [$record, $this->server]))
->visible(fn (CronJob $record) => $record->isDisabled())
->action(function (CronJob $record) {
run_action($this, function () use ($record) {
app(EnableCronJob::class)->enable($this->server, $record);
});
}),
Action::make('disable')
->hiddenLabel()
->tooltip('Disable')
->icon('heroicon-o-stop')
->requiresConfirmation()
->authorize(fn (CronJob $record) => auth()->user()->can('update', [$record, $this->server]))
->visible(fn (CronJob $record) => $record->isEnabled())
->action(function (CronJob $record) {
run_action($this, function () use ($record) {
app(DisableCronJob::class)->disable($this->server, $record);
});
}),
Action::make('delete') Action::make('delete')
->icon('heroicon-o-trash') ->icon('heroicon-o-trash')
->tooltip('Delete') ->tooltip('Delete')

View File

@ -23,9 +23,9 @@ class Backups extends Page implements HasSecondSubNav
protected static ?string $title = 'Backups'; protected static ?string $title = 'Backups';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Backup::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Backup::class, $this->server]);
} }
protected function getHeaderActions(): array protected function getHeaderActions(): array

View File

@ -20,9 +20,9 @@ class Index extends Page implements HasSecondSubNav
protected static ?string $title = 'Databases'; protected static ?string $title = 'Databases';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Database::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Database::class, $this->server]);
} }
protected function getHeaderActions(): array protected function getHeaderActions(): array

View File

@ -2,6 +2,9 @@
namespace App\Web\Pages\Servers\Databases\Traits; namespace App\Web\Pages\Servers\Databases\Traits;
use App\Models\Backup;
use App\Models\Database;
use App\Models\DatabaseUser;
use App\Web\Pages\Servers\Databases\Backups as Backups; use App\Web\Pages\Servers\Databases\Backups as Backups;
use App\Web\Pages\Servers\Databases\Index as Databases; use App\Web\Pages\Servers\Databases\Index as Databases;
use App\Web\Pages\Servers\Databases\Users as Users; use App\Web\Pages\Servers\Databases\Users as Users;
@ -14,21 +17,21 @@ public function getSecondSubNavigation(): array
{ {
$items = []; $items = [];
if (Databases::canAccess()) { if (auth()->user()->can('viewAny', [Database::class, $this->server])) {
$items[] = NavigationItem::make(Databases::getNavigationLabel()) $items[] = NavigationItem::make(Databases::getNavigationLabel())
->icon('heroicon-o-circle-stack') ->icon('heroicon-o-circle-stack')
->isActiveWhen(fn () => request()->routeIs(Databases::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(Databases::getRouteName()))
->url(Databases::getUrl(parameters: ['server' => $this->server])); ->url(Databases::getUrl(parameters: ['server' => $this->server]));
} }
if (Users::canAccess()) { if (auth()->user()->can('viewAny', [DatabaseUser::class, $this->server])) {
$items[] = NavigationItem::make(Users::getNavigationLabel()) $items[] = NavigationItem::make(Users::getNavigationLabel())
->icon('heroicon-o-users') ->icon('heroicon-o-users')
->isActiveWhen(fn () => request()->routeIs(Users::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(Users::getRouteName()))
->url(Users::getUrl(parameters: ['server' => $this->server])); ->url(Users::getUrl(parameters: ['server' => $this->server]));
} }
if (Backups::canAccess()) { if (auth()->user()->can('viewAny', [Backup::class, $this->server])) {
$items[] = NavigationItem::make(Backups::getNavigationLabel()) $items[] = NavigationItem::make(Backups::getNavigationLabel())
->icon('heroicon-o-cloud') ->icon('heroicon-o-cloud')
->isActiveWhen(fn () => request()->routeIs(Backups::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(Backups::getRouteName()))

View File

@ -22,9 +22,9 @@ class Users extends Page implements HasSecondSubNav
protected static ?string $title = 'Database Users'; protected static ?string $title = 'Database Users';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [DatabaseUser::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [DatabaseUser::class, $this->server]);
} }
protected function getHeaderActions(): array protected function getHeaderActions(): array

View File

@ -20,9 +20,9 @@ class Index extends Page
protected $listeners = ['$refresh']; protected $listeners = ['$refresh'];
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [FirewallRule::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [FirewallRule::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -32,9 +32,9 @@ public static function getNavigationItemActiveRoutePattern(): string
return static::getRouteName().'*'; return static::getRouteName().'*';
} }
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', Server::class) ?? false; $this->authorize('viewAny', Server::class);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -12,9 +12,9 @@ class Index extends Page
protected static ?string $title = 'Logs'; protected static ?string $title = 'Logs';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [ServerLog::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [ServerLog::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -11,9 +11,9 @@ class Index extends Page
protected static ?string $title = 'Metrics'; protected static ?string $title = 'Metrics';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Metric::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Metric::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -16,9 +16,9 @@ class Index extends Page
protected static ?string $title = 'PHP'; protected static ?string $title = 'PHP';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Service::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Service::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -2,7 +2,15 @@
namespace App\Web\Pages\Servers; namespace App\Web\Pages\Servers;
use App\Models\CronJob;
use App\Models\Database;
use App\Models\FirewallRule;
use App\Models\Metric;
use App\Models\Server; use App\Models\Server;
use App\Models\ServerLog;
use App\Models\Service;
use App\Models\Site;
use App\Models\SshKey;
use App\Web\Components\Page as BasePage; use App\Web\Components\Page as BasePage;
use App\Web\Pages\Servers\Console\Index as ConsoleIndex; use App\Web\Pages\Servers\Console\Index as ConsoleIndex;
use App\Web\Pages\Servers\CronJobs\Index as CronJobsIndex; use App\Web\Pages\Servers\CronJobs\Index as CronJobsIndex;
@ -18,8 +26,6 @@
use App\Web\Pages\Servers\View as ServerView; use App\Web\Pages\Servers\View as ServerView;
use App\Web\Pages\Servers\Widgets\ServerSummary; use App\Web\Pages\Servers\Widgets\ServerSummary;
use Filament\Navigation\NavigationItem; use Filament\Navigation\NavigationItem;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;
abstract class Page extends BasePage abstract class Page extends BasePage
{ {
@ -31,84 +37,84 @@ public function getSubNavigation(): array
{ {
$items = []; $items = [];
if (ServerView::canAccess()) { if (auth()->user()->can('view', $this->server)) {
$items[] = NavigationItem::make(ServerView::getNavigationLabel()) $items[] = NavigationItem::make(ServerView::getNavigationLabel())
->icon('heroicon-o-chart-pie') ->icon('heroicon-o-chart-pie')
->isActiveWhen(fn () => request()->routeIs(ServerView::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(ServerView::getRouteName()))
->url(ServerView::getUrl(parameters: ['server' => $this->server])); ->url(ServerView::getUrl(parameters: ['server' => $this->server]));
} }
if (SitesIndex::canAccess()) { if (auth()->user()->can('viewAny', [Site::class, $this->server])) {
$items[] = NavigationItem::make(SitesIndex::getNavigationLabel()) $items[] = NavigationItem::make(SitesIndex::getNavigationLabel())
->icon('heroicon-o-cursor-arrow-ripple') ->icon('heroicon-o-cursor-arrow-ripple')
->isActiveWhen(fn () => request()->routeIs(SitesIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(SitesIndex::getRouteName().'*'))
->url(SitesIndex::getUrl(parameters: ['server' => $this->server])); ->url(SitesIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (DatabasesIndex::canAccess()) { if (auth()->user()->can('viewAny', [Database::class, $this->server])) {
$items[] = NavigationItem::make(DatabasesIndex::getNavigationLabel()) $items[] = NavigationItem::make(DatabasesIndex::getNavigationLabel())
->icon('heroicon-o-circle-stack') ->icon('heroicon-o-circle-stack')
->isActiveWhen(fn () => request()->routeIs(DatabasesIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(DatabasesIndex::getRouteName().'*'))
->url(DatabasesIndex::getUrl(parameters: ['server' => $this->server])); ->url(DatabasesIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (PHPIndex::canAccess()) { if (auth()->user()->can('viewAny', [Service::class, $this->server])) {
$items[] = NavigationItem::make(PHPIndex::getNavigationLabel()) $items[] = NavigationItem::make(PHPIndex::getNavigationLabel())
->icon('heroicon-o-code-bracket') ->icon('heroicon-o-code-bracket')
->isActiveWhen(fn () => request()->routeIs(PHPIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(PHPIndex::getRouteName().'*'))
->url(PHPIndex::getUrl(parameters: ['server' => $this->server])); ->url(PHPIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (FirewallIndex::canAccess()) { if (auth()->user()->can('viewAny', [FirewallRule::class, $this->server])) {
$items[] = NavigationItem::make(FirewallIndex::getNavigationLabel()) $items[] = NavigationItem::make(FirewallIndex::getNavigationLabel())
->icon('heroicon-o-fire') ->icon('heroicon-o-fire')
->isActiveWhen(fn () => request()->routeIs(FirewallIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(FirewallIndex::getRouteName().'*'))
->url(FirewallIndex::getUrl(parameters: ['server' => $this->server])); ->url(FirewallIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (CronJobsIndex::canAccess()) { if (auth()->user()->can('viewAny', [CronJob::class, $this->server])) {
$items[] = NavigationItem::make(CronJobsIndex::getNavigationLabel()) $items[] = NavigationItem::make(CronJobsIndex::getNavigationLabel())
->icon('heroicon-o-clock') ->icon('heroicon-o-clock')
->isActiveWhen(fn () => request()->routeIs(CronJobsIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(CronJobsIndex::getRouteName().'*'))
->url(CronJobsIndex::getUrl(parameters: ['server' => $this->server])); ->url(CronJobsIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (SshKeysIndex::canAccess()) { if (auth()->user()->can('viewAnyServer', [SshKey::class, $this->server])) {
$items[] = NavigationItem::make(SshKeysIndex::getNavigationLabel()) $items[] = NavigationItem::make(SshKeysIndex::getNavigationLabel())
->icon('heroicon-o-key') ->icon('heroicon-o-key')
->isActiveWhen(fn () => request()->routeIs(SshKeysIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(SshKeysIndex::getRouteName().'*'))
->url(SshKeysIndex::getUrl(parameters: ['server' => $this->server])); ->url(SshKeysIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (ServicesIndex::canAccess()) { if (auth()->user()->can('viewAny', [Service::class, $this->server])) {
$items[] = NavigationItem::make(ServicesIndex::getNavigationLabel()) $items[] = NavigationItem::make(ServicesIndex::getNavigationLabel())
->icon('heroicon-o-cog-6-tooth') ->icon('heroicon-o-cog-6-tooth')
->isActiveWhen(fn () => request()->routeIs(ServicesIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(ServicesIndex::getRouteName().'*'))
->url(ServicesIndex::getUrl(parameters: ['server' => $this->server])); ->url(ServicesIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (MetricsIndex::canAccess()) { if (auth()->user()->can('viewAny', [Metric::class, $this->server])) {
$items[] = NavigationItem::make(MetricsIndex::getNavigationLabel()) $items[] = NavigationItem::make(MetricsIndex::getNavigationLabel())
->icon('heroicon-o-chart-bar') ->icon('heroicon-o-chart-bar')
->isActiveWhen(fn () => request()->routeIs(MetricsIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(MetricsIndex::getRouteName().'*'))
->url(MetricsIndex::getUrl(parameters: ['server' => $this->server])); ->url(MetricsIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (ConsoleIndex::canAccess()) { if (auth()->user()->can('manage', $this->server)) {
$items[] = NavigationItem::make(ConsoleIndex::getNavigationLabel()) $items[] = NavigationItem::make(ConsoleIndex::getNavigationLabel())
->icon('heroicon-o-command-line') ->icon('heroicon-o-command-line')
->isActiveWhen(fn () => request()->routeIs(ConsoleIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(ConsoleIndex::getRouteName().'*'))
->url(ConsoleIndex::getUrl(parameters: ['server' => $this->server])); ->url(ConsoleIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (LogsIndex::canAccess()) { if (auth()->user()->can('viewAny', [ServerLog::class, $this->server])) {
$items[] = NavigationItem::make(LogsIndex::getNavigationLabel()) $items[] = NavigationItem::make(LogsIndex::getNavigationLabel())
->icon('heroicon-o-square-3-stack-3d') ->icon('heroicon-o-square-3-stack-3d')
->isActiveWhen(fn () => request()->routeIs(LogsIndex::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(LogsIndex::getRouteName().'*'))
->url(LogsIndex::getUrl(parameters: ['server' => $this->server])); ->url(LogsIndex::getUrl(parameters: ['server' => $this->server]));
} }
if (ServerSettings::canAccess()) { if (auth()->user()->can('update', $this->server)) {
$items[] = NavigationItem::make(ServerSettings::getNavigationLabel()) $items[] = NavigationItem::make(ServerSettings::getNavigationLabel())
->icon('heroicon-o-wrench-screwdriver') ->icon('heroicon-o-wrench-screwdriver')
->isActiveWhen(fn () => request()->routeIs(ServerSettings::getRouteName().'*')) ->isActiveWhen(fn () => request()->routeIs(ServerSettings::getRouteName().'*'))
@ -127,25 +133,6 @@ protected function getHeaderWidgets(): array
]; ];
} }
protected static function getServerFromRoute(): ?Server
{
$server = request()->route('server');
if (! $server) {
$server = Route::getRoutes()->match(Request::create(url()->previous()))->parameter('server');
}
if ($server instanceof Server) {
return $server;
}
if ($server) {
return Server::query()->find($server);
}
return null;
}
public function getHeaderWidgetsColumns(): int|string|array public function getHeaderWidgetsColumns(): int|string|array
{ {
return 1; return 1;

View File

@ -20,9 +20,9 @@ class Index extends Page
protected static ?string $title = 'SSH Keys'; protected static ?string $title = 'SSH Keys';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAnyServer', [SshKey::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [SshKey::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -17,9 +17,9 @@ class Index extends Page
protected static ?string $title = 'Services'; protected static ?string $title = 'Services';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Service::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Service::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -20,9 +20,9 @@ class Settings extends Page
protected $listeners = ['$refresh']; protected $listeners = ['$refresh'];
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('update', static::getServerFromRoute()) ?? false; $this->authorize('update', $this->server);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -24,9 +24,9 @@ class Index extends \App\Web\Pages\Servers\Page
protected static ?string $title = 'Sites'; protected static ?string $title = 'Sites';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Site::class, static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Site::class, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -2,7 +2,9 @@
namespace App\Web\Pages\Servers\Sites; namespace App\Web\Pages\Servers\Sites;
use App\Models\Queue;
use App\Models\Site; use App\Models\Site;
use App\Models\Ssl;
use App\Web\Contracts\HasSecondSubNav; use App\Web\Contracts\HasSecondSubNav;
use App\Web\Pages\Servers\Page as BasePage; use App\Web\Pages\Servers\Page as BasePage;
use App\Web\Pages\Servers\Sites\Widgets\SiteSummary; use App\Web\Pages\Servers\Sites\Widgets\SiteSummary;
@ -17,9 +19,10 @@ abstract class Page extends BasePage implements HasSecondSubNav
public function getSecondSubNavigation(): array public function getSecondSubNavigation(): array
{ {
$user = auth()->user();
$items = []; $items = [];
if (View::canAccess()) { if ($user->can('view', [$this->site, $this->server])) {
$items[] = NavigationItem::make(View::getNavigationLabel()) $items[] = NavigationItem::make(View::getNavigationLabel())
->icon('heroicon-o-globe-alt') ->icon('heroicon-o-globe-alt')
->isActiveWhen(fn () => request()->routeIs(View::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(View::getRouteName()))
@ -29,7 +32,7 @@ public function getSecondSubNavigation(): array
])); ]));
} }
if (Pages\SSL\Index::canAccess()) { if ($user->can('viewAny', [Ssl::class, $this->site, $this->server])) {
$items[] = NavigationItem::make(Pages\SSL\Index::getNavigationLabel()) $items[] = NavigationItem::make(Pages\SSL\Index::getNavigationLabel())
->icon('heroicon-o-lock-closed') ->icon('heroicon-o-lock-closed')
->isActiveWhen(fn () => request()->routeIs(Pages\SSL\Index::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(Pages\SSL\Index::getRouteName()))
@ -39,7 +42,7 @@ public function getSecondSubNavigation(): array
])); ]));
} }
if (Pages\Queues\Index::canAccess()) { if ($user->can('viewAny', [Queue::class, $this->site, $this->server])) {
$items[] = NavigationItem::make(Pages\Queues\Index::getNavigationLabel()) $items[] = NavigationItem::make(Pages\Queues\Index::getNavigationLabel())
->icon('heroicon-o-queue-list') ->icon('heroicon-o-queue-list')
->isActiveWhen(fn () => request()->routeIs(Pages\Queues\Index::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(Pages\Queues\Index::getRouteName()))
@ -49,7 +52,7 @@ public function getSecondSubNavigation(): array
])); ]));
} }
if (Settings::canAccess()) { if ($user->can('update', [$this->site, $this->server])) {
$items[] = NavigationItem::make(Settings::getNavigationLabel()) $items[] = NavigationItem::make(Settings::getNavigationLabel())
->icon('heroicon-o-wrench-screwdriver') ->icon('heroicon-o-wrench-screwdriver')
->isActiveWhen(fn () => request()->routeIs(Settings::getRouteName())) ->isActiveWhen(fn () => request()->routeIs(Settings::getRouteName()))

View File

@ -3,6 +3,7 @@
namespace App\Web\Pages\Servers\Sites\Pages\Queues; namespace App\Web\Pages\Servers\Sites\Pages\Queues;
use App\Actions\Queue\CreateQueue; use App\Actions\Queue\CreateQueue;
use App\Models\Queue;
use App\Web\Pages\Servers\Sites\Page; use App\Web\Pages\Servers\Sites\Page;
use Filament\Actions\Action; use Filament\Actions\Action;
use Filament\Actions\CreateAction; use Filament\Actions\CreateAction;
@ -18,9 +19,9 @@ class Index extends Page
protected static ?string $title = 'Queues'; protected static ?string $title = 'Queues';
public static function canAccess(): bool public function mount(): void
{ {
return true; $this->authorize('viewAny', [Queue::class, $this->site, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -20,9 +20,9 @@ class Index extends Page
protected static ?string $title = 'SSL'; protected static ?string $title = 'SSL';
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('viewAny', [Ssl::class, static::getSiteFromRoute(), static::getServerFromRoute()]) ?? false; $this->authorize('viewAny', [Ssl::class, $this->site, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -17,9 +17,9 @@ class Settings extends Page
protected $listeners = ['$refresh']; protected $listeners = ['$refresh'];
public static function canAccess(): bool public function mount(): void
{ {
return auth()->user()?->can('update', [static::getSiteFromRoute(), static::getServerFromRoute()]) ?? false; $this->authorize('update', [$this->site, $this->server]);
} }
public function getWidgets(): array public function getWidgets(): array

View File

@ -28,12 +28,9 @@ class View extends Page
public function mount(): void public function mount(): void
{ {
$this->previousStatus = $this->site->status; $this->authorize('view', [$this->site, $this->server]);
}
public static function canAccess(): bool $this->previousStatus = $this->site->status;
{
return auth()->user()?->can('view', [static::getSiteFromRoute(), static::getServerFromRoute()]) ?? false;
} }
#[On('$refresh')] #[On('$refresh')]

View File

@ -18,14 +18,10 @@ class View extends Page
public function mount(): void public function mount(): void
{ {
$this->authorize('view', $this->server);
$this->previousStatus = $this->server->status; $this->previousStatus = $this->server->status;
} }
public static function canAccess(): bool
{
return auth()->user()?->can('view', static::getServerFromRoute()) ?? false;
}
#[On('$refresh')] #[On('$refresh')]
public function refresh(): void public function refresh(): void
{ {

View File

@ -21,13 +21,13 @@ class Settings extends Page
protected static bool $shouldRegisterNavigation = false; protected static bool $shouldRegisterNavigation = false;
public static function canAccess(): bool
{
return auth()->user()?->can('update', get_from_route(Project::class, 'project')) ?? false;
}
public Project $project; public Project $project;
public function mount(): void
{
$this->authorize('update', $this->project);
}
public function getWidgets(): array public function getWidgets(): array
{ {
return [ return [

2505
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,7 @@
class="fi-tenant-menu-trigger group flex w-full items-center justify-center gap-x-3 rounded-lg p-2 text-sm font-medium outline-none transition duration-75 hover:bg-gray-100 focus-visible:bg-gray-100 dark:hover:bg-white/5 dark:focus-visible:bg-white/5" class="fi-tenant-menu-trigger group flex w-full items-center justify-center gap-x-3 rounded-lg p-2 text-sm font-medium outline-none transition duration-75 hover:bg-gray-100 focus-visible:bg-gray-100 dark:hover:bg-white/5 dark:focus-visible:bg-white/5"
> >
<div <div
class="bg-primary-700-gradient text-md flex size-8 items-center justify-center rounded-lg capitalize text-white" class="bg-primary-700-gradient text-md flex h-8 w-8 items-center justify-center rounded-lg capitalize text-white"
> >
{{ $currentProject->name[0] }} {{ $currentProject->name[0] }}
</div> </div>

View File

@ -5,8 +5,11 @@
use App\Enums\DeploymentStatus; use App\Enums\DeploymentStatus;
use App\Facades\SSH; use App\Facades\SSH;
use App\Models\GitHook; use App\Models\GitHook;
use App\Web\Pages\Servers\Sites\View;
use App\Web\Pages\Servers\Sites\Widgets\DeploymentsList;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class ApplicationTest extends TestCase class ApplicationTest extends TestCase
@ -18,28 +21,29 @@ public function test_visit_application()
$this->actingAs($this->user); $this->actingAs($this->user);
$this->get( $this->get(
route('servers.sites.show', [ View::getUrl([
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]) ])
) )
->assertSuccessful() ->assertSuccessful()
->assertSee($this->site->domain); ->assertSee($this->site->domain)
->assertSee('Deployments')
->assertSee('Actions');
} }
public function test_update_deployment_script() public function test_update_deployment_script()
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post( Livewire::test(View::class, [
route('servers.sites.application.deployment-script', [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]), ])
[ ->callAction('deployment-script', [
'script' => 'some script', 'script' => 'some script',
] ])
)->assertSessionDoesntHaveErrors(); ->assertSuccessful();
$this->assertDatabaseHas('deployment_scripts', [ $this->assertDatabaseHas('deployment_scripts', [
'site_id' => $this->site->id, 'site_id' => $this->site->id,
@ -59,7 +63,7 @@ public function test_deploy(): void
'email' => 'test@example.com', 'email' => 'test@example.com',
'url' => 'https://github.com/commit-url', 'url' => 'https://github.com/commit-url',
], ],
], 200), ]),
]); ]);
$this->site->deploymentScript->update([ $this->site->deploymentScript->update([
@ -68,13 +72,13 @@ public function test_deploy(): void
$this->actingAs($this->user); $this->actingAs($this->user);
$response = $this->post(route('servers.sites.application.deploy', [ Livewire::test(View::class, [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]))->assertSessionDoesntHaveErrors(); ])
->callAction('deploy')
$response->assertSessionHas('toast.type', 'success'); ->assertSuccessful()
$response->assertSessionHas('toast.message', 'Deployment started!'); ->assertNotified('Deployment started!');
$this->assertDatabaseHas('deployments', [ $this->assertDatabaseHas('deployments', [
'site_id' => $this->site->id, 'site_id' => $this->site->id,
@ -84,22 +88,23 @@ public function test_deploy(): void
SSH::assertExecutedContains('cd /home/vito/'.$this->site->domain); SSH::assertExecutedContains('cd /home/vito/'.$this->site->domain);
SSH::assertExecutedContains('git pull'); SSH::assertExecutedContains('git pull');
$this->get(route('servers.sites.show', [ $this->get(
View::getUrl([
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
])) ])
)
->assertSuccessful() ->assertSuccessful()
->assertSee('test commit message'); ->assertSee('test commit message');
$deployment = $this->site->deployments()->first(); $deployment = $this->site->deployments()->first();
$this->get(route('servers.sites.application.deployment.log', [ Livewire::test(DeploymentsList::class, [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
'deployment' => $deployment, ])
])) ->callTableAction('view', $deployment->id)
->assertRedirect() ->assertSuccessful();
->assertSessionHas('content', 'fake output');
} }
public function test_change_branch() public function test_change_branch()
@ -108,14 +113,15 @@ public function test_change_branch()
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.sites.application.branch', [ Livewire::test(View::class, [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]), [ ])
->callAction('branch', [
'branch' => 'master', 'branch' => 'master',
]) ])
->assertSessionDoesntHaveErrors() ->assertSuccessful()
->assertSessionHas('toast.type', 'success'); ->assertNotified('Branch updated!');
$this->site->refresh(); $this->site->refresh();
$this->assertEquals('master', $this->site->branch); $this->assertEquals('master', $this->site->branch);
@ -128,15 +134,18 @@ public function test_enable_auto_deployment()
Http::fake([ Http::fake([
'github.com/*' => Http::response([ 'github.com/*' => Http::response([
'id' => '123', 'id' => '123',
], 200), ], 201),
]); ]);
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.sites.application.auto-deployment', [ Livewire::test(View::class, [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]))->assertSessionDoesntHaveErrors(); ])
->callAction('auto-deployment')
->assertSuccessful()
->assertNotified('Auto deployment enabled!');
$this->site->refresh(); $this->site->refresh();
@ -154,15 +163,18 @@ public function test_disable_auto_deployment()
$this->actingAs($this->user); $this->actingAs($this->user);
$hook = GitHook::factory()->create([ GitHook::factory()->create([
'site_id' => $this->site->id, 'site_id' => $this->site->id,
'source_control_id' => $this->site->source_control_id, 'source_control_id' => $this->site->source_control_id,
]); ]);
$this->delete(route('servers.sites.application.auto-deployment', [ Livewire::test(View::class, [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]))->assertSessionDoesntHaveErrors(); ])
->callAction('auto-deployment')
->assertSuccessful()
->assertNotified('Auto deployment disabled!');
$this->site->refresh(); $this->site->refresh();
@ -175,12 +187,15 @@ public function test_update_env_file(): void
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.sites.application.env', [ Livewire::test(View::class, [
'server' => $this->server, 'server' => $this->server,
'site' => $this->site, 'site' => $this->site,
]), [ ])
->callAction('dot-env', [
'env' => 'APP_ENV="production"', 'env' => 'APP_ENV="production"',
])->assertSessionDoesntHaveErrors(); ])
->assertSuccessful()
->assertNotified('.env updated!');
SSH::assertFileUploaded('/home/vito/'.$this->site->domain.'/.env', 'APP_ENV="production"'); SSH::assertFileUploaded('/home/vito/'.$this->site->domain.'/.env', 'APP_ENV="production"');
} }
@ -200,7 +215,7 @@ public function test_git_hook_deployment(): void
], 200), ], 200),
]); ]);
$hook = GitHook::factory()->create([ GitHook::factory()->create([
'site_id' => $this->site->id, 'site_id' => $this->site->id,
'source_control_id' => $this->site->source_control_id, 'source_control_id' => $this->site->source_control_id,
'secret' => 'secret', 'secret' => 'secret',
@ -228,7 +243,7 @@ public function test_git_hook_deployment_invalid_secret(): void
SSH::fake(); SSH::fake();
Http::fake(); Http::fake();
$hook = GitHook::factory()->create([ GitHook::factory()->create([
'site_id' => $this->site->id, 'site_id' => $this->site->id,
'source_control_id' => $this->site->source_control_id, 'source_control_id' => $this->site->source_control_id,
'secret' => 'secret', 'secret' => 'secret',

View File

@ -3,6 +3,7 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Facades\SSH; use App\Facades\SSH;
use App\Web\Pages\Servers\Console\Index;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase; use Tests\TestCase;
@ -14,7 +15,7 @@ public function test_see_console(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->get(route('servers.console', $this->server)) $this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful() ->assertSuccessful()
->assertSeeText('Headless Console'); ->assertSeeText('Headless Console');
} }

View File

@ -5,7 +5,10 @@
use App\Enums\CronjobStatus; use App\Enums\CronjobStatus;
use App\Facades\SSH; use App\Facades\SSH;
use App\Models\CronJob; use App\Models\CronJob;
use App\Web\Pages\Servers\CronJobs\Index;
use App\Web\Pages\Servers\CronJobs\Widgets\CronJobsList;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class CronjobTest extends TestCase class CronjobTest extends TestCase
@ -21,7 +24,7 @@ public function test_see_cronjobs_list()
'server_id' => $this->server->id, 'server_id' => $this->server->id,
]); ]);
$this->get(route('servers.cronjobs', $this->server)) $this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful() ->assertSuccessful()
->assertSeeText($cronjob->frequencyLabel()); ->assertSeeText($cronjob->frequencyLabel());
} }
@ -38,10 +41,11 @@ public function test_delete_cronjob()
'user' => 'vito', 'user' => 'vito',
]); ]);
$this->delete(route('servers.cronjobs.destroy', [ Livewire::test(CronJobsList::class, [
'server' => $this->server, 'server' => $this->server,
'cronJob' => $cronjob, ])
]))->assertSessionDoesntHaveErrors(); ->callTableAction('delete', $cronjob->id)
->assertSuccessful();
$this->assertDatabaseMissing('cron_jobs', [ $this->assertDatabaseMissing('cron_jobs', [
'id' => $cronjob->id, 'id' => $cronjob->id,
@ -57,11 +61,15 @@ public function test_create_cronjob()
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.cronjobs.store', $this->server), [ Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('create', [
'command' => 'ls -la', 'command' => 'ls -la',
'user' => 'vito', 'user' => 'vito',
'frequency' => '* * * * *', 'frequency' => '* * * * *',
])->assertSessionDoesntHaveErrors(); ])
->assertSuccessful();
$this->assertDatabaseHas('cron_jobs', [ $this->assertDatabaseHas('cron_jobs', [
'server_id' => $this->server->id, 'server_id' => $this->server->id,
@ -81,12 +89,16 @@ public function test_create_custom_cronjob()
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.cronjobs.store', $this->server), [ Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('create', [
'command' => 'ls -la', 'command' => 'ls -la',
'user' => 'vito', 'user' => 'vito',
'frequency' => 'custom', 'frequency' => 'custom',
'custom' => '* * * 1 1', 'custom' => '* * * 1 1',
])->assertSessionDoesntHaveErrors(); ])
->assertSuccessful();
$this->assertDatabaseHas('cron_jobs', [ $this->assertDatabaseHas('cron_jobs', [
'server_id' => $this->server->id, 'server_id' => $this->server->id,
@ -115,10 +127,12 @@ public function test_enable_cronjob()
'status' => CronjobStatus::DISABLED, 'status' => CronjobStatus::DISABLED,
]); ]);
$this->post(route('servers.cronjobs.enable', [ Livewire::test(CronJobsList::class, [
'server' => $this->server, 'server' => $this->server,
'cronJob' => $cronjob, ])
]))->assertSessionDoesntHaveErrors(); ->assertTableActionHidden('disable', $cronjob->id)
->callTableAction('enable', $cronjob->id)
->assertSuccessful();
$cronjob->refresh(); $cronjob->refresh();
@ -143,10 +157,12 @@ public function test_disable_cronjob()
'status' => CronjobStatus::READY, 'status' => CronjobStatus::READY,
]); ]);
$this->post(route('servers.cronjobs.disable', [ Livewire::test(CronJobsList::class, [
'server' => $this->server, 'server' => $this->server,
'cronJob' => $cronjob, ])
]))->assertSessionDoesntHaveErrors(); ->assertTableActionHidden('enable', $cronjob->id)
->callTableAction('disable', $cronjob->id)
->assertSuccessful();
$cronjob->refresh(); $cronjob->refresh();

View File

@ -17,7 +17,10 @@ public function test_create_rules(): void
])); ]));
$this->assertSame([ $this->assertSame([
'webhook_url' => 'required|url', 'webhook_url' => [
'required',
'url',
],
], $provider->createRules([])); ], $provider->createRules([]));
} }

View File

@ -17,7 +17,10 @@ public function test_create_rules(): void
])); ]));
$this->assertSame([ $this->assertSame([
'email' => 'required|email', 'email' => [
'required',
'email',
],
], $provider->createRules([])); ], $provider->createRules([]));
} }

View File

@ -17,7 +17,10 @@ public function test_create_rules(): void
])); ]));
$this->assertSame([ $this->assertSame([
'webhook_url' => 'required|url', 'webhook_url' => [
'required',
'url',
],
], $provider->createRules([])); ], $provider->createRules([]));
} }

View File

@ -17,8 +17,12 @@ public function test_create_rules(): void
])); ]));
$this->assertSame([ $this->assertSame([
'bot_token' => 'required|string', 'bot_token' => [
'chat_id' => 'required', 'required',
],
'chat_id' => [
'required',
],
], $provider->createRules([])); ], $provider->createRules([]));
} }