CreateAccountCosQuota.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Class CreateAccountCosQuota
  4. * User: ThurData
  5. * Date: 2019-10-07
  6. * Time: 14:05
  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\Kerio\Components\Api\Soap\Models\Account;
  14. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  15. class CreateAccountCosQuota extends CreateAccount
  16. {
  17. /**
  18. * array with class of service models
  19. * @var array
  20. */
  21. protected $cosModels = [];
  22. /**
  23. * configure service before will run
  24. */
  25. protected function config()
  26. {
  27. /**
  28. *
  29. * load class of service models
  30. */
  31. $this->cosModels = $this->api->repository()->cos->all();
  32. /**
  33. * parent configuration
  34. */
  35. parent::config();
  36. }
  37. /**
  38. *
  39. * @return bool
  40. */
  41. public function isValid()
  42. {
  43. /**
  44. *
  45. * if parent service is not valid, return false
  46. */
  47. if (!$result = parent::isValid())
  48. {
  49. return $result;
  50. }
  51. /**
  52. * check limits for dedicated class of service
  53. */
  54. $groupedByCos = $this->api->repository()->accounts->getGroupedByCos($this->formData['domain']);
  55. $cosLimits = $this->productManager->getSettingCos();
  56. /**
  57. *
  58. * if is configured one quote in product configuration and provided by client is not the same
  59. */
  60. if(is_string($cosLimits) && $cosLimits !== $this->formData['cosId'])
  61. {
  62. $this->setError('Quota id is not valid.');
  63. return false;
  64. }
  65. /**
  66. *
  67. * if is configured one quote
  68. */
  69. if(is_string($cosLimits) && $cosLimits === $this->formData['cosId']){
  70. return true;
  71. }
  72. /**
  73. * check if type of quote is availabel for client
  74. */
  75. if(!array_key_exists($this->formData['cosId'], $cosLimits))
  76. {
  77. $this->setError('There is no quota available to use');
  78. return false;
  79. }
  80. /**
  81. * check if limit has been reached
  82. */
  83. if(is_countable($groupedByCos[$this->formData['cosId']])) {
  84. if ($cosLimits[$this->formData['cosId']] !== Size::UNLIMITED && count($groupedByCos[$this->formData['cosId']]) >= $cosLimits[$this->formData['cosId']] && $this->formData['cosId'] !== $this->formData['currentCosId'])
  85. {
  86. $this->setError('There are too many mailboxes with selected quota limit');
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. /**
  93. * @return Account
  94. */
  95. protected function getModel()
  96. {
  97. /**
  98. * create new account in kerio
  99. */
  100. $account = new Account();
  101. $account->setName($this->formData['username'].'@'.$this->formData['domain']);
  102. $account->setPassword(html_entity_decode($this->formData['password']), ENT_QUOTES);
  103. /**
  104. * set account attributes
  105. */
  106. $account->setAttr(Account::ATTR_FIRSTNAME, $this->formData['firstname']);
  107. $account->setAttr(Account::ATTR_LASTNAME, $this->formData['lastname']);
  108. $account->setAttr(Account::ATTR_PHONE, $this->formData['phone']);
  109. $account->setAttr(Account::ATTR_MOBILE_PHONE, $this->formData['mobile_phone']);
  110. $account->setAttr(Account::ATTR_FAX, $this->formData['fax']);
  111. $account->setAttr(Account::ATTR_PAGER, $this->formData['pager']);
  112. $account->setAttr(Account::ATTR_HOME_PHONE, $this->formData['home_phone']);
  113. $account->setAttr(Account::ATTR_COUNTRY, $this->formData['country']);
  114. $account->setAttr(Account::ATTR_STATE, $this->formData['state']);
  115. $account->setAttr(Account::ATTR_POSTAL_CODE, $this->formData['post_code']);
  116. $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
  117. $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
  118. $account->setAttr(Account::ATTR_STREET, $this->formData['street']);
  119. $account->setAttr(Account::ATTR_COMPANY, $this->formData['company']);
  120. $account->setAttr(Account::ATTR_ACCOUNT_STATUS, $this->formData['status']);
  121. $account->setAttr(Account::ATTR_DISPLAY_NAME, $this->formData['display_name']);
  122. /* @var $cos ClassOfService*/
  123. $cos = $this->cosModels[$this->formData['cosId']];
  124. /**
  125. * set quota by class of service
  126. */
  127. $account->setAttr(Account::ATTR_MAIL_QUOTA, $cos->getDataResourceA(Account::ATTR_MAIL_QUOTA));
  128. /**
  129. * define class of services attribute for account
  130. */
  131. $cosAttrs = $cos->getAllDataResourcesAAttributes();
  132. foreach(Kerio::BASE_ACCOUNT_CONFIG as $key)
  133. {
  134. $value = $cosAttrs[$key] ? $cosAttrs[$key] : Kerio::ATTR_DISABLED;
  135. $account->setAttr($key, $value);
  136. }
  137. /**
  138. *
  139. * set class of service id as account attribute
  140. */
  141. $account->setAttr(Account::ATTR_CLASS_OF_SERVICE_ID, $cos->getId());
  142. return $account;
  143. }
  144. /**
  145. * @return mixed
  146. */
  147. protected function getMailboxes()
  148. {
  149. $mailBoxes = $this->api->repository()->accounts->getMailboxes($this->formData['domain']);
  150. $filterbyCOS = $this->productManager->get('filterAccountsByCOS');
  151. /**
  152. * todo should be taken from option or from configurable options, is depend if from config option or dedicated class of service has been selected
  153. */
  154. $getConfigOptCos = $this->formData['cosId'];
  155. /**
  156. * filter mailboxes if required
  157. */
  158. if($getConfigOptCos && $filterbyCOS === ProductParams::SWITCHER_ENABLED)
  159. {
  160. $mailBoxes = array_filter($mailBoxes, function($row) use ($getConfigOptCos)
  161. {
  162. /* @var $row Account*/
  163. return $row->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID) == $getConfigOptCos;
  164. });
  165. }
  166. return $mailBoxes;
  167. }
  168. }