AccountDataProvider.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  4. use ThurData\Servers\KerioEmail\App\Enums\Size;
  5. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  6. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  7. use ThurData\Servers\KerioEmail\App\Http\Admin\ProductConfiguration;
  8. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\ServiceFactory;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  12. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  13. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  14. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  15. use ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  17. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  18. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  19. /**
  20. *
  21. * Created by PhpStorm.
  22. * User: ThurData
  23. * Date: 10.09.19
  24. * Time: 13:06
  25. * Class AccountDataProvider
  26. */
  27. class AccountDataProvider extends BaseDataProvider
  28. {
  29. public function read()
  30. {
  31. /**
  32. * hosting id
  33. */
  34. $hid = $this->request->get('id');
  35. $hosting = Hosting::where('id', $hid)->first();
  36. $lang = di('lang');
  37. $this->availableValues['status'] = [
  38. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  39. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  40. ];
  41. $this->availableValues['unit'] = [
  42. 'MegaBytes' => 'MB',
  43. 'GigaBytes' => 'GB',
  44. ];
  45. }
  46. public function create()
  47. {
  48. /**
  49. * hosting id
  50. */
  51. $hid = $this->request->get('id');
  52. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  53. foreach ($this->formData as $field => &$value)
  54. {
  55. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  56. }
  57. /**
  58. * product manager allow to check product settings
  59. */
  60. $productManager = new ProductManager();
  61. $productManager->loadByHostingId($hid);
  62. $maildomain = $this->getWhmcsParamByKey('customfields')['maildomain'];
  63. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  64. try {
  65. $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
  66. $domainID = $api->getDomainId($maildomain);
  67. } catch (KerioApiException $error) {
  68. logModuleCall(
  69. 'kerioEmail',
  70. __FUNCTION__,
  71. $error,
  72. 'Debug Error',
  73. $error->getMessage()
  74. );
  75. return ['error' => $error->getMessage()];
  76. }
  77. if ($domainID === FALSE) {
  78. return "Error: Domain $maildomain not found";
  79. }
  80. try {
  81. $userID = $api->createUser($domainID, $this->formData['username'], $this->formData['password']);
  82. } catch (KerioApiException $error) {
  83. logModuleCall(
  84. 'kerioEmail',
  85. __FUNCTION__,
  86. $error,
  87. 'Debug Error',
  88. $error->getMessage()
  89. );
  90. return ['error' => $error->getMessage()];
  91. }
  92. $account['fullName'] = $this->formData['display_name'];
  93. if ($this->formData['quota'] > 0) {
  94. $account['diskSizeLimit']['isActive'] = TRUE;
  95. $account['diskSizeLimit']['limit']['value'] = $this->formData['quota'];
  96. $account['diskSizeLimit']['limit']['unit'] = $this->formData['unit'];
  97. }
  98. $fields['firstName'] = $this->formData['firstname'];
  99. $fields['surName'] = $this->formData['lastname'];
  100. $fields['commonName'] = $this->formData['display_name'];
  101. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  102. $fields['titleBefore'] = $this->formData['title'];
  103. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  104. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  105. $fields['departmentName'] = $this->formData['department'];
  106. $fields['profession'] = $this->formData['profession'];
  107. logModuleCall(
  108. 'kerioEmail',
  109. __FUNCTION__,
  110. $domainID,
  111. 'Debug Add Account',
  112. $userID
  113. );
  114. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  115. }
  116. public function updateStatus()
  117. {
  118. }
  119. public function update()
  120. {
  121. /**
  122. * hosting id
  123. */
  124. $hid = $this->request->get('id');
  125. /**
  126. * product manager allow to check product settings
  127. */
  128. $productManager = new ProductManager();
  129. $productManager->loadByHostingId($hid);
  130. /**
  131. * run service for each id
  132. */
  133. foreach($this->request->get('massActions') as $id)
  134. {
  135. $service->setFormData(['status' => $this->formData['status'], 'id' => $id]);
  136. }
  137. /**
  138. * return success
  139. */
  140. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  141. }
  142. /**
  143. *
  144. * read data per cos_name
  145. */
  146. protected function readCosParams()
  147. {
  148. $hid = $this->request->get('id');
  149. /**
  150. *
  151. * load product manager
  152. */
  153. $productManager = new ProductManager();
  154. $productManager->loadByHostingId($hid);
  155. /**
  156. *
  157. * check if class of services should be selectable
  158. */
  159. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  160. {
  161. /**
  162. *
  163. * get soap create domain service
  164. */
  165. $api =(new KerioManager())
  166. ->getApiByHosting($hid)
  167. ->soap;
  168. /**
  169. *
  170. * get cos from API
  171. */
  172. $classOfServices = $api->repository()->cos->all();
  173. /**
  174. *
  175. * load configurable options coses
  176. */
  177. $supportedCos = $productManager->getSettingCos();
  178. /**
  179. *
  180. * add COS to array
  181. */
  182. $configoptions = $this->getFilteredCosConfigurableOptions();
  183. foreach($classOfServices as $cos)
  184. {
  185. /**
  186. *
  187. *
  188. * skip COS which is not used in configurable options
  189. */
  190. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  191. {
  192. continue;
  193. }
  194. /**
  195. * 1. check if config opts are available
  196. * 2. skip if class of services doesnt exists in config option list
  197. */
  198. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  199. {
  200. continue;
  201. }
  202. /**
  203. * 1. check if config opts are available
  204. * 2. skip not purchased as CO
  205. */
  206. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  207. {
  208. continue;
  209. }
  210. /**
  211. * 1. check if config opts are not available
  212. * 2. skip if quantity === 0
  213. */
  214. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  215. {
  216. continue;
  217. }
  218. /* @var $cos ClassOfService*/
  219. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  220. }
  221. return $this;
  222. }
  223. /**
  224. *
  225. * check if class of service is choosen by config opt
  226. */
  227. if($productManager->get('cos_name') === ClassOfServices::KERIO_CONFIG_OPTIONS)
  228. {
  229. $this->data['cosId'] = key($productManager->getSettingCos());
  230. return $this;
  231. }
  232. /**
  233. *
  234. * if cos_name is dedicated (loaded from API)
  235. */
  236. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_KERIO)
  237. {
  238. /**
  239. * if dedicated class of service has been selected
  240. */
  241. $this->data['cosId'] = $productManager->get('cos_name');
  242. return $this;
  243. }
  244. return $this;
  245. }
  246. /**
  247. * @return bool|mixed
  248. */
  249. protected function getFilteredCosConfigurableOptions()
  250. {
  251. $configoptions = $this->getWhmcsParamByKey('configoptions');
  252. foreach($configoptions as $key => $value)
  253. {
  254. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  255. {
  256. unset($configoptions[$key]);
  257. }
  258. }
  259. return $configoptions;
  260. }
  261. }