Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hosting account management functionality #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions app/Services/ControlPanels/CpanelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Services\ControlPanels;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;

class CpanelClient
{
Expand All @@ -19,21 +21,79 @@ public function __construct()

public function createAccount($username, $domain, $package)
{
// Implement cPanel API call to create account
$endpoint = '/createacct';
$params = [
'username' => $username,
'domain' => $domain,
'plan' => $package,
];

return $this->makeApiCall($endpoint, $params);
}

public function suspendAccount($username)
{
// Implement cPanel API call to suspend account
$endpoint = '/suspendacct';
$params = [
'user' => $username,
];

return $this->makeApiCall($endpoint, $params);
}

public function unsuspendAccount($username)
{
// Implement cPanel API call to unsuspend account
$endpoint = '/unsuspendacct';
$params = [
'user' => $username,
];

return $this->makeApiCall($endpoint, $params);
}

public function changePackage($username, $newPackage)
{
// Implement cPanel API call to change package
$endpoint = '/changepackage';
$params = [
'user' => $username,
'pkg' => $newPackage,
];

return $this->makeApiCall($endpoint, $params);
}

public function terminateAccount($username)
{
$endpoint = '/removeacct';
$params = [
'user' => $username,
];

return $this->makeApiCall($endpoint, $params);
}

protected function makeApiCall($endpoint, $params)
{
try {
$response = $this->client->request('POST', $this->apiUrl . $endpoint, [
'headers' => [
'Authorization' => 'WHM ' . $this->apiToken,
],
'form_params' => $params,
]);

$result = json_decode($response->getBody(), true);

if (isset($result['result']) && $result['result'] === 1) {
Log::info("cPanel API call successful", ['endpoint' => $endpoint, 'params' => $params]);
return true;
} else {
Log::error("cPanel API call failed", ['endpoint' => $endpoint, 'params' => $params, 'response' => $result]);
return false;
}
} catch (GuzzleException $e) {
Log::error("cPanel API call error", ['endpoint' => $endpoint, 'params' => $params, 'error' => $e->getMessage()]);
return false;
}
}
}
135 changes: 103 additions & 32 deletions app/Services/HostingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Services\ControlPanels\CpanelClient;
use App\Services\ControlPanels\PleskClient;
use App\Services\ControlPanels\DirectAdminClient;
use Illuminate\Support\Facades\Log;
use Exception;

class HostingService
{
Expand All @@ -25,54 +27,123 @@ public function __construct(

public function provisionAccount(HostingAccount $account)
{
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->createAccount($account->username, $account->domain, $account->package);

if ($result) {
$account->status = 'active';
$account->save();
}
try {
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->createAccount($account->username, $account->domain, $account->package);

if ($result) {
$account->status = 'active';
$account->save();
Log::info("Account provisioned successfully", ['account_id' => $account->id]);
} else {
Log::error("Failed to provision account", ['account_id' => $account->id]);
}

return $result;
return $result;
} catch (Exception $e) {
Log::error("Error provisioning account", [
'account_id' => $account->id,
'error' => $e->getMessage()
]);
throw new Exception("Failed to provision account: " . $e->getMessage());
}
}

public function suspendAccount(HostingAccount $account)
{
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->suspendAccount($account->username);

if ($result) {
$account->status = 'suspended';
$account->save();
}
try {
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->suspendAccount($account->username);

if ($result) {
$account->status = 'suspended';
$account->save();
Log::info("Account suspended successfully", ['account_id' => $account->id]);
} else {
Log::error("Failed to suspend account", ['account_id' => $account->id]);
}

return $result;
return $result;
} catch (Exception $e) {
Log::error("Error suspending account", [
'account_id' => $account->id,
'error' => $e->getMessage()
]);
throw new Exception("Failed to suspend account: " . $e->getMessage());
}
}

public function unsuspendAccount(HostingAccount $account)
{
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->unsuspendAccount($account->username);

if ($result) {
$account->status = 'active';
$account->save();
}
try {
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->unsuspendAccount($account->username);

if ($result) {
$account->status = 'active';
$account->save();
Log::info("Account unsuspended successfully", ['account_id' => $account->id]);
} else {
Log::error("Failed to unsuspend account", ['account_id' => $account->id]);
}

return $result;
return $result;
} catch (Exception $e) {
Log::error("Error unsuspending account", [
'account_id' => $account->id,
'error' => $e->getMessage()
]);
throw new Exception("Failed to unsuspend account: " . $e->getMessage());
}
}

public function upgradeAccount(HostingAccount $account, $newPackage)
{
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->changePackage($account->username, $newPackage);

if ($result) {
$account->package = $newPackage;
$account->save();
try {
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->changePackage($account->username, $newPackage);

if ($result) {
$account->package = $newPackage;
$account->save();
Log::info("Account upgraded successfully", ['account_id' => $account->id, 'new_package' => $newPackage]);
} else {
Log::error("Failed to upgrade account", ['account_id' => $account->id, 'new_package' => $newPackage]);
}

return $result;
} catch (Exception $e) {
Log::error("Error upgrading account", [
'account_id' => $account->id,
'new_package' => $newPackage,
'error' => $e->getMessage()
]);
throw new Exception("Failed to upgrade account: " . $e->getMessage());
}
}

return $result;
public function terminateAccount(HostingAccount $account)
{
try {
$client = $this->getClientForControlPanel($account->control_panel);
$result = $client->terminateAccount($account->username);

if ($result) {
$account->status = 'terminated';
$account->save();
Log::info("Account terminated successfully", ['account_id' => $account->id]);
} else {
Log::error("Failed to terminate account", ['account_id' => $account->id]);
}

return $result;
} catch (Exception $e) {
Log::error("Error terminating account", [
'account_id' => $account->id,
'error' => $e->getMessage()
]);
throw new Exception("Failed to terminate account: " . $e->getMessage());
}
}

protected function getClientForControlPanel($controlPanel)
Expand All @@ -85,7 +156,7 @@ protected function getClientForControlPanel($controlPanel)
case 'directadmin':
return $this->directAdminClient;
default:
throw new \Exception("Unsupported control panel: $controlPanel");
throw new Exception("Unsupported control panel: $controlPanel");
}
}
}
Loading