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(count($mailBoxes) >= $accountLimit && $accountLimit !== ProductParams::SIZE_UNLIMITED && 'editAccountForm' !== $this->getRequestValue('loadData'))
  65. {
  66. $this->setError('There are too many mailboxes');
  67. return false;
  68. }
  69. /**
  70. *
  71. */
  72. return parent::isValid();
  73. }
  74. /**
  75. * @return mixed
  76. */
  77. protected function getMailboxes()
  78. {
  79. $mailBoxes = $this->api->repository()->accounts->getMailboxes($this->formData['domain']);
  80. return $mailBoxes;
  81. }
  82. /**
  83. * @return Account
  84. */
  85. protected function getModel()
  86. {
  87. $accountSize = $this->productManager->get(ProductParams::ACCOUNT_SIZE);
  88. /**
  89. * create new account in kerio
  90. */
  91. $account = new Account();
  92. $account->setName($this->formData['username'].'@'.$this->formData['domain']);
  93. $account->setPassword(html_entity_decode($this->formData['password']), ENT_QUOTES);
  94. /**
  95. * set account attributes
  96. */
  97. $account->setAttr(Account::ATTR_FIRSTNAME, $this->formData['firstname']);
  98. $account->setAttr(Account::ATTR_LASTNAME, $this->formData['lastname']);
  99. $account->setAttr(Account::ATTR_PHONE, $this->formData['phone']);
  100. $account->setAttr(Account::ATTR_MOBILE_PHONE, $this->formData['mobile_phone']);
  101. $account->setAttr(Account::ATTR_FAX, $this->formData['fax']);
  102. $account->setAttr(Account::ATTR_PAGER, $this->formData['pager']);
  103. $account->setAttr(Account::ATTR_HOME_PHONE, $this->formData['home_phone']);
  104. $account->setAttr(Account::ATTR_COUNTRY, $this->formData['country']);
  105. $account->setAttr(Account::ATTR_STATE, $this->formData['state']);
  106. $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
  107. $account->setAttr(Account::ATTR_POSTAL_CODE, $this->formData['post_code']);
  108. $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
  109. $account->setAttr(Account::ATTR_STREET, $this->formData['street']);
  110. $account->setAttr(Account::ATTR_COMPANY, $this->formData['company']);
  111. $account->setAttr(Account::ATTR_ACCOUNT_STATUS, $this->formData['status']);
  112. $account->setAttr(Account::ATTR_DISPLAY_NAME, $this->formData['display_name']);
  113. $account->setAttr(Account::ATTR_MAIL_QUOTA, $accountSize * Size::B_TO_MB);
  114. foreach($this->productManager->getKerioConfiguration() as $key => $value)
  115. {
  116. $value = $value === ProductParams::SWITCHER_ENABLED ? Kerio::ATTR_ENABLED : Kerio::ATTR_DISABLED;
  117. $account->setAttr($key, $value);
  118. }
  119. return $account;
  120. }
  121. /**
  122. * @return bool|mixed|Account|void
  123. */
  124. protected function process()
  125. {
  126. /**
  127. *
  128. */
  129. $model = $this->getModel();
  130. /**
  131. * create account in KERIO
  132. */
  133. $result = $this->api->account->create($model);
  134. /**
  135. * problem with create account
  136. */
  137. if(!$result)
  138. {
  139. $this->setError($this->api->account->getLastResult()->getLastErrorCode());
  140. return false;
  141. }
  142. return $result;
  143. }
  144. }