ServerSettingProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  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\UI\ServerSettings\Providers;
  20. use MGProvision\Proxmox\v2\ProxmoxApiException;
  21. use MGProvision\Proxmox\v2\repository\StorageRepository;
  22. use ModulesGarden\ProxmoxAddon\App\Models\RangeVm;
  23. use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
  24. use ModulesGarden\ProxmoxAddon\App\Services\BaseService;
  25. use ModulesGarden\ProxmoxAddon\Core\DependencyInjection;
  26. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server;
  27. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  28. use ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  29. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  30. /**
  31. *
  32. * Description of RangeVmProvider
  33. *
  34. * @author Pawel Kopec <pawelk@modulesgardne.com>
  35. */
  36. class ServerSettingProvider extends BaseDataProvider implements AdminArea
  37. {
  38. use BaseService;
  39. protected $rangeVm;
  40. /**
  41. * @var ServerConfigurationRepository
  42. */
  43. protected $serverConfiguration;
  44. public function __construct()
  45. {
  46. $this->rangeVm = DependencyInjection::create(RangeVm::class);
  47. $this->serverConfiguration = new ServerConfigurationRepository($this->getRequestValue('id'));
  48. parent::__construct();
  49. }
  50. public function read()
  51. {
  52. $dbData = $this->rangeVm->where('server_id', $this->getRequestValue('id'))->first();
  53. if ($dbData !== null)
  54. {
  55. $this->data = $dbData->toArray();
  56. }
  57. $this->data['server_id'] = $this->getRequestValue('id');
  58. foreach ($this->serverConfiguration->all() as $key => $value){
  59. $this->data[$key] = $value;
  60. }
  61. $this->data['sshPassword'] = $this->serverConfiguration->getSshPassword();
  62. $this->data['sshKey'] = $this->serverConfiguration->getSshKey();
  63. //snippetStorage
  64. try{
  65. $this->availableValues['snippetStorage'] = [];
  66. $this->setServerId($this->getRequestValue('id'));
  67. $this->getApi()->setInstance();
  68. $repository = new StorageRepository();
  69. $repository->findSnippets();
  70. foreach ( $repository->fetch() as $storage){
  71. $this->availableValues['snippetStorage'][$storage->getStorage()] = $storage->getStorage();
  72. }
  73. }catch (ProxmoxApiException $ex){
  74. //nothing to do
  75. }
  76. }
  77. public function update()
  78. {
  79. //update & create range vnm
  80. if ($this->getFormDataValues()['vmid_from'] && $this->formData['vmid_to'])
  81. {
  82. $dbData = $this->rangeVm->where('server_id', $this->formData['server_id'])->first();
  83. if ($dbData === null)
  84. {
  85. $dbData = new RangeVm();
  86. }
  87. $dbData->fill($this->formData)->save();
  88. }
  89. else
  90. {//delete range vm
  91. $this->rangeVm->where('server_id', $this->formData['server_id'])->delete();
  92. }
  93. $data = [
  94. 'sshHost' => $this->formData['sshHost'],
  95. 'sshPort' => $this->formData['sshPort'],
  96. 'sshUser' => $this->formData['sshUser'],
  97. 'sshPassword' => encrypt($this->formData['sshPassword']),
  98. 'sshKey' => encrypt($this->formData['sshKey']),
  99. 'snippetStorage' => $this->formData['snippetStorage'],
  100. 'snippetDirectory' => null
  101. ];
  102. if($this->formData['snippetStorage']){
  103. $this->setServerId($this->formData['server_id']);
  104. $this->getApi()->setInstance();
  105. $repository = new StorageRepository();
  106. $repository->findSnippets();
  107. foreach ( $repository->fetch() as $storage){
  108. if($storage->getStorage() == $this->formData['snippetStorage']){
  109. $data['snippetDirectory'] = sprintf("%s/snippets/", $storage->getPath());
  110. break;
  111. }
  112. }
  113. }
  114. $this->serverConfiguration->fillAndSave($data);
  115. return (new HtmlDataJsonResponse())->setMessageAndTranslate('The server settings has been edited successfully')
  116. ->setStatusSuccess()
  117. ->setCallBackFunction($this->callBackFunction);
  118. }
  119. }