update port remote change
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\Invoice;
|
||||
use App\Models\InvoiceItem;
|
||||
use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
||||
use App\Http\Services\vCenter;
|
||||
use App\Jobs\VirtualMachine\Action\ChangeRemotePortJob;
|
||||
use App\Models\ModuleSnapshot;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\Order;
|
||||
@@ -600,85 +601,44 @@ class ServiceController extends Controller
|
||||
/**
|
||||
* Change Remote Desktop port (Windows only)
|
||||
*/
|
||||
public function changeRemotePort(Request $request, vCenter $serviceVCenter)
|
||||
public function changeRemotePort(Request $request)
|
||||
{
|
||||
try {
|
||||
$user = customer();
|
||||
if (!$user) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Chưa đăng nhập. Vui lòng đăng nhập để sử dụng tính năng này.'),
|
||||
], 401);
|
||||
}
|
||||
|
||||
$validated = FacadesValidator::make($request->all(), [
|
||||
'product_id' => 'required|string|max:255',
|
||||
'port' => 'required|integer|min:1|max:65535',
|
||||
]);
|
||||
|
||||
if ($validated->fails()) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Dữ liệu không hợp lệ'),
|
||||
'errors' => $validated->errors(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
$productId = $this->sanitizeInput($request->product_id);
|
||||
$newPort = (int) $request->port;
|
||||
|
||||
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter', 'os.osGroup')
|
||||
->where('vmware_id', $productId)
|
||||
->where('customer_id', $user->id)
|
||||
->first();
|
||||
|
||||
if (!$product) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Không tìm thấy VPS hoặc bạn không có quyền truy cập'),
|
||||
], 404);
|
||||
}
|
||||
|
||||
$osFamily = strtoupper($product->os->osGroup->name ?? '');
|
||||
if (!str_contains($osFamily, 'WINDOWS')) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Chỉ hỗ trợ Windows'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
$vcenter = $product->ipAddress->host->datacenter->vCenter ?? null;
|
||||
if (!$vcenter) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Cấu hình vCenter không hợp lệ'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
if (!$serviceVCenter->loginServer($vcenter)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Không thể kết nối đến vCenter'),
|
||||
], 500);
|
||||
}
|
||||
|
||||
// NOTE: Implement the actual change RDP port over WinRM/PowerShell here.
|
||||
// Placeholder success until service method is available.
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu đổi port RDP đã được ghi nhận'),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi changeRemotePort', [
|
||||
'product_id' => $request->product_id ?? null,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Có lỗi xảy ra khi đổi port'),
|
||||
], 500);
|
||||
$user = customer();
|
||||
if (!$user) {
|
||||
return response()->json(['error' => true, 'message' => __('Chưa đăng nhập. Vui lòng đăng nhập để sử dụng tính năng này.')], 401);
|
||||
}
|
||||
|
||||
$validated = Validator::make($request->all(), [
|
||||
'product_id' => 'required|string|max:255',
|
||||
'new_port' => 'required|string|min:4|max:128',
|
||||
'password' => 'required|string|min:8|max:128',
|
||||
]);
|
||||
if ($validated->fails()) {
|
||||
return response()->json(['error' => true, 'message' => __('Dữ liệu không hợp lệ'), 'errors' => $validated->errors()], 422);
|
||||
}
|
||||
|
||||
$productId = $this->sanitizeInput($request->product_id);
|
||||
$newPort = (string) $request->new_port;
|
||||
|
||||
// load & ownership check nhanh (không cần toàn bộ nặng nề vì Job sẽ retry)
|
||||
$product = ModuleVmware::where('vmware_id', $productId)
|
||||
->where('customer_id', $user->id)
|
||||
->first();
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => __('Không tìm thấy VPS hoặc bạn không có quyền truy cập')], 404);
|
||||
}
|
||||
|
||||
// Dispatch job với delay 5s
|
||||
ChangeRemotePortJob::dispatch(
|
||||
productId: $productId,
|
||||
newPort: $newPort,
|
||||
password: (string) $request->password,
|
||||
userId: $user->id,
|
||||
email: $user->email
|
||||
)->delay(now()->addSeconds(5));
|
||||
|
||||
// trả accepted để UI biết đang xử lý nền
|
||||
return redirect()->back()->with('success', 'Đã gửi yêu cầu thay đổi cổng Remote Desktop. Quá trình sẽ mất 3-5 phút, vui lòng chờ đợi.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user