Files
jabali-panel/app/Filament/Admin/Resources/GeoBlockRules/Pages/ListGeoBlockRules.php

82 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Admin\Resources\GeoBlockRules\Pages;
use App\Filament\Admin\Resources\GeoBlockRules\GeoBlockRuleResource;
use App\Models\DnsSetting;
use App\Services\Agent\AgentClient;
use Exception;
use Filament\Actions\Action;
use Filament\Actions\CreateAction;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
class ListGeoBlockRules extends ListRecords
{
protected static string $resource = GeoBlockRuleResource::class;
protected function getHeaderActions(): array
{
return [
Action::make('updateGeoIpDatabase')
->label(__('Update GeoIP Database'))
->icon('heroicon-o-arrow-down-tray')
->form([
TextInput::make('account_id')
->label(__('MaxMind Account ID'))
->required()
->default(fn (): string => (string) (DnsSetting::get('geoip_account_id') ?? '')),
TextInput::make('license_key')
->label(__('MaxMind License Key'))
->password()
->revealable()
->required()
->default(fn (): string => (string) (DnsSetting::get('geoip_license_key') ?? '')),
TextInput::make('edition_ids')
->label(__('Edition IDs'))
->helperText(__('Comma-separated (default: GeoLite2-Country)'))
->default(fn (): string => (string) (DnsSetting::get('geoip_edition_ids') ?? 'GeoLite2-Country')),
])
->action(function (array $data): void {
$accountId = trim((string) ($data['account_id'] ?? ''));
$licenseKey = trim((string) ($data['license_key'] ?? ''));
$editionIds = trim((string) ($data['edition_ids'] ?? 'GeoLite2-Country'));
if ($accountId === '' || $licenseKey === '') {
Notification::make()
->title(__('MaxMind credentials are required'))
->danger()
->send();
return;
}
DnsSetting::set('geoip_account_id', $accountId);
DnsSetting::set('geoip_license_key', $licenseKey);
DnsSetting::set('geoip_edition_ids', $editionIds);
try {
$agent = new AgentClient;
$result = $agent->geoUpdateDatabase($accountId, $licenseKey, $editionIds);
Notification::make()
->title(__('GeoIP database updated'))
->body($result['path'] ?? null)
->success()
->send();
} catch (Exception $e) {
Notification::make()
->title(__('GeoIP update failed'))
->body($e->getMessage())
->danger()
->send();
}
}),
CreateAction::make(),
];
}
}