CreateAccount.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Class CreateAccount
  4. * User: ThurData
  5. * Date: 2019-10-07
  6. * Time: 10:41
  7. * @package ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create
  8. */
  9. namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create;
  10. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  11. use ThurData\Servers\KerioEmail\App\Enums\Size;
  12. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  13. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  14. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
  15. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
  16. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProductManagerHandler;
  17. use ThurData\Servers\KerioEmail\Core\Helper\ConfigOptionsHelper;
  18. class CreateAccount extends ApiService
  19. {
  20. use ProductManagerHandler;
  21. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  22. /**
  23. * added more conditions
  24. * @return bool
  25. */
  26. protected function isValid()
  27. {
  28. /**
  29. * check if product manager is set
  30. */
  31. if(!$this->productManager)
  32. {
  33. $this->setError('Product Manager Not Found');
  34. return false;
  35. }
  36. /**
  37. * domain name
  38. */
  39. if(!$this->formData['domain'])
  40. {
  41. $this->setError('Domain name can not be found.');
  42. return false;
  43. }
  44. /**
  45. * check full usages of domains
  46. */
  47. $maxDomainQuota = $this->productManager->get('domainMaxSize') * Size::B_TO_GB;
  48. $usages = $this->api->repository()->accounts->getFullUsages($this->formData['domain']);
  49. if($maxDomainQuota !== Size::UNLIMITED && $usages >= $maxDomainQuota)
  50. {
  51. $this->setError("Domain size limit has been reached");
  52. return false;
  53. }
  54. /**
  55. *
  56. * Check if accounts limit has been reached
  57. */
  58. $hosting = $this->productManager->getHosting();
  59. $configOption = new ConfigOptionsHelper;
  60. $acc_addObj = $configOption->getConfigurableOption($hosting->id, 'acc_add');
  61. $acc_add = $acc_addObj->qty ? $acc_addObj->qty : 0;
  62. $accountLimit = $this->productManager->get('acc_base') + $acc_add;
  63. $mailBoxes = $this->getMailboxes();
  64. if(is_countable($mailBoxes)){
  65. if(count($mailBoxes) >= $accountLimit && $accountLimit !== ProductParams::SIZE_UNLIMITED && 'editAccountForm' !== $this->getRequestValue('loadData'))
  66. {
  67. $this->setError('There are too many mailboxes');
  68. return false;
  69. }
  70. }
  71. /**
  72. *
  73. */
  74. return parent::isValid();
  75. }
  76. /**
  77. * @return mixed
  78. */
  79. protected function getMailboxes()
  80. {
  81. $mailBoxes = $this->api->repository()->accounts->getMailboxes($this->formData['domain']);
  82. return $mailBoxes;
  83. }
  84. /**
  85. * @return Account
  86. */
  87. protected function getModel()
  88. {
  89. $accountSize = $this->productManager->get(ProductParams::ACCOUNT_SIZE);
  90. /**
  91. * create new account in kerio
  92. */
  93. $account = new Account();
  94. $account->setName($this->formData['username'].'@'.$this->formData['domain']);
  95. $account->setPassword(html_entity_decode($this->formData['password']), ENT_QUOTES);
  96. /**
  97. * set account attributes
  98. */
  99. $account->setAttr(Account::ATTR_FIRSTNAME, $this->formData['firstname']);
  100. $account->setAttr(Account::ATTR_LASTNAME, $this->formData['lastname']);
  101. $account->setAttr(Account::ATTR_PHONE, $this->formData['phone']);
  102. $account->setAttr(Account::ATTR_MOBILE_PHONE, $this->formData['mobile_phone']);
  103. $account->setAttr(Account::ATTR_FAX, $this->formData['fax']);
  104. $account->setAttr(Account::ATTR_PAGER, $this->formData['pager']);
  105. $account->setAttr(Account::ATTR_HOME_PHONE, $this->formData['home_phone']);
  106. $account->setAttr(Account::ATTR_COUNTRY, $this->formData['country']);
  107. $account->setAttr(Account::ATTR_STATE, $this->formData['state']);
  108. $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
  109. $account->setAttr(Account::ATTR_POSTAL_CODE, $this->formData['post_code']);
  110. $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
  111. $account->setAttr(Account::ATTR_STREET, $this->formData['street']);
  112. $account->setAttr(Account::ATTR_COMPANY, $this->formData['company']);
  113. $account->setAttr(Account::ATTR_ACCOUNT_STATUS, $this->formData['status']);
  114. $account->setAttr(Account::ATTR_DISPLAY_NAME, $this->formData['display_name']);
  115. $account->setAttr(Account::ATTR_MAIL_QUOTA, $accountSize * Size::B_TO_MB);
  116. foreach($this->productManager->getKerioConfiguration() as $key => $value)
  117. {
  118. $value = $value === ProductParams::SWITCHER_ENABLED ? Kerio::ATTR_ENABLED : Kerio::ATTR_DISABLED;
  119. $account->setAttr($key, $value);
  120. }
  121. return $account;
  122. }
  123. /**
  124. * @return bool|mixed|Account|void
  125. */
  126. protected function process()
  127. {
  128. /**
  129. *
  130. */
  131. $model = $this->getModel();
  132. /**
  133. * create account in KERIO
  134. */
  135. $result = $this->api->account->create($model);
  136. /**
  137. * problem with create account
  138. */
  139. if(!$result)
  140. {
  141. $this->setError($this->api->account->getLastResult()->getLastErrorCode());
  142. return false;
  143. }
  144. return $result;
  145. }
  146. }