update change password

This commit is contained in:
pbaodev
2025-08-24 22:41:15 +07:00
parent 6b358afd4b
commit 197fe413e5
19 changed files with 699 additions and 193 deletions
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\frontend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Vm\SnapShotRequest;
use App\Jobs\VirtualMachine\Action\ResetVmPasswordJob;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Repositories\Invoice\InvoiceRepositoryInterface;
@@ -552,109 +553,50 @@ class ServiceController extends Controller
/**
* Reset VM password (Windows/Linux) via vCenter helpers
*/
public function resetVMPassword(Request $request, vCenter $serviceVCenter)
public function resetVMPassword(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',
'new_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);
$newPassword = (string) $request->new_password;
// Load product and ensure ownership
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')
->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);
}
$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);
}
// Login to vCenter
if (!$serviceVCenter->loginServer($vcenter)) {
return response()->json([
'error' => true,
'message' => __('Không thể kết nối đến vCenter'),
], 500);
}
// Determine OS family
$osFamily = strtoupper($product->os->osGroup->name ?? '');
$isWindows = str_contains($osFamily, 'WINDOWS');
// Prepare data
$ip = $product->ipAddress->ip ?? null;
if (!$ip) {
return response()->json([
'error' => true,
'message' => __('Không tìm thấy IP của máy ảo'),
], 400);
}
$payload = [
'ip' => $ip,
'user' => $isWindows ? 'administrator' : 'root',
'password' => $product->password ?? 'TGS@123', // fallback if field exists; adjust per model
'new_password' => $newPassword,
];
$ok = $isWindows
? $serviceVCenter->change_password_windows($payload)
: $serviceVCenter->change_password_linux($payload);
if (!$ok) {
return response()->json([
'error' => true,
'message' => __('Không thể đặt lại mật khẩu. Vui lòng thử lại sau.'),
], 500);
}
return response()->json([
'error' => false,
'message' => __('Đặt lại mật khẩu thành công'),
]);
} catch (\Exception $e) {
Log::error('Lỗi reset VM password', [
'product_id' => $request->product_id ?? null,
'error' => $e->getMessage(),
]);
return response()->json([
'error' => true,
'message' => __('Có lỗi xảy ra khi đặt lại mật khẩu'),
], 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_password' => 'required|string|min:8|max:128',
'old_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);
$newPassword = (string) $request->new_password;
$oldPassword = (string) $request->old_password;
// 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
ResetVmPasswordJob::dispatch(
productId: $productId,
newPassword: $newPassword,
oldPassword: $oldPassword,
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 tạo đặt lại mật khẩu. Quá trình sẽ mất 3-5 phút, vui lòng chờ đợi.');
}
/**
* Change Remote Desktop port (Windows only)
*/
@@ -827,8 +769,8 @@ class ServiceController extends Controller
// Fetch product price based on invoice item link
$invoiceItem = InvoiceItem::whereHas('invoice', function ($q) use ($user) {
$q->where('customer_id', $user->id);
})
$q->where('customer_id', $user->id);
})
->where('product_type', 4) // VPS
->whereHas('invoice', fn($q) => $q->where('status', '!=', 0))
->first();
@@ -1131,7 +1073,7 @@ class ServiceController extends Controller
], 401);
}
$validator = \Validator::make($request->all(), [
$validator = Validator::make($request->all(), [
'product_id' => 'required|string|max:255',
]);
if ($validator->fails()) {
@@ -1158,7 +1100,7 @@ class ServiceController extends Controller
$result = $vCenterService->ShutdownGuestVirtualMachine($product, $product->vmware_id);
if (is_array($result) && isset($result['error']) && $result['error'] === false) {
// Clear VM info cache after stopping VM
\Cache::forget('vm_info_' . $product->vmware_id);
Cache::forget('the_gioi_so_database_the_gioi_so_cache:vm_info_' . $product->vmware_id);
return response()->json([
'error' => false,
'message' => __('Yêu cầu tắt máy ảo đã được gửi thành công.')
@@ -1287,9 +1229,9 @@ class ServiceController extends Controller
// Debug: Log invoice lookup
Log::info('Looking for invoice', ['invoice_id' => $request->invoice_id]);
$invoice = Invoice::find($request->invoice_id);
// Debug: Log invoice result
Log::info('Invoice lookup result', [
'invoice_id' => $request->invoice_id,