SuspendAccount.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**********************************************************************
  3. * ProxmoxVPS developed. (26.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\Servers\ProxmoxVps\App\Http\Actions;
  20. use MGProvision\Proxmox\v2\models\Kvm;
  21. use MGProvision\Proxmox\v2\models\User;
  22. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
  25. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\AppParams;
  26. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
  27. use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\AddonController;
  28. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;
  29. class SuspendAccount extends AddonController
  30. {
  31. use ProductService;
  32. use ApiService;
  33. use UserService;
  34. use WhmcsParams;
  35. public function execute($params = null)
  36. {
  37. if(!ProxmoxAddonValidator::isInstalled()){
  38. return ProxmoxAddonValidator::failAsString();
  39. }
  40. (new AppParams())->initFromWhmcsParams();
  41. try
  42. {
  43. //User suspend
  44. $user = $this->getUser();
  45. $userService = new User("{$user->username}@{$user->realm}");
  46. $userService->setApi($this->api());
  47. $permissions = $userService->permissions();
  48. $vmid = $params['customfields']['vmid'];
  49. foreach ($permissions as $p)
  50. {
  51. if ($p['path'] == "/vms/" . $vmid && $p['ugid'] == $userService->getUserid())
  52. {
  53. $userService->updatePermission($vmid, $p['roleid'], 1);
  54. }
  55. }
  56. if ($user->hosting_id > 0)
  57. {
  58. $userService->disable();
  59. }
  60. //VM suspend
  61. $this->vm()->updateConfig(["onboot" => 0]);
  62. if (!$this->vm()->isRunning())
  63. {
  64. return "success";
  65. }
  66. $suspensionAction = $this->configuration()->getSuspensionAction();
  67. if ($suspensionAction && method_exists($this->vm(), $suspensionAction))
  68. {
  69. $this->vm()->{$suspensionAction}();
  70. return "success";
  71. }
  72. if ($this->vm() instanceof Kvm)
  73. {
  74. $this->vm()->suspend();
  75. return "success";
  76. }
  77. if ($this->vm()->isRunning())
  78. {
  79. $this->vm()->stop();
  80. }
  81. return "success";
  82. }
  83. catch (\Exception $ex)
  84. {
  85. return $ex->getMessage();
  86. }
  87. }
  88. }