Skip to content

Commit

Permalink
Merge pull request #38 from solusio/task-lbinde-adv-config
Browse files Browse the repository at this point in the history
Advanced configuration
  • Loading branch information
laurisb authored Dec 15, 2020
2 parents ea8adb3 + 9300804 commit 838e7f3
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/config.php
/node_modules
/releases
/vendor
7 changes: 7 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Copyright 2020. Plesk International GmbH.

return [
'delete_solus_user' => false,
];
47 changes: 47 additions & 0 deletions hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,55 @@

// Copyright 2020. Plesk International GmbH.

use WHMCS\Module\Server\SolusIoVps\Database\Models\User;
use WHMCS\Module\Server\SolusIoVps\Logger\Logger;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Connector;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\ServerResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\UserResource;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Config;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Product;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Servers;

add_hook('AfterProductUpgrade', 1, function (array $params) {
Product::upgrade($params['upgradeid']);
});

add_hook('PreDeleteClient', 1, function (array $params) {
try {
$config = Config::loadModuleConfig();

if (empty($config['delete_solus_user'])) {
return;
}

$whmcsUser = User::getById($params['userid']);

if ($whmcsUser === null) {
return;
}

$serverParams = Servers::getValidParams();

if (empty($serverParams)) {
throw new \Exception('No valid WHMCS server found');
}

$userResource = new UserResource(Connector::create($serverParams));
$solusUser = $userResource->getUserByEmail($whmcsUser->email);

if (empty($solusUser)) {
return;
}

$serverResource = new ServerResource(Connector::create($serverParams));
$servers = $serverResource->getAllByUser($solusUser['id']);

if (!empty($servers)) {
return;
}

$userResource->deleteUser($solusUser['id']);
} catch (\Exception $e) {
Logger::log([], $e->getMessage());
}
});
5 changes: 5 additions & 0 deletions lib/Database/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public static function getParams(int $serverId): array
'serverpassword' => decrypt($row->password),
];
}

public static function getServerIds(): array
{
return DB::table(self::TABLE)->pluck('id')->toArray();
}
}
24 changes: 24 additions & 0 deletions lib/Database/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

// Copyright 2020. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\Database\Models;

use WHMCS\Database\Capsule as DB;

/**
* @package WHMCS\Module\Server\SolusIoVps\Database\Models
*/
class User
{
const TABLE = 'tblusers';

/**
* @param int $id
* @return object|null
*/
public static function getById(int $id)
{
return DB::table(self::TABLE)->where(['id' => $id])->first();
}
}
2 changes: 1 addition & 1 deletion lib/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
final class Logger
{
const MODULE_NAME = 'solus';
const MODULE_NAME = 'solusiovps';

/**
* @param array $data
Expand Down
13 changes: 13 additions & 0 deletions lib/SolusAPI/Resources/ServerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ public function get(int $id): array
return $this->processResponse($this->connector->get("servers/{$id}"));
}

public function getAllByUser(int $userId): array
{
$response = $this->processResponse($this->connector->get("servers", [
'query' => [
'filter' => [
'user_id' => $userId,
],
],
]));

return $response['data'];
}

/**
* @param int $id
* @return array
Expand Down
9 changes: 9 additions & 0 deletions lib/SolusAPI/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ public function updateUser(int $userId, array $data): void
'json' => $data,
]));
}

/**
* @param int $userId
* @return void
*/
public function deleteUser(int $userId): void
{
$this->processResponse($this->connector->delete("users/{$userId}"));
}
}
15 changes: 15 additions & 0 deletions lib/WhmcsAPI/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ public static function getSystemUrl(): string

return $response['value'];
}

public static function loadModuleConfig(): array
{
$configFile = dirname(__DIR__, 2) . '/config.php';

if (is_file($configFile)) {
$config = require $configFile;

if (is_array($config)) {
return $config;
}
}

throw new \Exception('Failed to load module configuration');
}
}
34 changes: 34 additions & 0 deletions lib/WhmcsAPI/Servers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// Copyright 2020. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\WhmcsAPI;

use WHMCS\Module\Server\SolusIoVps\Database\Models\Server;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\ProjectResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Connector;

/**
* @package WHMCS\Module\Server\SolusIoVps\WhmcsAPI
*/
class Servers
{
public static function getValidParams(): array
{
$serverIds = Server::getServerIds();

foreach ($serverIds as $serverId) {
$serverParams = Server::getParams($serverId);

try {
$projectResource = new ProjectResource(Connector::create($serverParams));

$projectResource->list();

return $serverParams;
} catch (\Exception $e) {
return [];
}
}
}
}

0 comments on commit 838e7f3

Please sign in to comment.