diff --git a/web/app/Http/Controllers/Api/HostingSubscriptionsController.php b/web/app/Http/Controllers/Api/HostingSubscriptionsController.php index 57316d4..6b197f1 100644 --- a/web/app/Http/Controllers/Api/HostingSubscriptionsController.php +++ b/web/app/Http/Controllers/Api/HostingSubscriptionsController.php @@ -36,14 +36,28 @@ class HostingSubscriptionsController extends ApiController ], 400); } + if (isset($request->system_username)) { + $findHostingSubscription = HostingSubscription::where('system_username', $request->system_username)->first(); + if ($findHostingSubscription) { + return response()->json([ + 'status' => 'error', + 'message' => 'System username already exists', + ], 400); + } + } + $hostingSubscription = new HostingSubscription(); $hostingSubscription->customer_id = $request->customer_id; $hostingSubscription->hosting_plan_id = $request->hosting_plan_id; $hostingSubscription->domain = $request->domain; - // $hostingSubscription->username = $request->username; - // $hostingSubscription->password = $request->password; - // $hostingSubscription->description = $request->description; + if (isset($request->system_username)) { + $hostingSubscription->system_username = $request->system_username; + } + + if (isset($request->system_password)) { + $hostingSubscription->system_password = $request->system_password; + } $hostingSubscription->setup_date = Carbon::now(); $hostingSubscription->save(); diff --git a/web/app/Models/HostingSubscription.php b/web/app/Models/HostingSubscription.php index 3f68995..43cef72 100644 --- a/web/app/Models/HostingSubscription.php +++ b/web/app/Models/HostingSubscription.php @@ -46,6 +46,9 @@ class HostingSubscription extends Model throw new \Exception('Domain already exists'); } $create = $model->_createLinuxWebUser($model); + if (isset($create['error'])) { + throw new \Exception($create['message']); + } if (isset($create['system_username']) && isset($create['system_password'])) { $model->system_username = $create['system_username']; $model->system_password = $create['system_password']; @@ -125,21 +128,38 @@ class HostingSubscription extends Model return []; } - $systemUsername = $this->_generateUsername($model->domain . $findCustomer->id); - if ($this->_startsWithNumber($systemUsername)) { - $systemUsername = $this->_generateUsername(Str::random(4)); + if (!empty($model->system_username)) { + $getLinuxUser = new GetLinuxUser(); + $getLinuxUser->setUsername($model->system_username); + $linuxUser = $getLinuxUser->handle(); + if (!empty($linuxUser)) { + return [ + 'error' => true, + 'message' => 'System username already exists.' + ]; + } } - $getLinuxUser = new GetLinuxUser(); - $getLinuxUser->setUsername($systemUsername); - $linuxUser = $getLinuxUser->handle(); + if (empty($model->system_username)) { + $systemUsername = $this->_generateUsername($model->domain . $findCustomer->id); + if ($this->_startsWithNumber($systemUsername)) { + $systemUsername = $this->_generateUsername(Str::random(4)); + } - if (! empty($linuxUser)) { - $systemUsername = $this->_generateUsername($systemUsername.$findCustomer->id.Str::random(4)); + $getLinuxUser = new GetLinuxUser(); + $getLinuxUser->setUsername($systemUsername); + $linuxUser = $getLinuxUser->handle(); + + if (!empty($linuxUser)) { + $systemUsername = $this->_generateUsername($systemUsername . $findCustomer->id . Str::random(4)); + } + + $systemPassword = Str::random(14); + } else { + $systemUsername = $model->system_username; + $systemPassword = $model->system_password; } - $systemPassword = Str::random(14); - $createLinuxWebUser = new CreateLinuxWebUser(); $createLinuxWebUser->setUsername($systemUsername); $createLinuxWebUser->setPassword($systemPassword);