Skip to content

Commit

Permalink
BUGFIX SIO-2951 create user with role and limit group
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Vozhov committed Mar 4, 2021
1 parent 3b5ea51 commit 8eec3a3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lang/english.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
$_LANG['solusiovps_config_option_backup_enabled'] = 'Enable Backups';
$_LANG['solusiovps_config_option_user_data'] = 'User Data';
$_LANG['solusiovps_config_option_none'] = '(None)';
$_LANG['solusiovps_config_option_default_role'] = 'Default Role';
$_LANG['solusiovps_config_option_default_limit_group'] = 'Default Limit Group';

$_LANG['solusiovps_button_restart'] = 'Restart';
$_LANG['solusiovps_button_vnc'] = 'VNC Access';
Expand Down
17 changes: 17 additions & 0 deletions lib/SolusAPI/Resources/LimitGroupResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// Copyright 2020. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources;

use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\Traits\ListRequest;

/**
* @package WHMCS\Module\Server\SolusIoVps\SolusAPI
*/
class LimitGroupResource extends ApiResource
{
use ListRequest;

const ENTITY = 'limit_groups';
}
17 changes: 17 additions & 0 deletions lib/SolusAPI/Resources/RoleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// Copyright 2020. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources;

use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\Traits\ListRequest;

/**
* @package WHMCS\Module\Server\SolusIoVps\SolusAPI
*/
class RoleResource extends ApiResource
{
use ListRequest;

const ENTITY = 'roles';
}
100 changes: 92 additions & 8 deletions lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Helpers\DataWrapper;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Helpers\Strings;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\ApplicationResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\LimitGroupResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\LocationResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\OsImageResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\PlanResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\ProjectResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\RoleResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\ServerResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\SshKeyResource;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\UserResource;
Expand Down Expand Up @@ -64,46 +66,60 @@ function solusiovps_ConfigOptions(): array
global $_LANG;

return [
'plan' => [
'plan' => [ // configoption1
'FriendlyName' => $_LANG['solusiovps_config_option_plan'],
'Type' => 'text',
'Size' => '25',
'Loader' => 'solusiovps_PlanLoader',
'SimpleMode' => true,
],
'location' => [
'location' => [ // configoption2
'FriendlyName' => $_LANG['solusiovps_config_option_default_location'],
'Type' => 'text',
'Size' => '25',
'Loader' => 'solusiovps_LocationLoader',
'SimpleMode' => true,
],
'os_image' => [
'os_image' => [ // configoption3
'FriendlyName' => $_LANG['solusiovps_config_option_default_operating_system'],
'Type' => 'text',
'Size' => '25',
'Loader' => 'solusiovps_OsImageLoader',
'SimpleMode' => true,
],
'application' => [
'application' => [ // configoption4
'FriendlyName' => $_LANG['solusiovps_config_option_application'],
'Type' => 'text',
'Size' => '25',
'Loader' => 'solus_ApplicationLoader',
'SimpleMode' => true,
],
'user_data' => [
'user_data' => [ // configoption5
'FriendlyName' => $_LANG['solusiovps_config_option_user_data'],
'Type' => 'textarea',
'Rows' => 5,
'Cols' => 25,
'SimpleMode' => true,
],
'backup_enabled' => [
'backup_enabled' => [ // configoption6
'FriendlyName' => $_LANG['solusiovps_config_option_backup_enabled'],
'Type' => 'yesno',
'SimpleMode' => true,
],
'role' => [ // configoption7
'FriendlyName' => $_LANG['solusiovps_config_option_default_role'],
'Type' => 'text',
'Size' => '25',
'Loader' => 'solus_RoleLoader',
'SimpleMode' => true,
],
'limit_group' => [ // configoption8
'FriendlyName' => $_LANG['solusiovps_config_option_default_limit_group'],
'Type' => 'text',
'Size' => '25',
'Loader' => 'solus_LimitGroupLoader',
'SimpleMode' => true,
],
];
}

Expand Down Expand Up @@ -211,6 +227,62 @@ function solus_ApplicationLoader(array $params): array
}
}

/**
* @param array $params
* @return array
* @throws Exception
*/
function solus_RoleLoader(array $params): array
{
global $_LANG;

try {
$roleResource = new RoleResource(Connector::create($params));

$result = [
0 => $_LANG['solusiovps_config_option_none'],
];

foreach (DataWrapper::wrap($roleResource->list()) as $item) {
$result[Arr::get($item, 'id')] = Arr::get($item, 'name');
}

return $result;
} catch (Exception $e) {
Logger::log([], $e->getMessage());

throw $e;
}
}

/**
* @param array $params
* @return array
* @throws Exception
*/
function solus_LimitGroupLoader(array $params): array
{
global $_LANG;

try {
$limitGroupResource = new LimitGroupResource(Connector::create($params));

$result = [
0 => $_LANG['solusiovps_config_option_none'],
];

foreach (DataWrapper::wrap($limitGroupResource->list()) as $item) {
$result[Arr::get($item, 'id')] = Arr::get($item, 'name');
}

return $result;
} catch (Exception $e) {
Logger::log([], $e->getMessage());

throw $e;
}
}

/**
* @param array $params
* @return string
Expand All @@ -235,12 +307,24 @@ function solusiovps_CreateAccount(array $params): string
$solusUser = $userResource->getUserByEmail($params['clientsdetails']['email']);

if (empty($solusUser)) {
$solusUserId = $userResource->create([
$solusUserData = [
'password' => $params['password'],
'email' => $params['clientsdetails']['email'],
'billing_user_id' => (string) $whmcsUserId,
'status' => 'active',
]);
];

$role = (int) Arr::get($params, 'configoption7');
if ($role > 0) {
$solusUserData['roles'] = [$role];
}

$limitGroup = (int) Arr::get($params, 'configoption8');
if ($limitGroup > 0) {
$solusUserData['limit_group_id'] = $limitGroup;
}

$solusUserId = $userResource->create($solusUserData);
} else {
$solusUserId = $solusUser['id'];

Expand Down

0 comments on commit 8eec3a3

Please sign in to comment.