ChangePackage.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update;
  3. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  4. use ThurData\Servers\KerioEmail\App\Enums\Size;
  5. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProductManagerHandler;
  10. use ThurData\Servers\KerioEmail\Core\Helper\ConfigOptionsHelper;
  11. use ThurData\Servers\KerioEmail\Core\Helper\UpdateLimit;
  12. /**
  13. *
  14. * Created by PhpStorm.
  15. * User: Tomasz Bielecki ( tomasz.bi@thurdata.com )
  16. * Date: 07.11.19
  17. * Time: 13:03
  18. * Class ChangePackage
  19. */
  20. class ChangePackage extends ApiService
  21. {
  22. use ProductManagerHandler;
  23. use \ThurData\Servers\KerioEmail\App\Traits\HostingService;
  24. public function isValid()
  25. {
  26. if(!$this->formData['domain'])
  27. {
  28. $this->setError('Domain name can not be found.');
  29. return false;
  30. }
  31. return parent::isValid(); // TODO: Change the autogenerated stub
  32. }
  33. public function process()
  34. {
  35. $serviceID = $this->formData['serviceid'];
  36. $configOption = new ConfigOptionsHelper;
  37. $accounts = $this->api->repository()->accounts->getByDomainName($this->formData['domain']);
  38. $acc_base = $this->productManager->get('acc_base') ? $this->productManager->get('acc_base') : 0;
  39. if(isset($this->formData['configoptions']['acc_add']) && isset($this->formData['configoptions']['acc_new'])) {
  40. $acc_add = $this->formData['configoptions']['acc_add'];
  41. $acc_new = $this->formData['configoptions']['acc_new'];
  42. $acc_add = $acc_add + $acc_new;
  43. $acc_new = 0;
  44. $accountLimit = $acc_base + $acc_add;
  45. if (($this->productManager->get('acc_limit') > 0) && ($accountLimit > $this->productManager->get('acc_limit'))) {
  46. $acc_add = $this->productManager->get('acc_limit') - $acc_base;
  47. }
  48. $acc_addObj = $configOption->getConfigurableOption($serviceID, 'acc_add');
  49. if(isset($acc_addObj->id)) {
  50. $configOption->updateConfigurableOption($acc_addObj->id, ['qty' => $acc_add]);
  51. }
  52. $acc_newObj = $configOption->getConfigurableOption($serviceID, 'acc_new');
  53. if(isset($acc_newObj->id)) {
  54. $configOption->updateConfigurableOption($acc_newObj->id, ['qty' => $acc_new]);
  55. }
  56. } else {
  57. $accountLimit = $this->productManager->get('acc_limit');
  58. }
  59. foreach($accounts as $account)
  60. {
  61. /* @var $account Account*/
  62. foreach($this->productManager->getKerioConfiguration() as $key => $value)
  63. {
  64. $value = $value === ProductParams::SWITCHER_ENABLED ? Kerio::ATTR_ENABLED : Kerio::ATTR_DISABLED;
  65. $account->setAttr($key, $value);
  66. $result = $this->api->account->update($account);
  67. if(!$result)
  68. {
  69. //todo some logs
  70. }
  71. }
  72. }
  73. $domain = $this->api->repository()->domains->getByName($this->formData['domain']);
  74. if(!$domain) {
  75. throw new \Exception($this->api->repository()->domains->getError());
  76. return false;
  77. }
  78. $domainBaseSize = $this->productManager->get('domainBaseSize') ? $this->productManager->get('domainBaseSize') : 0;
  79. if(isset($this->formData['configoptions']['domainAddSize']) && isset($this->formData['configoptions']['domainNewSize'])) {
  80. $domainAddSize = $this->formData['configoptions']['domainAddSize'];
  81. $domainNewSize = $this->formData['configoptions']['domainNewSize'];
  82. $domainAddSize = $domainAddSize + $domainNewSize;
  83. $domainNewSize = 0;
  84. $domainSize = $domainBaseSize + $domainAddSize;
  85. if (($this->productManager->get('domainMaxSize') > 0) && ($domainSize > $this->productManager->get('domainMaxSize'))) {
  86. $domainMaxSize = $this->productManager->get('domainMaxSize');
  87. $domainAddSize = $domainMaxSize - $domainBaseSize;
  88. $domainSize = $domainBaseSize + $domainAddSize;
  89. }
  90. $domainAddSizeObj = $configOption->getConfigurableOption($serviceID, 'domainAddSize');
  91. if(isset($domainAddSizeObj->id)) {
  92. $configOption->updateConfigurableOption($domainAddSizeObj->id, ['qty' => $domainAddSize]);
  93. }
  94. $domainNewSizeObj = $configOption->getConfigurableOption($serviceID, 'domainNewSize');
  95. if(isset($domainNewSizeObj->id)) {
  96. $configOption->updateConfigurableOption($domainNewSizeObj->id, ['qty' => $domainNewSize]);
  97. }
  98. } else {
  99. $domainSize = $this->productManager->get('domainBaseSize');
  100. }
  101. $updateLimit = new UpdateLimit;
  102. $updateLimit->updateDiskLimit($serviceID, $domainSize * 1024);
  103. $updateLimit->updateBWLimit($serviceID, $accountLimit);
  104. $domain->setAttrs([
  105. Domain::ATTR_MAIL_DOMAIN_QUOTA => $domainSize * Size::B_TO_GB,
  106. ]);
  107. $response = $this->api->domain->update($domain);
  108. if(!$response)
  109. {
  110. throw new \Exception($this->api->domain->getError());
  111. return false;
  112. }
  113. return true;
  114. }
  115. }