UserService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  20. use Illuminate\Database\Capsule\Manager as DB;
  21. use MGProvision\Proxmox\v2 as proxmox;
  22. use ModulesGarden\ProxmoxAddon\App\Models\User;
  23. use ModulesGarden\ProxmoxAddon\App\Repositories\Vps\ProductConfigurationRepository;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  25. use ModulesGarden\ProxmoxAddon\Core\Traits\Smarty;
  26. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\CustomField;
  27. /**
  28. * Trait UserService
  29. * @package ModulesGarden\Servers\ProxmoxVps\App\Services
  30. * @method ProductConfigurationRepository configuration()
  31. */
  32. trait UserService
  33. {
  34. use Smarty;
  35. /**
  36. * @return User
  37. */
  38. protected function getUser()
  39. {
  40. $userId = $this->getWhmcsParamByKey('userid');
  41. $serviceId = $this->getWhmcsParamByKey('serviceid');
  42. if (User::ofUserId($userId)->ofHostingId($serviceId)->count())
  43. {
  44. return User::ofUserId($userId)->ofHostingId($serviceId)->orderBy("id", "desc")->firstOrFail();
  45. } else if(User::ofUserId($userId)->ofHostingId('0')->count()){
  46. return User::ofUserId($userId)->ofHostingId('0')->orderBy("id", "desc")->firstOrFail();
  47. }
  48. $hostingId = $this->configuration()->isOneUserPerVps() ? $serviceId : '0';
  49. return User::ofUserId($userId)->ofHostingId($hostingId)->orderBy("id", "desc")->firstOrFail();
  50. }
  51. protected function isUser()
  52. {
  53. $userId = $this->getWhmcsParamByKey('userid');
  54. $serviceId = $this->getWhmcsParamByKey('serviceid');
  55. if (User::ofUserId($userId)->ofHostingId($serviceId)->count())
  56. {
  57. return true;
  58. }
  59. else if(User::ofUserId($userId)->ofHostingId('0')->count()){
  60. return true;
  61. }
  62. $hostingId = $this->configuration()->isOneUserPerVps() ? $serviceId : '0';
  63. return User::ofUserId($userId)->ofHostingId($hostingId)->count() == 1;
  64. }
  65. /**
  66. * @return User
  67. * @throws proxmox\v2\ProxmoxApiException
  68. */
  69. public function userCreate(User $user = null)
  70. {
  71. if (is_null($user))
  72. {
  73. $user = new User();
  74. $user->user_id = $this->getWhmcsParamByKey('userid');
  75. }
  76. $userPrefix = $this->configuration()->getUserPrefix();
  77. if ($userPrefix)
  78. {
  79. $userPrefix = $this->getSmarty()->fetchString($userPrefix, [
  80. "serviceid" => $this->getWhmcsParamByKey('serviceid'),
  81. "service_id" => $this->getWhmcsParamByKey('serviceid')
  82. ]);
  83. }
  84. if ($this->configuration()->isOneUserPerVps())
  85. {
  86. $user->hosting_id = $this->getWhmcsParamByKey('serviceid');
  87. }
  88. else
  89. {
  90. $user->hosting_id = '0';
  91. }
  92. $username = $this->getWhmcsCustomField(CustomField::USERNAME) ? $this->getWhmcsCustomField(CustomField::USERNAME) : $this->getWhmcsParamByKey('username');
  93. $user->username = $userPrefix . $username;
  94. $user->setPassword(Utility::generatePassword(12));
  95. $user->realm = $this->configuration()->getRealm();
  96. $userService = new proxmox\models\User();
  97. $userService->setApi($this->api());
  98. $data = [
  99. "userid" => "{$user->username}@{$user->realm}",
  100. "comment" => $this->configuration()->getUserComment(),
  101. "email" => $this->getWhmcsParamByKey('clientsdetails')['email'],
  102. "enable" => 1,
  103. "firstname" => $this->getWhmcsParamByKey('clientsdetails')['firstname'],
  104. "lastname" => $this->getWhmcsParamByKey('clientsdetails')['lastname'],
  105. "password" => $user->getPassword()
  106. ];
  107. $userService->create($data);
  108. $userService->changePassword($user->getPassword());
  109. $user->save();
  110. return $user;
  111. }
  112. protected function userUpdate()
  113. {
  114. $user = $this->getUser();
  115. $userService = new proxmox\models\User("{$user->username}@{$user->realm}");
  116. $userService->setApi($this->api());
  117. try
  118. {
  119. $userService->changePassword($user->getPassword());
  120. }
  121. catch (\Exception $ex)
  122. {
  123. if (!preg_match('/No such user/', $ex->getMessage()))
  124. {
  125. throw $ex;
  126. }
  127. $this->userCreate($user);
  128. }
  129. $vmid = $this->getWhmcsCustomField(CustomField::VMID);
  130. if (!$vmid)
  131. {
  132. return;
  133. }
  134. $permissions = $userService->permissions();
  135. foreach ($permissions as $p)
  136. {
  137. if ($p['path'] == "/vms/" . $vmid && $p['ugid'] == $userService->getUserid())
  138. {
  139. $userService->updatePermission($vmid, $p['roleid'], 1);
  140. }
  141. }
  142. $role = $this->configuration()->getUserRole();
  143. $userService->updatePermission($vmid, $role);
  144. return $this;
  145. }
  146. protected function deleteUser()
  147. {
  148. $user = $this->getUser();
  149. $userService = new proxmox\models\User("{$user->username}@{$user->realm}");
  150. $userService->setApi($this->api());
  151. if (!$this->configuration()->isOneUserPerVps())
  152. {
  153. $h = 'tblhosting';
  154. $p = 'tblproducts';
  155. $query = DB::table($h)
  156. ->rightJoin($p, "{$p}.id", "=", "{$h}.packageid")
  157. ->where("{$h}.userid", $this->getWhmcsParamByKey('userid'))
  158. ->where("{$h}.id", "!=", $this->getWhmcsParamByKey('serviceid'))
  159. ->whereIn("{$h}.domainstatus", ['Active', 'Suspended'])
  160. ->where("{$p}.servertype", 'proxmoxVPS');
  161. if ($query->count() == "0")
  162. {
  163. $userService->delete();
  164. $user->delete();
  165. return $this;
  166. }
  167. $vmid = $this->getWhmcsParamByKey('customfields')['vmid'];
  168. if (!$vmid)
  169. {
  170. return $this;
  171. }
  172. //Delete permissions
  173. $permissions = $userService->permissions();
  174. foreach ($permissions as $p)
  175. {
  176. if ($p['path'] == "/vms/" . $vmid && $p['ugid'] == $userService->getUserid())
  177. {
  178. $userService->updatePermission($vmid, $p['roleid'], 1);
  179. }
  180. }
  181. }
  182. else
  183. {
  184. $userService->delete();
  185. $user->delete();
  186. return $this;
  187. }
  188. }
  189. public function createUserIfNotExist(User $user){
  190. $userService = new proxmox\models\User();
  191. $userService->setApi($this->api());
  192. $userService->setUserid("{$user->username}@{$user->realm}");
  193. if(!$userService->exist()){
  194. $data = [
  195. "userid" => "{$user->username}@{$user->realm}",
  196. "comment" => $this->configuration()->getUserComment(),
  197. "email" => $this->getWhmcsParamByKey('clientsdetails')['email'],
  198. "enable" => 1,
  199. "firstname" => $this->getWhmcsParamByKey('clientsdetails')['firstname'],
  200. "lastname" => $this->getWhmcsParamByKey('clientsdetails')['lastname'],
  201. "password" => $user->getPassword()
  202. ];
  203. $userService->create($data);
  204. }
  205. return $this;
  206. }
  207. }