UnsuspendAccount.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\ProxmoxCloudVps\App\Http\Actions;
  20. use MGProvision\Proxmox\v2\models\Kvm;
  21. use MGProvision\Proxmox\v2\models\User;
  22. use MGProvision\Proxmox\v2\VmFactory;
  23. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
  26. use ModulesGarden\ProxmoxAddon\App\Services\CloudService;
  27. use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\AppParams;
  28. use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\ProxmoxAddonValidator;
  29. use ModulesGarden\Servers\ProxmoxCloudVps\Core\App\Controllers\Instances\AddonController;
  30. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;
  31. class UnsuspendAccount extends AddonController
  32. {
  33. use ApiService;
  34. use UserService;
  35. use ProductService;
  36. use WhmcsParams;
  37. public function execute($params = null)
  38. {
  39. if(!ProxmoxAddonValidator::isInstalled()){
  40. return ProxmoxAddonValidator::failAsString();
  41. }
  42. try
  43. {
  44. (new AppParams())->initFromParams($params);
  45. $cloud = new CloudService();
  46. //User Unsuspend
  47. $user = $this->getUser();
  48. $userService = new User("{$user->username}@{$user->realm}");
  49. $userService->setApi($this->api());
  50. if ($userService->configuration()['enable'] == "0")
  51. {
  52. $userService->enable();
  53. }
  54. //VM Permission
  55. $permissions = $userService->permissions();
  56. foreach ($cloud->getVmModels() as $vmModel){
  57. $vmid = $vmModel->vmid;
  58. $vm = (new VmFactory())->fromVmModel($vmModel);
  59. foreach ($permissions as $p)
  60. {
  61. if ($p['path'] == "/vms/" . $vmid && $p['ugid'] == $userService->getUserid())
  62. {
  63. $userService->updatePermission($vmid, $p['roleid'], 1);
  64. }
  65. }
  66. $role = $this->configuration()->getUserRole() ? $this->configuration()->getUserRole() : 'PVEVMUser';
  67. $userService->updatePermission($vmid, $role);
  68. if ($this->configuration()->isOnboot())
  69. {
  70. $vm->updateConfig(["onboot" => 1]);
  71. }
  72. $status = $vm->status();
  73. if ($vm instanceof Kvm && $status['qmpstatus'] == "paused")
  74. {
  75. $vm->resume();
  76. continue;
  77. }
  78. if ($status['status'] == "running")
  79. {
  80. continue;
  81. }
  82. else
  83. {
  84. $vm->start();
  85. }
  86. }
  87. return "success";
  88. }
  89. catch (\Exception $ex)
  90. {
  91. return $ex->getMessage();
  92. }
  93. }
  94. }