CreateAccountCosQuota.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 ($cosLimits[$this->formData['cosId']] !== Size::UNLIMITED && count($groupedByCos[$this->formData['cosId']]) >= $cosLimits[$this->formData['cosId']] && $this->formData['cosId'] !== $this->formData['currentCosId'])
  84. {
  85. $this->setError('There are too many mailboxes with selected quota limit');
  86. return false;
  87. }
  88. return true;
  89. }
  90. /**
  91. * @return Account
  92. */
  93. protected function getModel()
  94. {
  95. /**
  96. * create new account in kerio
  97. */
  98. $account = new Account();
  99. $account->setName($this->formData['username'].'@'.$this->formData['domain']);
  100. $account->setPassword(html_entity_decode($this->formData['password']), ENT_QUOTES);
  101. /**
  102. * set account attributes
  103. */
  104. $account->setAttr(Account::ATTR_FIRSTNAME, $this->formData['firstname']);
  105. $account->setAttr(Account::ATTR_LASTNAME, $this->formData['lastname']);
  106. $account->setAttr(Account::ATTR_PHONE, $this->formData['phone']);
  107. $account->setAttr(Account::ATTR_MOBILE_PHONE, $this->formData['mobile_phone']);
  108. $account->setAttr(Account::ATTR_FAX, $this->formData['fax']);
  109. $account->setAttr(Account::ATTR_PAGER, $this->formData['pager']);
  110. $account->setAttr(Account::ATTR_HOME_PHONE, $this->formData['home_phone']);
  111. $account->setAttr(Account::ATTR_COUNTRY, $this->formData['country']);
  112. $account->setAttr(Account::ATTR_STATE, $this->formData['state']);
  113. $account->setAttr(Account::ATTR_POSTAL_CODE, $this->formData['post_code']);
  114. $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
  115. $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
  116. $account->setAttr(Account::ATTR_STREET, $this->formData['street']);
  117. $account->setAttr(Account::ATTR_COMPANY, $this->formData['company']);
  118. $account->setAttr(Account::ATTR_ACCOUNT_STATUS, $this->formData['status']);
  119. $account->setAttr(Account::ATTR_DISPLAY_NAME, $this->formData['display_name']);
  120. /* @var $cos ClassOfService*/
  121. $cos = $this->cosModels[$this->formData['cosId']];
  122. /**
  123. * set quota by class of service
  124. */
  125. $account->setAttr(Account::ATTR_MAIL_QUOTA, $cos->getDataResourceA(Account::ATTR_MAIL_QUOTA));
  126. /**
  127. * define class of services attribute for account
  128. */
  129. $cosAttrs = $cos->getAllDataResourcesAAttributes();
  130. foreach(Kerio::BASE_ACCOUNT_CONFIG as $key)
  131. {
  132. $value = $cosAttrs[$key] ? $cosAttrs[$key] : Kerio::ATTR_DISABLED;
  133. $account->setAttr($key, $value);
  134. }
  135. /**
  136. *
  137. * set class of service id as account attribute
  138. */
  139. $account->setAttr(Account::ATTR_CLASS_OF_SERVICE_ID, $cos->getId());
  140. return $account;
  141. }
  142. /**
  143. * @return mixed
  144. */
  145. protected function getMailboxes()
  146. {
  147. $mailBoxes = $this->api->repository()->accounts->getMailboxes($this->formData['domain']);
  148. $filterbyCOS = $this->productManager->get('filterAccountsByCOS');
  149. /**
  150. * todo should be taken from option or from configurable options, is depend if from config option or dedicated class of service has been selected
  151. */
  152. $getConfigOptCos = $this->formData['cosId'];
  153. /**
  154. * filter mailboxes if required
  155. */
  156. if($getConfigOptCos && $filterbyCOS === ProductParams::SWITCHER_ENABLED)
  157. {
  158. $mailBoxes = array_filter($mailBoxes, function($row) use ($getConfigOptCos)
  159. {
  160. /* @var $row Account*/
  161. return $row->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID) == $getConfigOptCos;
  162. });
  163. }
  164. return $mailBoxes;
  165. }
  166. }