UserService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Cloud;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon\App\Models\User;
  22. use ModulesGarden\ProxmoxAddon\App\Repositories\Cloud\ProductConfigurationRepository;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  24. use ModulesGarden\ProxmoxAddon\Core\Traits\Smarty;
  25. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  26. /**
  27. * Trait UserService
  28. * @method ProductConfigurationRepository configuration()
  29. */
  30. trait UserService
  31. {
  32. use Smarty;
  33. /**
  34. * @return User
  35. */
  36. protected function getUser()
  37. {
  38. $userId = $this->getWhmcsParamByKey('userid');
  39. $serviceId = $this->getWhmcsParamByKey('serviceid');
  40. return User::ofUserId($userId)->ofHostingId($serviceId)->orderBy("id", "desc")->firstOrFail();
  41. }
  42. protected function isUser()
  43. {
  44. $userId = $this->getWhmcsParamByKey('userid');
  45. $serviceId = $this->getWhmcsParamByKey('serviceid');
  46. return User::ofUserId($userId)->ofHostingId($serviceId)->count()== 1;
  47. }
  48. /**
  49. * @return User
  50. * @throws proxmox\v2\ProxmoxApiException
  51. */
  52. public function userCreate(User $user = null)
  53. {
  54. if (is_null($user))
  55. {
  56. $user = new User();
  57. $user->user_id = $this->getWhmcsParamByKey('userid');
  58. }
  59. $userPrefix = $this->configuration()->getUserPrefix();
  60. if ($userPrefix)
  61. {
  62. $userPrefix = $this->getSmarty()->fetchString($userPrefix, [
  63. "serviceid" => $this->getWhmcsParamByKey('serviceid'),
  64. "service_id" => $this->getWhmcsParamByKey('serviceid')
  65. ]);
  66. }
  67. $user->hosting_id = $this->getWhmcsParamByKey('serviceid');
  68. $username = $this->getWhmcsParamByKey('username');
  69. $user->username = $userPrefix . $username;
  70. $password = $this->getWhmcsParamByKey('password', Utility::generatePassword(12));
  71. $user->setPassword($password);
  72. $user->realm = $this->configuration()->getRealm();
  73. $userService = new proxmox\models\User();
  74. $userService->setApi($this->api());
  75. $data = [
  76. "userid" => "{$user->username}@{$user->realm}",
  77. "comment" => $this->configuration()->getUserComment(),
  78. "email" => $this->getWhmcsParamByKey('clientsdetails')['email'],
  79. "enable" => 1,
  80. "firstname" => $this->getWhmcsParamByKey('clientsdetails')['firstname'],
  81. "lastname" => $this->getWhmcsParamByKey('clientsdetails')['lastname'],
  82. "password" => $user->getPassword()
  83. ];
  84. $userService->create($data);
  85. $userService->changePassword($user->getPassword());
  86. $user->save();
  87. return $user;
  88. }
  89. protected function userUpdate()
  90. {
  91. $user = $this->getUser();
  92. $userService = new proxmox\models\User("{$user->username}@{$user->realm}");
  93. $userService->setApi($this->api());
  94. try
  95. {
  96. $userService->changePassword($user->getPassword());
  97. }
  98. catch (\Exception $ex)
  99. {
  100. if (!preg_match('/No such user/', $ex->getMessage()))
  101. {
  102. throw $ex;
  103. }
  104. $this->userCreate($user);
  105. }
  106. if(!sl('Vm')->hasVm()){
  107. return ;
  108. }
  109. $vmid = sl('Vm')->getVm()->getVmid();
  110. $this->userUpdatePermission([$vmid]);
  111. return $this;
  112. }
  113. public function userUpdatePermission($vmIds){
  114. $user = $this->getUser();
  115. $userService = new proxmox\models\User("{$user->username}@{$user->realm}");
  116. $userService->setApi($this->api());
  117. $permissions = $userService->permissions();
  118. foreach ($vmIds as $vmid){
  119. foreach ($permissions as $p)
  120. {
  121. if ($p['path'] == "/vms/" . $vmid && $p['ugid'] == $userService->getUserid())
  122. {
  123. $userService->updatePermission($vmid, $p['roleid'], 1);
  124. }
  125. }
  126. $role = $this->configuration()->getUserRole();
  127. $userService->updatePermission($vmid, $role);
  128. }
  129. return $this;
  130. }
  131. protected function deleteUser()
  132. {
  133. $user = $this->getUser();
  134. $userService = new proxmox\models\User("{$user->username}@{$user->realm}");
  135. $userService->setApi($this->api());
  136. $userService->delete();
  137. $user->delete();
  138. return $this;
  139. }
  140. }