EditAccountDataProvider.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateAccount;
  12. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  14. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  15. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  17. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  18. /**
  19. *
  20. * Created by PhpStorm.
  21. * User: ThurData
  22. * Date: 18.09.19
  23. * Time: 09:35
  24. * Class EditAccountDataProvider
  25. */
  26. class EditAccountDataProvider extends BaseDataProvider
  27. {
  28. /**
  29. *
  30. */
  31. public function read()
  32. {
  33. /**
  34. * hosting id
  35. */
  36. $hid = $this->getRequestValue('id');
  37. /**
  38. * load hosting
  39. */
  40. $hosting = Hosting::where('id', $hid)->first();
  41. /**
  42. * load api
  43. */
  44. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  45. $fields =array(
  46. 'id',
  47. 'companyContactId',
  48. 'loginName',
  49. 'fullName',
  50. 'description',
  51. 'isEnabled',
  52. 'emailAddresses',
  53. );
  54. $cond = array(
  55. array(
  56. "fieldName" => "id",
  57. "comparator" => "Eq",
  58. "value" => $this->actionElementId
  59. )
  60. );
  61. try {
  62. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  63. } catch (KerioApiException $error) {
  64. logModuleCall(
  65. 'kerioEmail',
  66. __FUNCTION__,
  67. $error,
  68. 'Debug Error',
  69. $error->getMessage()
  70. );
  71. return ['error' => $error->getMessage()];
  72. }
  73. try {
  74. $domainID = $api->getDomainId($this->getWhmcsParamByKey('customfields')['maildomain']);
  75. } catch (KerioApiException $error) {
  76. logModuleCall(
  77. 'kerioEmail',
  78. __FUNCTION__,
  79. $error,
  80. 'Debug Error',
  81. $error->getMessage()
  82. );
  83. return ['error' => $error->getMessage()];
  84. }
  85. try {
  86. $account = $api->getUsers($fields,$domainID,$cond);
  87. } catch (KerioApiException $error) {
  88. logModuleCall(
  89. 'kerioEmail',
  90. __FUNCTION__,
  91. $error,
  92. 'Debug Error',
  93. $error->getMessage()
  94. );
  95. return ['error' => $error->getMessage()];
  96. }
  97. try {
  98. $address = $api->getAddress($this->actionElementId);
  99. } catch (KerioApiException $error) {
  100. logModuleCall(
  101. 'kerioEmail',
  102. __FUNCTION__,
  103. $error,
  104. 'Debug Error',
  105. $error->getMessage()
  106. );
  107. return ['error' => $error->getMessage()];
  108. }
  109. $api->logout();
  110. logModuleCall(
  111. 'kerioEmail',
  112. __FUNCTION__,
  113. $account,
  114. 'Debug Error',
  115. $address
  116. );
  117. $this->data['id'] = $account['id'];
  118. $this->data['username'] = $account['loginName'];
  119. $this->data['domain'] = $this->getWhmcsParamByKey('customfields')['maildomain'];
  120. $this->data['firstname'] = $address['contacts'][0]['firstName'];
  121. $this->data['lastname'] = $address['contacts'][0]['surName'];
  122. $this->data['display_name'] = $address['contacts'][0]['commonName'];
  123. $this->data['status'] = $account['isEnabled'];
  124. $this->data['company'] = $address['contacts'][0]['companyName'];
  125. $this->data['title'] = $address['contacts'][0]['titleBefore'];
  126. $this->data['phone'] = $address['contacts'][0]['phoneNumberWorkVoice'];
  127. $this->data['home_phone'] = '';
  128. $this->data['mobile_phone'] = $address['contacts'][0]['phoneNumberMobile'];
  129. $this->data['fax'] = '';
  130. $this->data['pager'] = '';
  131. $this->data['country'] = $address['contacts'][0]['postalAddressWork']['country'];
  132. $this->data['city'] = $address['contacts'][0]['postalAddressWork'][''];
  133. $this->data['street'] = $address['contacts'][0]['postalAddressWork'][''];
  134. $this->data['post_code'] = $address['contacts'][0]['postalAddressWork'][''];
  135. $this->data['currentCosId'] = '';
  136. $this->data['cosId'] = '';
  137. $this->data['state'] = $address['contacts'][0]['postalAddressWork'][''];
  138. logModuleCall(
  139. 'kerioEmail',
  140. __FUNCTION__,
  141. $account,
  142. 'Debug Error',
  143. $this->data
  144. );
  145. $lang = di('lang');
  146. $this->availableValues['status'] = [
  147. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  148. Kerio::ACC_STATUS_LOCKED => $lang->absoluteT('kerio','account','status','locked'),
  149. Kerio::ACC_STATUS_MAINTENANCE => $lang->absoluteT('kerio','account','status','maintenance'),
  150. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  151. Kerio::ACC_STATUS_LOCKOUT => $lang->absoluteT('kerio','account','status','lockout'),
  152. Kerio::ACC_STATUS_PENDING => $lang->absoluteT('kerio','account','status','pending')
  153. ];
  154. }
  155. /**
  156. * @return HtmlDataJsonResponse
  157. */
  158. public function update()
  159. {
  160. /**
  161. * hosting id
  162. */
  163. $hid = $this->request->get('id');
  164. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  165. foreach ($this->formData as $field => &$value)
  166. {
  167. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  168. }
  169. /**
  170. * product manager allow to check product settings
  171. */
  172. $productManager = new ProductManager();
  173. $productManager->loadByHostingId($hid);
  174. /**
  175. *
  176. * get soap create domain service
  177. */
  178. $service =(new KerioManager())
  179. ->getApiByHosting($hid)
  180. ->soap
  181. ->service()
  182. ->updateAccount($productManager->get('cos_name'));
  183. /**
  184. *
  185. * set product manager & form data to service
  186. */
  187. $service
  188. ->setProductManager($productManager)
  189. ->setFormData($this->formData);
  190. /**
  191. * run service
  192. */
  193. $result = $service->run();
  194. /**
  195. * return success or error response
  196. */
  197. if(!$result)
  198. {
  199. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  200. }
  201. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  202. }
  203. /**
  204. * @return HtmlDataJsonResponse
  205. */
  206. public function updateStatus()
  207. {
  208. /**
  209. * hosting id
  210. */
  211. $hid = $this->request->get('id');
  212. /**
  213. * product manager allow to check product settings
  214. */
  215. $productManager = new ProductManager();
  216. $productManager->loadByHostingId($hid);
  217. /**
  218. *
  219. * get soap create domain service
  220. */
  221. $service =(new KerioManager())
  222. ->getApiByHosting($hid)
  223. ->soap
  224. ->service()
  225. ->updateAccountStatus()
  226. ->setProductManager($productManager)
  227. ;
  228. /**
  229. *
  230. * set product manager & form data to service
  231. */
  232. /**
  233. * run service for each id
  234. */
  235. $service->setFormData($this->formData);
  236. $result = $service->run();
  237. if(!$result)
  238. {
  239. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  240. }
  241. /**
  242. * return success
  243. */
  244. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  245. }
  246. /**
  247. * @return HtmlDataJsonResponse
  248. */
  249. public function changePassword()
  250. {
  251. /**
  252. * hosting id
  253. */
  254. $hid = $this->request->get('id');
  255. /**
  256. * product manager allow to check product settings
  257. */
  258. $productManager = new ProductManager();
  259. $productManager->loadByHostingId($hid);
  260. /**
  261. *
  262. * get soap create domain service
  263. */
  264. $service =(new KerioManager())
  265. ->getApiByHosting($hid)
  266. ->soap
  267. ->service()
  268. ->updateAccountPassword()
  269. ->setProductManager($productManager)
  270. ;
  271. /**
  272. *
  273. * set product manager & form data to service
  274. */
  275. /**
  276. * run service for each id
  277. */
  278. $service->setFormData($this->formData);
  279. $result = $service->run();
  280. if(!$result)
  281. {
  282. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  283. }
  284. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  285. }
  286. /**
  287. *
  288. */
  289. public function readCosParams()
  290. {
  291. $hid = $this->getRequestValue('id');
  292. /**
  293. * product manager allow to check product settings
  294. */
  295. $productManager = new ProductManager();
  296. $productManager->loadByHostingId($hid);
  297. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  298. {
  299. /**
  300. *
  301. * get soap create domain service
  302. */
  303. $api =(new KerioManager())
  304. ->getApiByHosting($hid)
  305. ->soap;
  306. /**
  307. *
  308. * get cos from API
  309. */
  310. $classOfServices = $api->repository()->cos->all();
  311. /**
  312. *
  313. * load configurable options coses
  314. */
  315. $supportedCos = $productManager->getSettingCos();
  316. /**
  317. *
  318. * add COS to array
  319. */
  320. $configoptions = $this->getFilteredCosConfigurableOptions();
  321. foreach($classOfServices as $cos)
  322. {
  323. /**
  324. *
  325. *
  326. * skip COS which is not used in configurable options
  327. */
  328. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  329. {
  330. continue;
  331. }
  332. /**
  333. *
  334. * skip if class of services doesnt exists in config option list
  335. */
  336. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  337. {
  338. continue;
  339. }
  340. /**
  341. * skip not purchased as CO
  342. */
  343. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  344. {
  345. continue;
  346. }
  347. /**
  348. * 1. check if config opts are not available
  349. * 2. skip if quantity === 0
  350. */
  351. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  352. {
  353. continue;
  354. }
  355. /* @var $cos ClassOfService*/
  356. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  357. }
  358. }
  359. }
  360. /**
  361. * @return bool|mixed
  362. */
  363. protected function getFilteredCosConfigurableOptions()
  364. {
  365. $configoptions = $this->getWhmcsParamByKey('configoptions');
  366. foreach($configoptions as $key => $value)
  367. {
  368. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  369. {
  370. unset($configoptions[$key]);
  371. }
  372. }
  373. return $configoptions;
  374. }
  375. }