feat: Implement Plesk domain and hosting services

- Added PleskDomainService for managing domain operations (create, update, remove, get info).
- Introduced PleskHostingService for provisioning hosting accounts and notifying customers.
- Created PleskService for handling API requests to Plesk.
- Added migration to include 'type' column in dns_manager_servers table.
- Created migration for module_hosting_accounts table to track hosting subscriptions.
- Created migration for module_hosting_plesk_clients table to manage Plesk client credentials.
- Added email templates for notifying customers and admins about hosting account creation.
This commit is contained in:
baodev0
2026-08-06 17:24:01 +07:00
parent 79f06c2fa2
commit d6d63e5152
29 changed files with 1744 additions and 113 deletions
+175
View File
@@ -0,0 +1,175 @@
<?php
namespace App\Services\Plesk;
use App\Http\Facade\HelperFacade as Helper;
use App\Models\Customer;
use App\Models\DnsServer;
use App\Models\ModuleHostingPleskClient;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
/**
* Plesk `/clients` entry point — the customer's own Plesk Panel login,
* separate from the FTP/hosting credential PleskDomainService hands out.
* Plesk never returns a client's password after creation, so it's stored
* (encrypted) in module_hosting_plesk_clients — the only way to ever show
* it again on reuse.
*/
class PleskClientService
{
public function __construct(private PleskService $plesk)
{
}
/**
* @return array{error: bool, data?: array{id: int, login: string, password: ?string}, status?: int, message?: string}
*/
public function findOrCreateCustomer(DnsServer $server, Customer $customer): array
{
$existing = ModuleHostingPleskClient::query()
->where('customer_id', $customer->id)
->where('dns_manager_server_id', $server->id)
->first();
if ($existing) {
return [
'error' => false,
'data' => [
'id' => $existing->plesk_client_id,
'login' => $existing->login,
'password' => Crypt::decryptString($existing->password),
],
];
}
$login = $this->generateUsername($customer);
$password = Helper::general_password();
$created = $this->createCustomer($server, $customer, $login, $password);
if (!$created['error']) {
$this->persist($customer, $server, $created['data']['id'], $login, $password);
return ['error' => false, 'data' => ['id' => $created['data']['id'], 'login' => $login, 'password' => $password]];
}
// login already exists on this Plesk box but we have no local record of it (created
// out-of-band, or our own table was reset) — Plesk never gives back the old password,
// so reset it to one we generate now and persist that, instead of losing it again.
if (in_array($created['status'] ?? null, [400, 409], true)) {
$existingRemote = $this->findByLogin($server, $login);
if ($existingRemote) {
$reset = $this->resetPassword($server, (int) $existingRemote['id'], $password);
if ($reset) {
$this->persist($customer, $server, (int) $existingRemote['id'], $login, $password);
return ['error' => false, 'data' => ['id' => (int) $existingRemote['id'], 'login' => $login, 'password' => $password]];
}
Log::error('Plesk client found but password reset failed — reusing without a known password', [
'plesk_client_id' => $existingRemote['id'],
'login' => $login,
]);
return ['error' => false, 'data' => ['id' => (int) $existingRemote['id'], 'login' => $login, 'password' => null]];
}
}
return $created;
}
private function createCustomer(DnsServer $server, Customer $customer, string $login, string $password): array
{
$param = [
'name' => $customer->fullname ?: $login,
'login' => $login,
'email' => $customer->email,
'password' => $password,
'type' => 'customer',
];
try {
$response = $this->plesk->client($server, 30)
->post($this->plesk->url($server, 'clients'), $param);
$data = $response->json();
if ($response->successful() && isset($data['id'], $data['guid'])) {
return ['error' => false, 'data' => $data];
}
Log::error('Plesk client creation failed', ['status' => $response->status(), 'response' => $data]);
return [
'error' => true,
'status' => $response->status(),
'message' => $data['message'] ?? 'Plesk client creation failed',
];
} catch (RequestException $e) {
Log::error('Plesk client creation failed', ['error' => $e->getMessage(), 'response' => $e->response?->body()]);
return ['error' => true, 'status' => $e->response?->status(), 'message' => $e->getMessage()];
}
}
private function resetPassword(DnsServer $server, int $pleskClientId, string $password): bool
{
try {
$response = $this->plesk->client($server, 15)
->put($this->plesk->url($server, 'clients', "/{$pleskClientId}"), ['password' => $password]);
return $response->successful();
} catch (RequestException $e) {
Log::error('Plesk client password reset failed', ['error' => $e->getMessage(), 'plesk_client_id' => $pleskClientId]);
return false;
}
}
private function findByLogin(DnsServer $server, string $login): ?array
{
try {
$response = $this->plesk->client($server, 15)->get($this->plesk->url($server, 'clients'));
if (!$response->successful()) {
return null;
}
foreach ($response->json() ?? [] as $client) {
if (($client['login'] ?? null) === $login) {
return $client;
}
}
} catch (RequestException $e) {
Log::error($e->getMessage());
}
return null;
}
/**
* Plesk `login` is a short username, not the email (email is its own field
* on the same payload). Derived from the email's local part + customer id
* so it's stable and never collides across customers — needed because a
* 409 on create is treated as "this customer already has a client".
*/
private function generateUsername(Customer $customer): string
{
$localPart = strtolower((string) strstr($customer->email, '@', true) ?: 'user');
$sanitized = trim((string) preg_replace('/[^a-z0-9]+/', '_', $localPart), '_') ?: 'user';
return substr($sanitized, 0, 32) . '_' . $customer->id;
}
private function persist(Customer $customer, DnsServer $server, int $pleskClientId, string $login, string $password): void
{
ModuleHostingPleskClient::query()->updateOrCreate(
['customer_id' => $customer->id, 'dns_manager_server_id' => $server->id],
['plesk_client_id' => $pleskClientId, 'login' => $login, 'password' => Crypt::encryptString($password)]
);
}
}
+184
View File
@@ -0,0 +1,184 @@
<?php
namespace App\Services\Plesk;
use App\Models\DnsServer;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* Plesk `/domains` entry point — shared by the DNS module (ServerService)
* and the Hosting module (PleskHostingService). Extracted from
* ServerService so both stop drifting apart on the same Plesk call.
*/
class PleskDomainService
{
private ?string $lastError = null;
public function __construct(private PleskService $plesk)
{
}
/** Message from the most recent failed call — null if the last call succeeded or hasn't run yet. */
public function getLastError(): ?string
{
return $this->lastError;
}
public function createDomain(DnsServer $server, array $data = []): array|false
{
$this->lastError = null;
if (empty($data['domain_name'])) {
$this->lastError = 'Domain name is required';
return false;
}
$param = [
'name' => $data['domain_name'],
'description' => '',
'hosting_type' => 'virtual',
'hosting_settings' => [
'ftp_login' => $data['ftp_login'] ?? str_replace('.', '_', $data['domain_name']),
'ftp_password' => $data['ftp_password'] ?? Str::random(12),
],
'plan' => ['name' => $data['plan'] ?? 'Default Domain'],
];
// Omit ip_addresses/ipv4 unless explicitly given — Plesk assigns its
// own default IP for the domain, and a wrong hardcoded IP here gets
// rejected outright ("IP address 'x.x.x.x' is not available").
if (!empty($data['ip_addresses'])) {
$param['ip_addresses'] = $data['ip_addresses'];
$param['ipv4'] = $data['ip_addresses'];
}
if (!empty($data['owner_client_id'])) {
$param['owner_client'] = ['id' => $data['owner_client_id']];
}
try {
$response = $this->plesk->client($server, 30)
->post($this->plesk->url($server, 'domains'), $param);
$responseData = $response->json();
Log::info('Response Plesk API Created', ['data' => $responseData]);
if ($response->successful() && isset($responseData['id'], $responseData['guid'])) {
return $responseData + ['hosting_settings' => $param['hosting_settings']];
}
$this->lastError = $responseData['message'] ?? 'Plesk domain creation failed';
} catch (RequestException $e) {
$this->lastError = $e->response?->json('message') ?? $e->getMessage();
Log::error('Plesk domain creation failed', [
'error' => $e->getMessage(),
'response' => $e->response?->body(),
'data' => $param,
]);
}
return false;
}
public function updateDomain(DnsServer $server, int $pleskDomainId, array $data = []): array|false
{
$this->lastError = null;
if (empty($pleskDomainId) || empty($data)) {
$this->lastError = 'Invalid request';
return false;
}
$param = [
'name' => $data['domain_name'],
'description' => '',
'hosting_type' => 'virtual',
'hosting_settings' => [
'ftp_login' => $data['ftp_login'] ?? str_replace('.', '_', $data['domain_name']),
'ftp_password' => $data['ftp_password'] ?? Str::random(12),
],
'plan' => ['name' => $data['plan'] ?? 'Default Domain'],
];
if (!empty($data['ip_addresses'])) {
$param['ip_addresses'] = $data['ip_addresses'];
$param['ipv4'] = $data['ip_addresses'];
}
try {
$response = $this->plesk->client($server, 30)
->put($this->plesk->url($server, 'domains', "/{$pleskDomainId}"), $param);
$responseData = $response->json();
Log::info('Response Plesk API Updated', ['data' => $responseData]);
if ($response->successful() && isset($responseData['id'], $responseData['guid'])) {
return $responseData;
}
$this->lastError = $responseData['message'] ?? 'Plesk domain update failed';
} catch (RequestException $e) {
$this->lastError = $e->response?->json('message') ?? $e->getMessage();
Log::error('Plesk domain update failed', [
'error' => $e->getMessage(),
'response' => $e->response?->body(),
'data' => $param,
]);
}
return false;
}
public function removeDomain(DnsServer $server, int $pleskDomainId): bool
{
try {
$response = $this->plesk->client($server, 30)
->delete($this->plesk->url($server, 'domains', "/{$pleskDomainId}"));
if ($response->successful()) {
Log::info('Xoá domain thành công', ['plesk_id' => $pleskDomainId, 'status' => $response->status()]);
return true;
}
Log::error('Xoá domain thất bại', [
'plesk_id' => $pleskDomainId,
'status' => $response->status(),
'response' => $response->body(),
]);
return false;
} catch (\Exception $e) {
Log::error('Exception khi xoá domain', ['plesk_id' => $pleskDomainId, 'error' => $e->getMessage()]);
return false;
}
}
public function getDomainInfo(DnsServer $server, string $domain = ''): array|false
{
if (empty($domain)) {
return false;
}
try {
$response = $this->plesk->client($server, 10)
->get($this->plesk->url($server, 'domains'), ['name' => $domain]);
if ($response->successful()) {
foreach ($response->json() ?? [] as $domainInfo) {
if (isset($domainInfo['name']) && $domainInfo['name'] === $domain) {
return $domainInfo;
}
}
}
return false;
} catch (RequestException $e) {
Log::error($e->getMessage());
return false;
}
}
}
+152
View File
@@ -0,0 +1,152 @@
<?php
namespace App\Services\Plesk;
use App\Http\Facade\HelperFacade as Helper;
use App\Jobs\Mail\Admin\HostingAccountCreatedAdminNotificationJob;
use App\Jobs\Mail\Customer\HostingAccountCreatedJob;
use App\Models\Customer;
use App\Models\DnsServer;
use App\Models\ModuleHostingAccount;
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
use App\Repositories\Module\HostingAccount\HostingAccountRepositoryInterface;
/**
* Phase 1 orchestration: Create Customer -> Create Hosting Subscription ->
* Assign Plan -> persist module_hosting_accounts. No database/SSL/mail/git
* deploy — that's later phases.
*/
class PleskHostingService
{
public function __construct(
private PleskClientService $clients,
private PleskDomainService $domains,
private HostingAccountRepositoryInterface $accounts,
private DnsDomainRepositoryInterface $dnsDomains,
) {
}
public function provision(array $data): array
{
$server = DnsServer::find($data['dns_manager_server_id'] ?? null);
if (!$server) {
return ['error' => true, 'message' => 'Hosting server not found'];
}
if (!$server->servesHosting()) {
return ['error' => true, 'message' => 'This server does not support hosting'];
}
$customer = Customer::find($data['customer_id'] ?? null);
if (!$customer) {
return ['error' => true, 'message' => 'Customer not found'];
}
$client = $this->clients->findOrCreateCustomer($server, $customer);
if ($client['error']) {
return $client;
}
$pleskClientId = $client['data']['id'];
$pleskLogin = $client['data']['login'];
$pleskPassword = $client['data']['password'];
$ftpLogin = str_replace('.', '_', $data['domain_name']);
// Not persisted anywhere — the only record of it is this response, once.
$ftpPassword = $data['ftp_password'] ?? Helper::general_password();
$subscription = $this->domains->createDomain($server, [
'domain_name' => $data['domain_name'],
'ftp_login' => $ftpLogin,
'ftp_password' => $ftpPassword,
'plan' => $data['hosting_plan'] ?? null,
'owner_client_id' => $pleskClientId,
]);
if (!$subscription) {
return [
'error' => true,
'message' => $this->domains->getLastError() ?? 'Failed to create Plesk hosting subscription',
];
}
$domainInfo = $this->domains->getDomainInfo($server, $data['domain_name']) ?: [];
$dnsManagerDomainId = $data['dns_manager_domain_id'] ?? null;
if (!$dnsManagerDomainId) {
// Plesk's /domains object is the same entity the DNS module tracks —
// creating a hosting subscription there already created the DNS zone
// on Plesk, so mirror it locally instead of leaving it unlinked.
$dnsDomain = $this->dnsDomains->model->firstOrCreate(
[
'domain_name' => $data['domain_name'],
'dns_manager_server_id' => $server->id,
],
[
'plesk_id' => $subscription['id'],
'is_created' => 1,
]
);
$dnsManagerDomainId = $dnsDomain->id;
}
$account = $this->accounts->create([
'customer_id' => $customer->id,
'service_id' => $data['service_id'] ?? null,
'dns_manager_server_id' => $server->id,
'dns_manager_domain_id' => $dnsManagerDomainId,
'plesk_client_id' => $pleskClientId,
'plesk_subscription_id' => $subscription['id'],
'plesk_guid' => $subscription['guid'],
'hosting_plan' => $data['hosting_plan'] ?? null,
'ftp_username' => $subscription['hosting_settings']['ftp_login'] ?? $ftpLogin,
'system_username' => $subscription['hosting_settings']['ftp_login'] ?? $ftpLogin,
'document_root' => $domainInfo['www_root'] ?? null,
'status' => 'active',
]);
$this->notify($customer, $server, $account, $data['domain_name'], $ftpPassword, $pleskLogin, $pleskPassword);
return ['error' => false, 'data' => $account, 'ftp_password' => $ftpPassword];
}
private function notify(
Customer $customer,
DnsServer $server,
ModuleHostingAccount $account,
string $domainName,
string $ftpPassword,
string $pleskLogin,
?string $pleskPassword,
): void {
$shared = [
'customer_name' => $customer->fullname,
'customer_email' => $customer->email,
'domain_name' => $domainName,
'hosting_plan' => $account->hosting_plan,
'server_name' => $server->name,
'ftp_username' => $account->ftp_username,
'ftp_password' => $ftpPassword,
'plesk_login' => $pleskLogin,
'plesk_password' => $pleskPassword,
// Plesk's actual login page — not the :8443 REST API entry point.
'plesk_panel_url' => 'https://' . $server->ip_address . '/login_up.php',
'created_at' => now()->format('d/m/Y H:i:s'),
];
dispatch(new HostingAccountCreatedJob(array_merge($shared, [
'server_host' => $server->ip_address,
])));
dispatch(new HostingAccountCreatedAdminNotificationJob(array_merge($shared, [
'server_ip' => $server->ip_address,
'plesk_client_id' => $account->plesk_client_id,
'plesk_subscription_id' => $account->plesk_subscription_id,
])));
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace App\Services\Plesk;
use App\Facades\BaseHelperFacade as BaseHelper;
use App\Models\DnsServer;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
class PleskService
{
protected array $entryPoints = [
'clients' => ':8443/api/v2/clients',
'domains' => ':8443/api/v2/domains',
];
public function client(DnsServer $server, int $timeout = 30): PendingRequest
{
return Http::withoutVerifying()
->withBasicAuth($server->username, Crypt::decryptString($server->password))
->timeout($timeout);
}
public function url(DnsServer $server, string $entryPoint, string $suffix = ''): string
{
return BaseHelper::url($server->ip_address) . $this->entryPoints[$entryPoint] . $suffix;
}
}
+16 -113
View File
@@ -5,11 +5,11 @@ namespace App\Services;
use App\Facades\BaseHelperFacade as BaseHelper;
use App\Models\DnsServer;
use App\Repositories\DnsManager\Server\DnsServerRepositoryInterface;
use App\Services\Plesk\PleskDomainService;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class ServerService
{
@@ -33,7 +33,8 @@ class ServerService
];
public function __construct(
public DnsServerRepositoryInterface $serverRepository
public DnsServerRepositoryInterface $serverRepository,
private PleskDomainService $pleskDomainService,
) {
$this->server = $this->getServer();
}
@@ -205,32 +206,15 @@ class ServerService
public function getDomainInfo(string $domain = ''): array|false
{
if (empty($domain)) {
if (empty($domain) || empty($this->server)) {
return false;
}
try {
$response = Http::withoutVerifying()
->withBasicAuth($this->server->username, Crypt::decryptString($this->server->password))
->timeout(10)
->get(BaseHelper::url($this->server->ip_address) . $this->entry_points['domain'], ['name' => $domain]);
$result = $this->pleskDomainService->getDomainInfo($this->server, $domain);
$this->resetServer();
$this->resetServer();
if ($response->successful()) {
$domains = $response->json();
foreach ($domains as $domainInfo) {
if (isset($domainInfo['name']) && $domainInfo['name'] === $domain) {
return $domainInfo;
}
}
}
return false;
} catch (RequestException $e) {
Log::error($e->getMessage());
return false;
}
return $result;
}
public function createDomain(array $data = []): array|false
@@ -243,42 +227,11 @@ class ServerService
$this->server = $this->serverRepository->find($data['dns_manager_server_id']);
}
$param = [
'name' => $data['domain_name'],
'description' => '',
'hosting_type' => 'virtual',
'hosting_settings' => [
'ftp_login' => str_replace('.', '_', $data['domain_name']),
'ftp_password' => Str::random(12),
],
'ip_addresses' => ['103.42.58.124'],
'ipv4' => ['103.42.58.124'],
'plan' => ['name' => 'Default Domain'],
];
$result = $this->pleskDomainService->createDomain($this->server, $data);
try {
$response = Http::withoutVerifying()
->withBasicAuth($this->server->username, Crypt::decryptString($this->server->password))
->timeout(30)
->post(BaseHelper::url($this->server->ip_address) . $this->entry_points['domain'], $param);
$this->resetServer();
$this->resetServer();
$responseData = $response->json();
Log::info('Response Plesk API Created', ['data' => $responseData]);
if ($response->successful() && isset($responseData['id'], $responseData['guid'])) {
return $responseData;
}
} catch (RequestException $e) {
Log::error('Plesk domain creation failed', [
'error' => $e->getMessage(),
'response' => $e->response?->body(),
'data' => $param,
]);
}
return false;
return $result;
}
public function updateDomain(int $pleskDomainId, array $data = []): array|false
@@ -291,42 +244,11 @@ class ServerService
$this->server = $this->serverRepository->find($data['dns_manager_server_id']);
}
$param = [
'name' => $data['domain_name'],
'description' => '',
'hosting_type' => 'virtual',
'hosting_settings' => [
'ftp_login' => str_replace('.', '_', $data['domain_name']),
'ftp_password' => Str::random(12),
],
'ip_addresses' => ['103.42.58.124'],
'ipv4' => ['103.42.58.124'],
'plan' => ['name' => 'Default Domain'],
];
$result = $this->pleskDomainService->updateDomain($this->server, $pleskDomainId, $data);
try {
$response = Http::withoutVerifying()
->withBasicAuth($this->server->username, Crypt::decryptString($this->server->password))
->timeout(30)
->put(BaseHelper::url($this->server->ip_address) . $this->entry_points['domain'] . "/{$pleskDomainId}", $param);
$this->resetServer();
$this->resetServer();
$responseData = $response->json();
Log::info('Response Plesk API Updated', ['data' => $responseData]);
if ($response->successful() && isset($responseData['id'], $responseData['guid'])) {
return $responseData;
}
} catch (RequestException $e) {
Log::error('Plesk domain update failed', [
'error' => $e->getMessage(),
'response' => $e->response?->body(),
'data' => $param,
]);
}
return false;
return $result;
}
public function removeDomain(int $pleskDomainId): bool
@@ -335,30 +257,11 @@ class ServerService
return false;
}
try {
$response = Http::withoutVerifying()
->withBasicAuth($this->server->username, Crypt::decryptString($this->server->password))
->timeout(30)
->delete(BaseHelper::url($this->server->ip_address) . $this->entry_points['domain'] . "/{$pleskDomainId}");
$result = $this->pleskDomainService->removeDomain($this->server, $pleskDomainId);
$this->resetServer();
$this->resetServer();
if ($response->successful()) {
Log::info('Xoá domain thành công', ['plesk_id' => $pleskDomainId, 'status' => $response->status()]);
return true;
}
Log::error('Xoá domain thất bại', [
'plesk_id' => $pleskDomainId,
'status' => $response->status(),
'response' => $response->body(),
]);
return false;
} catch (\Exception $e) {
Log::error('Exception khi xoá domain', ['plesk_id' => $pleskDomainId, 'error' => $e->getMessage()]);
return false;
}
return $result;
}
public function addServerById(int $serverId): bool