CreateAccount.php 5.2 KB

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