Skip to content

Commit

Permalink
TASK SIO-2943 sync client email change
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Korsakov committed Mar 22, 2021
1 parent f952576 commit 624bb2d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
78 changes: 68 additions & 10 deletions hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

// 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\ClientArea;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Config;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Product;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\Servers;
use WHMCS\User\Client;

add_hook('AfterProductUpgrade', 1, function (array $params) {
Product::upgrade($params['upgradeid']);
Expand All @@ -23,20 +24,15 @@
return;
}

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

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

$whmcsUser = Client::findOrFail($params['userid']);
$serverParams = Servers::getValidParams();

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

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

if (empty($solusUser)) {
return;
Expand All @@ -50,7 +46,69 @@
}

$userResource->deleteUser($solusUser['id']);
} catch (\Exception $e) {
} catch (Exception $e) {
Logger::log([], $e->getMessage());
}
});

add_hook('ClientDetailsValidation', 1, function(array $params) {
try {
$userId = null;

// If updating user via admin area
if (isset($params['userid'])) {
$userId = $params['userid'];
}

// If updating user via client area
if (array_key_exists('save', $params)) {
$userId = (new ClientArea())->getUserID();
}

if ($userId) {
$whmcsUser = Client::findOrFail($userId);

if ($whmcsUser->email === $params['email']) {
return [];
}
}

$serverParams = Servers::getValidParams();

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

$userResource = new UserResource(Connector::create($serverParams));
$solusUser = $userResource->getUserByEmail($params['email']);

if (!empty($solusUser)) {
return 'email is already taken';
}
} catch (Exception $e) {
Logger::log([], $e->getMessage());
}

return [];
});

add_hook('ClientEdit', 1, function(array $params) {
try {
$serverParams = Servers::getValidParams();

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

$userResource = new UserResource(Connector::create($serverParams));
$solusUser = $userResource->getUserByEmail($params['olddata']['email']);

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

$userResource->patchUser($solusUser['id'], ['email' => $params['email']]);
} catch (Exception $e) {
Logger::log([], $e->getMessage());
}
});
4 changes: 3 additions & 1 deletion lib/Database/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static function getParams(int $serverId): array

public static function getServerIds(): array
{
return DB::table(self::TABLE)->pluck('id')->toArray();
$result = DB::table(self::TABLE)->pluck('id');

return collect($result)->all();
}
}
12 changes: 12 additions & 0 deletions lib/SolusAPI/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public function updateUser(int $userId, array $data): void
]));
}

/**
* @param int $userId
* @param array $data
* @return void
*/
public function patchUser(int $userId, array $data): void
{
$this->processResponse($this->connector->patch("users/{$userId}", [
'json' => $data,
]));
}

/**
* @param int $userId
* @return void
Expand Down
5 changes: 4 additions & 1 deletion lib/WhmcsAPI/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WHMCS\Module\Server\SolusIoVps\WhmcsAPI;

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

Expand All @@ -27,8 +28,10 @@ public static function getValidParams(): array

return $serverParams;
} catch (\Exception $e) {
return [];
Logger::log(['error' => $e->getMessage()], $e->getMessage());
}
}

return [];
}
}

0 comments on commit 624bb2d

Please sign in to comment.