Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.6.48",
"version": "1.6.49",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
2 changes: 1 addition & 1 deletion seeders/Concerns/ResolvesSeedCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait ResolvesSeedCompany
{
protected function resolveSeedCompany(?string $fallbackUuidEnv = null, ?string $fallbackPublicIdEnv = null): ?Company
{
$companyUuid = env('SEED_COMPANY_UUID') ?: ($fallbackUuidEnv ? env($fallbackUuidEnv) : null);
$companyUuid = env('SEED_COMPANY_UUID') ?: ($fallbackUuidEnv ? env($fallbackUuidEnv) : null);
$companyPublicId = env('SEED_COMPANY_PUBLIC_ID') ?: ($fallbackPublicIdEnv ? env($fallbackPublicIdEnv) : null);

if ($companyUuid) {
Expand Down
65 changes: 65 additions & 0 deletions src/Console/Commands/NotifyInstalled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Fleetbase\Console\Commands;

use Fleetbase\Support\SocketCluster\SocketClusterService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class NotifyInstalled extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fleetbase:notify-installed {--channel=fleetbase.install}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Notify open install pages that Fleetbase setup has completed';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$channel = $this->option('channel') ?: 'fleetbase.install';
$payload = [
'event' => 'fleetbase.installed',
'installed' => true,
'timestamp' => now()->toIso8601String(),
];

try {
$socketClusterClient = new SocketClusterService();
$sent = $socketClusterClient->send($channel, $payload);

if (!$sent) {
$message = $socketClusterClient->error() ?: 'SocketCluster did not acknowledge the install notification.';
$this->warn('Install notification was not sent: ' . $message);
Log::warning('Fleetbase install notification was not sent.', [
'channel' => $channel,
'error' => $message,
]);

return 0;
}

$this->info('Install notification sent.');
} catch (\Throwable $e) {
$this->warn('Install notification failed: ' . $e->getMessage());
Log::warning('Fleetbase install notification failed.', [
'channel' => $channel,
'error' => $e->getMessage(),
]);
}

return 0;
}
}
29 changes: 0 additions & 29 deletions src/Http/Controllers/Internal/v1/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -218,37 +217,9 @@ public function bootstrap(Request $request)
->distinct()
->get();

// Get installer status (cached separately)
$installer = Cache::remember('installer_status', now()->addHour(), function () {
$shouldInstall = false;
$shouldOnboard = false;

try {
DB::connection()->getPdo();
if (DB::connection()->getDatabaseName()) {
if (\Illuminate\Support\Facades\Schema::hasTable('companies')) {
$shouldOnboard = !DB::table('companies')->exists();
} else {
$shouldInstall = true;
}
} else {
$shouldInstall = true;
}
} catch (\Exception $e) {
$shouldInstall = true;
}

return [
'shouldInstall' => $shouldInstall,
'shouldOnboard' => $shouldOnboard,
'defaultTheme' => \Fleetbase\Models\Setting::lookup('branding.default_theme', 'dark'),
];
});

return [
'session' => $session,
'organizations' => Organization::collection($organizations),
'installer' => $installer,
];
});

Expand Down
135 changes: 0 additions & 135 deletions src/Http/Controllers/Internal/v1/InstallerController.php

This file was deleted.

79 changes: 79 additions & 0 deletions src/Http/Middleware/EnsureFleetbaseConfigured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Fleetbase\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class EnsureFleetbaseConfigured
{
/**
* Core tables required before the API can serve Fleetbase requests.
*
* @var array<int, string>
*/
protected array $requiredTables = [
'settings',
'users',
'companies',
];

protected static bool $configured = false;

public function handle(Request $request, \Closure $next)
{
if (!$this->shouldCheck($request)) {
return $next($request);
}

if (!$this->isConfigured()) {
return response()->json([
'error' => 'fleetbase_not_configured',
'errors' => ['fleetbase_not_configured'],
'message' => 'Fleetbase is not installed or configured. Complete setup from the CLI or application container, then reload the console.',
], 503);
}

return $next($request);
}

protected function shouldCheck(Request $request): bool
{
if ($request->isMethod('OPTIONS')) {
return false;
}

return $request->is('int/*')
|| $request->is('*/int/*')
|| $request->is('v1/*')
|| $request->is('*/v1/*');
}

protected function isConfigured(): bool
{
if (static::$configured) {
return true;
}

try {
DB::connection()->getPdo();

if (!DB::connection()->getDatabaseName()) {
return false;
}

foreach ($this->requiredTables as $table) {
if (!Schema::hasTable($table)) {
return false;
}
}

static::$configured = true;

return true;
} catch (\Throwable $e) {
return false;
}
}
}
2 changes: 2 additions & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CoreServiceProvider extends ServiceProvider
\Fleetbase\Http\Middleware\RequestTimer::class,
\Fleetbase\Http\Middleware\ResetJsonResourceWrap::class,
\Fleetbase\Http\Middleware\MergeConfigFromSettings::class,
\Fleetbase\Http\Middleware\EnsureFleetbaseConfigured::class,
\Fleetbase\Http\Middleware\AttachCacheHeaders::class,
];

Expand Down Expand Up @@ -85,6 +86,7 @@ class CoreServiceProvider extends ServiceProvider
\Fleetbase\Console\Commands\InitializeSandboxKeyColumn::class,
\Fleetbase\Console\Commands\SyncSandbox::class,
\Fleetbase\Console\Commands\CreatePermissions::class,
\Fleetbase\Console\Commands\NotifyInstalled::class,
\Fleetbase\Console\Commands\FixUserCompanies::class,
\Fleetbase\Console\Commands\PurgeApiLogs::class,
\Fleetbase\Console\Commands\PurgeWebhookLogs::class,
Expand Down
Loading
Loading