62 lines
3.2 KiB
PHP
62 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\SystemRole;
|
|
use App\Jobs\SyncDeviceBaseCredential;
|
|
use App\Models\Device;
|
|
use App\Models\TenantDevicePolicy;
|
|
use App\Services\Devices\MikrotikActivationService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Throwable;
|
|
|
|
class DeviceActivationController extends Controller
|
|
{
|
|
public function __invoke(Device $device, MikrotikActivationService $service): RedirectResponse
|
|
{
|
|
$user = request()->user();
|
|
abort_if($user->is_platform_admin || $device->tenant_id !== $user->tenant_id, 404);
|
|
abort_unless($user->can('device.connect'), 403);
|
|
if ($user->hasRole(SystemRole::TenantUser->value)) {
|
|
$policy = TenantDevicePolicy::where('tenant_id', $user->tenant_id)->first();
|
|
abort_if(! $policy?->tenant_user_can_update_own || $device->created_by !== $user->id, 403);
|
|
}
|
|
|
|
$device->update(['activation_status' => 'processing', 'activation_message' => null]);
|
|
try {
|
|
$queuedOlt = ($device->vendor?->slug === 'zte' && $device->hasConnection('ssh'))
|
|
|| ($device->vendor?->slug === 'hsgq' && ($device->hasConnection('ssh') || $device->hasConnection('telnet')))
|
|
|| ($device->vendor?->slug === 'hisfocus' && ($device->hasConnection('ssh') || $device->hasConnection('telnet')));
|
|
if ($queuedOlt) {
|
|
$device->update(['base_sync_status' => 'queued', 'base_sync_error_code' => null, 'base_sync_message' => 'Aktivasi OLT menunggu queue worker.']);
|
|
SyncDeviceBaseCredential::dispatch($device->tenant_id, $device->id);
|
|
|
|
return back()->with('success', 'Aktivasi OLT dimasukkan ke queue. Status akan diperbarui setelah proses SSH selesai.');
|
|
}
|
|
|
|
$result = $service->activate($device->load('vendor'));
|
|
$info = $result['device_info'];
|
|
$deleted = count($result['cleanup']['deleted']);
|
|
$device->update([
|
|
'hostname' => $info['identity'] ?? $device->hostname,
|
|
'serial_number' => $info['serial-number'] ?? $device->serial_number,
|
|
'firmware_version' => $info['current-firmware'] ?? $device->firmware_version,
|
|
'software_version' => $info['version'] ?? $device->software_version,
|
|
'device_facts' => $info, 'status' => 'online', 'last_seen_at' => now(),
|
|
'activation_status' => 'active', 'activated_at' => now(),
|
|
'activation_message' => 'Base User RADIQ aktif'.($deleted > 0 ? "; {$deleted} user lama dihapus." : '.'),
|
|
'base_sync_status' => 'synced', 'base_sync_error_code' => null,
|
|
'base_sync_message' => 'Base User berhasil diverifikasi saat aktivasi.', 'base_synced_at' => now(),
|
|
]);
|
|
|
|
return back()->with('success', 'Perangkat aktif dan akun management RADIQ berhasil dibuat.');
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$message = str($exception->getMessage())->before(':')->limit(80)->toString();
|
|
$device->update(['activation_status' => 'failed', 'activation_message' => $message]);
|
|
|
|
return back()->withErrors(['activation' => 'Aktivasi gagal: '.$message]);
|
|
}
|
|
}
|
|
}
|