Files
portal-api/app/Services/Customer/CustomerImpersonationService.php
baodev0andClaude Opus 5.5 c5115b52dd feat: nạp tiền thủ công mã 30 phút, gia hạn trừ số dư, đồng bộ domain từ Plesk
- Nạp tiền: mã thanh toán sống 30 phút (ManualDepositDraft), khôi phục/huỷ mã, lệnh dọn mã hết hạn; total_pending chỉ tính giao dịch đã nộp chứng từ, admin duyệt/từ chối/xoá giao dịch
- Gia hạn dịch vụ: phát hành invoice gia hạn trên order gốc và trừ số dư ngay
- Đồng bộ domain từ Plesk theo server, ServiceOrderRecorder tạo order/invoice cho dịch vụ admin dựng
- Danh sách dịch vụ trả field theo loại (service_plan, dns_records_count), đếm dịch vụ theo loại

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 23:07:51 +07:00

62 lines
2.0 KiB
PHP

<?php
namespace App\Services\Customer;
use App\Http\Resources\Api\Client\ClientResource;
use App\Models\Log;
use App\Models\User;
use App\Repositories\Customer\CustomerRepositoryInterface;
use App\Services\Api\Client\ClientAuthService;
use App\Services\Customer\Concerns\ResolvesCustomer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Tymon\JWTAuth\Facades\JWTAuth;
class CustomerImpersonationService
{
use ResolvesCustomer;
private const TTL_SECONDS = 120;
public function __construct(
private readonly CustomerRepositoryInterface $customerRepository,
private readonly ClientAuthService $clientAuthService,
) {}
/**
* Tạo mã impersonate một lần (hiệu lực 120s) để admin đăng nhập hộ khách hàng.
* portal-client đổi code lấy token qua endpoint public `v1/client/impersonate/exchange`.
*/
public function createLoginCode(string $uuid, User $admin, Request $request): string
{
$customer = $this->resolveCustomer($uuid);
abort_if((int) $customer->status !== 1, 422, 'Khách hàng không hoạt động, không thể đăng nhập hộ.');
$token = JWTAuth::fromUser($customer);
$refreshToken = $this->clientAuthService->createRefreshToken($customer->id);
$clientResource = (new ClientResource($customer))->resolve($request);
$code = Str::random(48);
Cache::put("impersonate:{$code}", [
'token' => $token,
'refresh_token' => $refreshToken,
'client' => $clientResource,
], self::TTL_SECONDS);
Log::create([
'agent' => 'admin',
'client_ip' => $request->ip(),
'description' => sprintf(
'%s đăng nhập hộ khách hàng %s',
$admin->username ?? $admin->email ?? 'Admin',
$customer->email,
),
'customer_id' => $customer->id,
]);
return $code;
}
}