update fix deadcode, security
This commit is contained in:
@@ -3,18 +3,20 @@
|
||||
namespace App\Http\Controllers\Api\Admin\IpPool;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Imports\IpPoolImport;
|
||||
use App\Models\IpPool;
|
||||
use App\Models\ModuleIpAddress;
|
||||
use App\Models\ModuleNetwork;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Maatwebsite\Excel\Validators\ValidationException;
|
||||
use App\Http\Controllers\Api\Admin\IpPool\Concerns\ManagesIpPoolBulkActions;
|
||||
use App\Http\Controllers\Api\Admin\IpPool\Concerns\ReadsIpPoolUsage;
|
||||
|
||||
class IpPoolController extends Controller
|
||||
{
|
||||
use ManagesIpPoolBulkActions;
|
||||
use ReadsIpPoolUsage;
|
||||
|
||||
/**
|
||||
* Descriptive/CSV-import fields — plain pass-through, unlike gateway/cidr/netmask/
|
||||
* status/service_id which have defaulting + module_ip_address mirror logic below.
|
||||
@@ -341,204 +343,4 @@ class IpPoolController extends Controller
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã xoá IP Pool thành công']);
|
||||
}
|
||||
|
||||
public function bulkUpdateGateway(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'ids' => 'required|array|min:1',
|
||||
'ids.*' => 'integer',
|
||||
'gateway' => 'required|string|max:45',
|
||||
]);
|
||||
|
||||
$pools = IpPool::whereIn('id', $request->ids)->get(['id', 'ip_address', 'module_network_id']);
|
||||
|
||||
if ($pools->isEmpty()) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy IP Pool nào'], 404);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
IpPool::whereIn('id', $pools->pluck('id'))->update(['gateway' => $request->gateway]);
|
||||
|
||||
// Mirror into module_ip_address, grouped by network to keep this to a handful of queries.
|
||||
foreach ($pools->groupBy('module_network_id') as $networkId => $group) {
|
||||
ModuleIpAddress::whereIn('ip', $group->pluck('ip_address'))
|
||||
->where('module_network_id', $networkId)
|
||||
->update(['gateway' => $request->gateway]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã cập nhật gateway cho {$pools->count()} IP",
|
||||
'updated' => $pools->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function bulkDelete(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'ids' => 'required|array|min:1',
|
||||
'ids.*' => 'integer',
|
||||
]);
|
||||
|
||||
$ids = $request->ids;
|
||||
$pools = IpPool::whereIn('id', $ids)->get();
|
||||
|
||||
if ($pools->isEmpty()) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy IP Pool nào'], 404);
|
||||
}
|
||||
|
||||
// Block deletion if any IP is currently assigned
|
||||
$inUseIps = ModuleIpAddress::whereIn('ip', $pools->pluck('ip_address'))
|
||||
->where('status', 1)
|
||||
->pluck('ip')
|
||||
->toArray();
|
||||
|
||||
if (!empty($inUseIps)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không thể xoá — các IP sau đang được gán cho máy chủ: ' . implode(', ', $inUseIps),
|
||||
], 422);
|
||||
}
|
||||
|
||||
foreach ($pools as $p) {
|
||||
ModuleIpAddress::where('ip', $p->ip_address)
|
||||
->where('module_network_id', $p->module_network_id)
|
||||
->delete();
|
||||
}
|
||||
|
||||
$deleted = IpPool::whereIn('id', $ids)->delete();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã xoá {$deleted} IP Pool thành công",
|
||||
'deleted' => $deleted,
|
||||
]);
|
||||
}
|
||||
|
||||
public function import(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|file|mimes:csv,txt|max:10240',
|
||||
]);
|
||||
|
||||
$countBefore = IpPool::withTrashed()->count();
|
||||
|
||||
try {
|
||||
Excel::import(new IpPoolImport(), $request->file('file'));
|
||||
} catch (ValidationException $e) {
|
||||
$errors = [];
|
||||
foreach ($e->failures() as $failure) {
|
||||
$errors[] = 'Dòng ' . $failure->row() . ': ' . implode(', ', $failure->errors());
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Import thất bại',
|
||||
'errors' => $errors,
|
||||
], 422);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => true, 'message' => 'Import thất bại: ' . $e->getMessage()], 500);
|
||||
}
|
||||
|
||||
$countAfter = IpPool::withTrashed()->count();
|
||||
|
||||
IpPool::resyncAllContacts();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Import thành công',
|
||||
'created' => max(0, $countAfter - $countBefore),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Backfill/repair the module_ip_address mirror for every ip_pools row.
|
||||
* Existing mirror rows only get gateway/netmask refreshed — status/vm_name
|
||||
* (assigned VM state) is left untouched. Missing rows are created with
|
||||
* status derived from the current ip_pools.status.
|
||||
*/
|
||||
public function sync(): JsonResponse
|
||||
{
|
||||
$created = 0;
|
||||
$updated = 0;
|
||||
$hostByNet = ModuleNetwork::pluck('module_host_id', 'id');
|
||||
|
||||
IpPool::chunkById(500, function ($pools) use (&$created, &$updated, $hostByNet) {
|
||||
foreach ($pools as $pool) {
|
||||
$hostId = $hostByNet[$pool->module_network_id] ?? null;
|
||||
$netmask = $pool->netmask ?: '255.255.255.0';
|
||||
|
||||
$mia = ModuleIpAddress::where('ip', $pool->ip_address)
|
||||
->where('module_network_id', $pool->module_network_id)
|
||||
->first();
|
||||
|
||||
if ($mia) {
|
||||
$mia->update([
|
||||
'module_host_id' => $hostId,
|
||||
'gateway' => $pool->gateway,
|
||||
'netmask' => $netmask,
|
||||
]);
|
||||
$updated++;
|
||||
} else {
|
||||
ModuleIpAddress::create([
|
||||
'ip' => $pool->ip_address,
|
||||
'module_network_id' => $pool->module_network_id,
|
||||
'module_host_id' => $hostId,
|
||||
'gateway' => $pool->gateway,
|
||||
'netmask' => $netmask,
|
||||
'status' => $pool->status === 'assigned' ? 1 : 0,
|
||||
]);
|
||||
$created++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã đồng bộ: tạo mới {$created}, cập nhật {$updated}",
|
||||
'created' => $created,
|
||||
'updated' => $updated,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Service IDs already tied to an IP — either directly via ip_pools.service_id,
|
||||
* or indirectly via a VM (module_vmware) sitting on that IP. Used by the admin
|
||||
* form to disable services that are already in use on another IP.
|
||||
*/
|
||||
public function usedServices(): JsonResponse
|
||||
{
|
||||
$direct = IpPool::whereNotNull('service_id')
|
||||
->select(['id as ip_pool_id', 'service_id']);
|
||||
|
||||
$viaVm = DB::table('ip_pools as ip')
|
||||
->join('module_ip_address as mia', function ($join) {
|
||||
$join->on('mia.ip', '=', 'ip.ip_address')
|
||||
->on('mia.module_network_id', '=', 'ip.module_network_id');
|
||||
})
|
||||
->join('module_vmware as mv', 'mv.module_ip_address', '=', 'mia.id')
|
||||
->whereNotNull('mv.service_id')
|
||||
->select(['ip.id as ip_pool_id', 'mv.service_id']);
|
||||
|
||||
$used = $direct->toBase()->union($viaVm)->get()->unique(fn ($row) => "{$row->service_id}-{$row->ip_pool_id}")->values();
|
||||
|
||||
return response()->json(['error' => false, 'data' => $used]);
|
||||
}
|
||||
|
||||
public function networks(): JsonResponse
|
||||
{
|
||||
$networks = ModuleNetwork::select(['id', 'name', 'type', 'network', 'module_host_id'])
|
||||
->with(['host:id,name,module_datacenter_id', 'host.datacenter:id,name'])
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
return response()->json(['error' => false, 'data' => $networks]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user