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:
@@ -74,6 +74,7 @@ class ServerController extends Controller
|
||||
'password' => 'required|string|max:255',
|
||||
'api_key' => 'nullable|string',
|
||||
'status' => 'nullable|string|in:activated,Stopped',
|
||||
'type' => 'nullable|string|in:domain,hosting,both',
|
||||
]);
|
||||
|
||||
DB::beginTransaction();
|
||||
@@ -86,6 +87,7 @@ class ServerController extends Controller
|
||||
'password' => Crypt::encryptString($validated['password']),
|
||||
'api_key' => $validated['api_key'] ?? null,
|
||||
'status' => $validated['status'] ?? 'activated',
|
||||
'type' => $validated['type'] ?? 'domain',
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
@@ -116,6 +118,7 @@ class ServerController extends Controller
|
||||
'password' => 'nullable|string|max:255',
|
||||
'api_key' => 'nullable|string',
|
||||
'status' => 'nullable|string|in:activated,Stopped',
|
||||
'type' => 'nullable|string|in:domain,hosting,both',
|
||||
]);
|
||||
|
||||
DB::beginTransaction();
|
||||
@@ -182,6 +185,7 @@ class ServerController extends Controller
|
||||
'username' => $server->username,
|
||||
'api_key' => $server->api_key,
|
||||
'status' => $server->status,
|
||||
'type' => $server->type,
|
||||
'created_at' => $server->created_at,
|
||||
'updated_at' => $server->updated_at,
|
||||
];
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Hosting;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ModuleHosting\CreateHostingAccountRequest;
|
||||
use App\Http\Requests\ModuleHosting\UpdateHostingAccountRequest;
|
||||
use App\Http\Resources\Api\Admin\HostingAccountResource;
|
||||
use App\Repositories\Module\HostingAccount\HostingAccountRepositoryInterface;
|
||||
use App\Services\Plesk\PleskHostingService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HostingAccountController extends Controller
|
||||
{
|
||||
private const RELATIONSHIPS = ['customer', 'server', 'domain'];
|
||||
|
||||
public function __construct(
|
||||
private readonly HostingAccountRepositoryInterface $accounts,
|
||||
private readonly PleskHostingService $pleskHostingService,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 500);
|
||||
$filter = $request->get('filter');
|
||||
$status = $request->get('status');
|
||||
$customerId = $request->get('customer_id');
|
||||
|
||||
$query = $this->accounts->model
|
||||
->with(self::RELATIONSHIPS)
|
||||
->newQuery();
|
||||
|
||||
if ($filter) {
|
||||
$query->where(function ($sub) use ($filter) {
|
||||
$sub->where('hosting_plan', 'like', "%{$filter}%")
|
||||
->orWhere('ftp_username', 'like', "%{$filter}%")
|
||||
->orWhere('system_username', 'like', "%{$filter}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($status !== null && $status !== '') {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
|
||||
if ($customerId) {
|
||||
$query->where('customer_id', $customerId);
|
||||
}
|
||||
|
||||
$paginator = $query->orderByDesc('id')->paginate($perPage);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => HostingAccountResource::collection($paginator->getCollection()),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$account = $this->accounts->model->with(self::RELATIONSHIPS)->find($id);
|
||||
|
||||
if (!$account) {
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy hosting account #$id"], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'data' => new HostingAccountResource($account)]);
|
||||
}
|
||||
|
||||
public function store(CreateHostingAccountRequest $request): JsonResponse
|
||||
{
|
||||
$result = $this->pleskHostingService->provision($request->validated());
|
||||
|
||||
if ($result['error']) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $result['message'] ?? 'Không thể tạo hosting account trên Plesk',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$account = $result['data']->load(self::RELATIONSHIPS);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => new HostingAccountResource($account),
|
||||
// Not persisted anywhere — this response is the only place it's ever shown.
|
||||
'ftp_password' => $result['ftp_password'],
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(int $id, UpdateHostingAccountRequest $request): JsonResponse
|
||||
{
|
||||
$account = $this->accounts->find($id);
|
||||
|
||||
if (!$account) {
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy hosting account #$id"], 404);
|
||||
}
|
||||
|
||||
$this->accounts->update($id, $request->validated());
|
||||
|
||||
return $this->show($id);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$account = $this->accounts->find($id);
|
||||
|
||||
if (!$account) {
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy hosting account #$id"], 404);
|
||||
}
|
||||
|
||||
// Local record only — does not deprovision the subscription on Plesk,
|
||||
// same as DnsManager\DomainController::destroy leaving the Plesk side alone.
|
||||
$this->accounts->delete($id);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã xoá hosting account']);
|
||||
}
|
||||
|
||||
public function suspend(int $id): JsonResponse
|
||||
{
|
||||
return $this->notImplemented();
|
||||
}
|
||||
|
||||
public function unsuspend(int $id): JsonResponse
|
||||
{
|
||||
return $this->notImplemented();
|
||||
}
|
||||
|
||||
public function resetFtpPassword(int $id): JsonResponse
|
||||
{
|
||||
return $this->notImplemented();
|
||||
}
|
||||
|
||||
private function notImplemented(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Not implemented in this phase',
|
||||
], 501);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user