This commit is contained in:
Saeed Vaziry
2024-12-14 03:13:47 +01:00
committed by GitHub
parent 0d12dd0c69
commit 572d1df010
20 changed files with 734 additions and 67 deletions

View File

@ -189,7 +189,7 @@ private static function providerRules(array $input): array
return $server->provider()->createRules($input);
}
private function createFirewallRules(Server $server): void
public function createFirewallRules(Server $server): void
{
$server->firewallRules()->createMany([
[

View File

@ -0,0 +1,96 @@
<?php
namespace App\Providers;
use App\Facades\SSH;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
class DemoServiceProvider extends ServiceProvider
{
protected string $error = 'Cannot modify on Demo!';
protected array $canDelete = [
//
];
protected array $canUpdate = [
'App\Models\ServerLog',
'App\Models\Script',
'App\Models\ScriptExecution',
];
protected array $canCreate = [
'App\Models\ServerLog',
'App\Models\Script',
'App\Models\ScriptExecution',
'App\Models\FirewallRule',
'App\Models\PersonalAccessToken',
];
public function register(): void
{
//
}
public function boot(): void
{
if (! config('app.demo') || app()->runningInConsole()) {
return;
}
// get all classes inside App\Models namespace
$models = collect(scandir(app_path('Models')))
->filter(fn ($file) => ! in_array($file, ['.', '..']))
->map(fn ($file) => 'App\\Models\\'.str_replace('.php', '', $file));
foreach ($models as $model) {
if (! in_array($model, $this->canCreate)) {
$this->preventCreating($model);
}
if (! in_array($model, $this->canUpdate)) {
$this->preventUpdating($model);
}
if (! in_array($model, $this->canDelete)) {
$this->preventDeletion($model);
}
}
SSH::fake('Demo SSH is enabled. No SSH commands will be executed.');
Http::fake([
'*' => Http::response([]),
]);
config()->set('queue.default', 'sync');
config()->set('logging.default');
config()->set('session.driver', 'file');
}
private function preventUpdating(string $model): void
{
$model::updating(function ($m) {
$throw = true;
if ($m instanceof User && ! $m->isDirty(['name', 'email', 'password', 'two_factor_secret', 'two_factor_recovery_codes'])) {
$throw = false;
}
if ($throw) {
abort(403, $this->error);
}
});
}
private function preventDeletion(string $model): void
{
$model::deleting(function ($m) {
abort(403, $this->error);
});
}
private function preventCreating(string $model): void
{
$model::creating(function ($m) {
abort(403, $this->error);
});
}
}

View File

@ -4,6 +4,7 @@
use App\Exceptions\SSHConnectionError;
use App\Helpers\SSH;
use App\Models\Server;
use Illuminate\Support\Traits\ReflectsClosures;
use PHPUnit\Framework\Assert;
@ -28,6 +29,21 @@ public function __construct(?string $output = null)
$this->output = $output;
}
public function init(Server $server, ?string $asUser = null): self
{
$this->connection = null;
$this->log = null;
$this->asUser = null;
$this->server = $server->refresh();
$this->user = $server->getSshUser();
if ($asUser && $asUser != $server->getSshUser()) {
$this->user = $asUser;
$this->asUser = $asUser;
}
return $this;
}
public function connectionWillFail(): void
{
$this->connectionWillFail = true;

View File

@ -24,6 +24,13 @@ public function mount(): void
$this->initTwoFactor();
$this->form->fill();
if (config('app.demo')) {
$this->form->fill([
'email' => 'demo@vitodeploy.com',
'password' => 'password',
]);
}
}
public function logoutAction(): Action