Files
portal-api/app/Http/Resources/Api/Client/Service/ServiceClientDetailResource.php
T
baodev0andClaude Sonnet 5.5 6315a392c0 feat(service): custom product, lịch sử giám sát VM và mã hoá response theo phiên
- Dịch vụ custom: bảng custom_products + services.custom_product_id; createService nhận
  custom{} thay product_id, giá = đơn giá tháng x số tháng chu kỳ (1/3/6/12/24/36),
  gia hạn tính theo cùng đơn giá, ServiceClientDetailResource trả custom_product và specs.
- Giám sát VM: GET v1/system/vm-metrics/history gom lịch sử về tối đa 240 điểm;
  lỗi lấy dung lượng đĩa không còn làm hỏng cả thẻ metrics.
- SealResponse: AES-256-GCM theo khoá phiên do proxy Next cấp qua header x-seal-key
  (HMAC của session id). Client opt-in bằng x-seal; chi tiết dịch vụ, mật khẩu VM và
  console ticket luôn được mã hoá. Body gửi lên dạng {enc} được giải mã trước validate;
  khoá lệch trả 428 để client lấy khoá mới. Đây là che mắt DevTools, không phải bảo mật
  chống chính chủ tài khoản.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 13:58:50 +07:00

158 lines
6.5 KiB
PHP

<?php
namespace App\Http\Resources\Api\Client\Service;
use App\Models\DnsDomain;
use App\Models\ModuleHostingPleskClient;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Crypt;
class ServiceClientDetailResource extends JsonResource
{
public const MASKED_PASSWORD = '*****';
/** Plesk panel login (persisted, reusable) — separate from the one-time FTP password, which is never stored. */
private function resolvePleskCredentials($hosting): ?array
{
$client = ModuleHostingPleskClient::query()
->where('customer_id', $hosting->customer_id)
->where('dns_manager_server_id', $hosting->dns_manager_server_id)
->first();
if (!$client) {
return null;
}
return [
'login' => $client->login,
'password' => Crypt::decryptString($client->password),
];
}
private function resolveDnsZone(): ?DnsDomain
{
if ($this->type !== 'domain' || !$this->server_name) {
return null;
}
return DnsDomain::query()
->whereRaw('LOWER(domain_name) = ?', [mb_strtolower(trim($this->server_name))])
->whereHas('customer', fn ($query) => $query->where('customers.id', $this->customer_id))
->first(['id', 'is_created']);
}
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
private function resolveSpecs($vm, $product, $custom = null): ?array
{
if ($custom) {
return [
'cpu' => (int) ($custom->cpu ?? 0),
'ram' => round(($custom->ram_mb ?? 0) / 1024, 2),
'disk' => (int) ($custom->disk_gb ?? 0),
'bandwidth' => (int) ($custom->bandwidth_gb ?? 0),
];
}
if ($vm && ($vm->cpu !== null || $vm->ram_mb !== null || $vm->disk_gb !== null)) {
return [
'cpu' => (int) ($vm->cpu ?? 0),
'ram' => round(($vm->ram_mb ?? 0) / 1024, 2),
'disk' => (int) ($vm->disk_gb ?? 0),
'bandwidth' => (int) ($product->bandwidth ?? 0),
];
}
$addons = $this->addons ?? collect();
if ($addons->isNotEmpty()) {
return [
'cpu' => (int) $addons->where('addon_type', 'cpu')->sum('addon_value'),
'ram' => round($addons->where('addon_type', 'ram')->sum('addon_value') / 1024, 2),
'disk' => (int) $addons->where('addon_type', 'disk')->sum('addon_value'),
'bandwidth' => (int) $addons->where('addon_type', 'bandwidth')->sum('addon_value'),
];
}
return $product ? [
'cpu' => (int) ($product->cpu ?? 0),
'ram' => (int) ($product->ram ?? 0),
'disk' => (int) ($product->disk ?? 0),
'bandwidth' => (int) ($product->bandwidth ?? 0),
] : null;
}
public function toArray(Request $request): array
{
if (!$this->resource) {
return [];
}
$orderItem = $this->orderItem;
$order = $orderItem?->order;
$vm = $this->resource->vmware;
$product = $orderItem?->product ?? $this->resource->product;
$custom = $this->resource->customProduct;
$renewalSource = $custom ?? $product;
$hosting = $this->resource->hostingAccount;
$pleskCredentials = $hosting ? $this->resolvePleskCredentials($hosting) : null;
$dnsZone = $this->resolveDnsZone();
$dnsCancelled = $dnsZone !== null && (int) $dnsZone->is_created === DnsDomain::STATUS_CANCELLED;
return [
'uuid' => $this->uuid,
'type' => $this->type,
'server_name' => $this->server_name,
'status' => $this->status,
'auto_renew' => $this->auto_renew,
'started_at' => $this->started_at?->toISOString(),
'expires_at' => $this->expires_at?->toISOString(),
'os' => [
'version' => $orderItem?->os?->version ?? $vm?->os?->version,
'group' => $orderItem?->os?->osGroup?->name ?? $vm?->os?->osGroup?->name,
],
'specs' => $this->type === 'hosting' ? null : $this->resolveSpecs($vm, $product, $custom),
'service_plan' => $this->type === 'hosting' ? $product?->servicePlan?->summary() : null,
'custom_product' => $custom?->summary(),
'order' => $order ? [
'total' => (float) $order->total,
'months' => (int) $orderItem->months,
'billing_label' => $orderItem->billing_label,
'cycle_price' => $custom
? (float) $custom->unit_price * (int) $orderItem->months
: (float) ($product?->pricings()
->where('billing_cycle', $orderItem->billing_cycle)
->where('is_active', 1)
->value('price') ?? 0),
'addon_total' => \App\Support\RenewalPricing::addonTotal($this->resource, 1),
'renewal_options' => \App\Support\RenewalPricing::options($renewalSource),
] : null,
'vm' => $vm ? [
'ip_address' => $vm->ipAddress?->ip ?? $vm->ip,
'hostname' => $vm->hostname,
'username' => $vm->user,
'password' => $vm->plain_password ? self::MASKED_PASSWORD : null,
'port' => $vm->port,
'status' => $vm->status,
'ip' => $vm->ip ?? null,
'dns' => $vm->dns ?? null,
] : null,
'hosting' => $hosting ? [
'ip_address' => $hosting->server?->publicHost(),
'plesk_url' => $hosting->server
? "http://{$hosting->server->publicHost()}:{$hosting->server->panel_port}/"
: null,
'hosting_plan' => $hosting->hosting_plan,
'ftp_username' => $hosting->ftp_username,
'username' => $pleskCredentials['login'] ?? null,
'password' => $pleskCredentials['password'] ?? null,
'status' => $hosting->status,
] : null,
'dns_domain_id' => $dnsZone && !$dnsCancelled ? $dnsZone->id : null,
'dns_cancelled' => $dnsCancelled,
];
}
}