IpAddressProvider.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Servers\ProxmoxVps\App\UI\IpAddress\Providers;
  20. use MGProvision\Proxmox\v2\Api;
  21. use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
  22. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  23. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  24. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
  26. use ModulesGarden\ProxmoxAddon\App\Services\Vps\IpSetIpFilterService;
  27. use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  29. use ModulesGarden\ProxmoxAddon\App\Services\Vps\VmIpAddressConveter;
  30. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  31. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  32. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  33. use Illuminate\Database\Capsule\Manager as DB;
  34. class IpAddressProvider extends BaseDataProvider implements ClientArea
  35. {
  36. use ApiService;
  37. use ProductService;
  38. use HostingService;
  39. private $networkService;
  40. /**
  41. * @var IpSetIpFilterService
  42. */
  43. private $ipSetIpFilterService;
  44. /**
  45. * IpAddressProvider constructor.
  46. */
  47. public function __construct()
  48. {
  49. parent::__construct();
  50. $this->networkService = new NetworkService();
  51. $this->ipSetIpFilterService = new IpSetIpFilterService();
  52. }
  53. public function read()
  54. {
  55. if ($this->actionElementId && $this->actionElementId != "diskDataTable")
  56. {
  57. $this->data['id'] = $this->actionElementId;
  58. $this->data['ip'] = VmIpAddress::where("id", $this->actionElementId)->value("ip");
  59. }
  60. }
  61. public function create()
  62. {
  63. try{
  64. DB::beginTransaction();
  65. Api::beginTransaction();
  66. $ip = new VmIpAddress();
  67. $ip->fill($this->formData);
  68. $ip->hosting_id = $this->getWhmcsParamByKey("serviceid");
  69. $ip->server_id = $this->getWhmcsParamByKey("serverid");
  70. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  71. if($this->getFormDataValues()['network']=='on'){
  72. $ipConveter = new VmIpAddressConveter([$ip], $this->vm());
  73. $config = $ipConveter->asConfig();
  74. $this->vm()->updateConfig($config);
  75. }
  76. //insert to data base
  77. $ip->save();
  78. $this->hosting()->ipAdd($ip->ip);
  79. //lock ip in proxmox addon
  80. if (!Utility::isIpManagerProxmoxVPSIntegration())
  81. {
  82. IpAddress::where('ip', $ip->ip)
  83. ->update(["hosting_id" => $ip->hosting_id]);
  84. }
  85. DB::commit();
  86. }catch (\Exception $ex){
  87. Api::commit();
  88. DB::rollBack();
  89. throw $ex;
  90. }
  91. return (new HtmlDataJsonResponse())
  92. ->setStatusSuccess()
  93. ->setMessageAndTranslate('The IP Address has been added successfully');
  94. }
  95. public function update()
  96. {
  97. }
  98. public function delete()
  99. {
  100. $this->networkService->deleteByIpAddresses([$this->formData['id']]);
  101. if ($this->configuration()->isIpsetIpFilter())
  102. {
  103. $this->ipSetIpFilterService->create();
  104. }
  105. return (new HtmlDataJsonResponse())
  106. ->setStatusSuccess()
  107. ->setMessageAndTranslate('The IP Address has been deleted successfully');
  108. }
  109. }