EditAccountDataProvider.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /*
  118. $result = $repository->accounts->getAccountOptionsById($this->actionElementId);
  119. if($result instanceof Response && $result->getLastError())
  120. {
  121. throw new \Exception($result->getLastError());
  122. }
  123. $mailBoxParams = explode('@', $result->getName());
  124. $this->data['id'] = $result->getId();
  125. $this->data['username'] = $mailBoxParams[0];
  126. $this->data['domain'] = $mailBoxParams[1];
  127. $this->data['firstname'] = $result->getDataResourceA(Account::ATTR_FIRSTNAME);
  128. $this->data['lastname'] = $result->getDataResourceA(Account::ATTR_LASTNAME);
  129. $this->data['display_name'] = $result->getDataResourceA(Account::ATTR_DISPLAY_NAME);
  130. $this->data['status'] = $result->getDataResourceA(Account::ATTR_ACCOUNT_STATUS);
  131. $this->data['company'] = $result->getDataResourceA(Account::ATTR_COMPANY);
  132. $this->data['title'] = $result->getDataResourceA(Account::ATTR_PROF_TITLE);
  133. $this->data['phone'] = $result->getDataResourceA(Account::ATTR_PHONE);
  134. $this->data['home_phone'] = $result->getDataResourceA(Account::ATTR_HOME_PHONE);
  135. $this->data['mobile_phone'] = $result->getDataResourceA(Account::ATTR_MOBILE_PHONE);
  136. $this->data['fax'] = $result->getDataResourceA(Account::ATTR_FAX);
  137. $this->data['pager'] = $result->getDataResourceA(Account::ATTR_PAGER);
  138. $this->data['country'] = $result->getDataResourceA(Account::ATTR_COUNTRY);
  139. $this->data['city'] = $result->getDataResourceA(Account::ATTR_CITY);
  140. $this->data['street'] = $result->getDataResourceA(Account::ATTR_STREET);
  141. $this->data['post_code'] = $result->getDataResourceA(Account::ATTR_POSTAL_CODE);
  142. $this->data['currentCosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  143. $this->data['cosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  144. $this->data['state'] = $result->getDataResourceA(Account::ATTR_STATE);
  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. $this->readCosParams(); */
  155. }
  156. /**
  157. * @return HtmlDataJsonResponse
  158. */
  159. public function update()
  160. {
  161. /**
  162. * hosting id
  163. */
  164. $hid = $this->request->get('id');
  165. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  166. foreach ($this->formData as $field => &$value)
  167. {
  168. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  169. }
  170. /**
  171. * product manager allow to check product settings
  172. */
  173. $productManager = new ProductManager();
  174. $productManager->loadByHostingId($hid);
  175. /**
  176. *
  177. * get soap create domain service
  178. */
  179. $service =(new KerioManager())
  180. ->getApiByHosting($hid)
  181. ->soap
  182. ->service()
  183. ->updateAccount($productManager->get('cos_name'));
  184. /**
  185. *
  186. * set product manager & form data to service
  187. */
  188. $service
  189. ->setProductManager($productManager)
  190. ->setFormData($this->formData);
  191. /**
  192. * run service
  193. */
  194. $result = $service->run();
  195. /**
  196. * return success or error response
  197. */
  198. if(!$result)
  199. {
  200. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  201. }
  202. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  203. }
  204. /**
  205. * @return HtmlDataJsonResponse
  206. */
  207. public function updateStatus()
  208. {
  209. /**
  210. * hosting id
  211. */
  212. $hid = $this->request->get('id');
  213. /**
  214. * product manager allow to check product settings
  215. */
  216. $productManager = new ProductManager();
  217. $productManager->loadByHostingId($hid);
  218. /**
  219. *
  220. * get soap create domain service
  221. */
  222. $service =(new KerioManager())
  223. ->getApiByHosting($hid)
  224. ->soap
  225. ->service()
  226. ->updateAccountStatus()
  227. ->setProductManager($productManager)
  228. ;
  229. /**
  230. *
  231. * set product manager & form data to service
  232. */
  233. /**
  234. * run service for each id
  235. */
  236. $service->setFormData($this->formData);
  237. $result = $service->run();
  238. if(!$result)
  239. {
  240. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  241. }
  242. /**
  243. * return success
  244. */
  245. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  246. }
  247. /**
  248. * @return HtmlDataJsonResponse
  249. */
  250. public function changePassword()
  251. {
  252. /**
  253. * hosting id
  254. */
  255. $hid = $this->request->get('id');
  256. /**
  257. * product manager allow to check product settings
  258. */
  259. $productManager = new ProductManager();
  260. $productManager->loadByHostingId($hid);
  261. /**
  262. *
  263. * get soap create domain service
  264. */
  265. $service =(new KerioManager())
  266. ->getApiByHosting($hid)
  267. ->soap
  268. ->service()
  269. ->updateAccountPassword()
  270. ->setProductManager($productManager)
  271. ;
  272. /**
  273. *
  274. * set product manager & form data to service
  275. */
  276. /**
  277. * run service for each id
  278. */
  279. $service->setFormData($this->formData);
  280. $result = $service->run();
  281. if(!$result)
  282. {
  283. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  284. }
  285. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  286. }
  287. /**
  288. *
  289. */
  290. public function readCosParams()
  291. {
  292. $hid = $this->getRequestValue('id');
  293. /**
  294. * product manager allow to check product settings
  295. */
  296. $productManager = new ProductManager();
  297. $productManager->loadByHostingId($hid);
  298. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  299. {
  300. /**
  301. *
  302. * get soap create domain service
  303. */
  304. $api =(new KerioManager())
  305. ->getApiByHosting($hid)
  306. ->soap;
  307. /**
  308. *
  309. * get cos from API
  310. */
  311. $classOfServices = $api->repository()->cos->all();
  312. /**
  313. *
  314. * load configurable options coses
  315. */
  316. $supportedCos = $productManager->getSettingCos();
  317. /**
  318. *
  319. * add COS to array
  320. */
  321. $configoptions = $this->getFilteredCosConfigurableOptions();
  322. foreach($classOfServices as $cos)
  323. {
  324. /**
  325. *
  326. *
  327. * skip COS which is not used in configurable options
  328. */
  329. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  330. {
  331. continue;
  332. }
  333. /**
  334. *
  335. * skip if class of services doesnt exists in config option list
  336. */
  337. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  338. {
  339. continue;
  340. }
  341. /**
  342. * skip not purchased as CO
  343. */
  344. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  345. {
  346. continue;
  347. }
  348. /**
  349. * 1. check if config opts are not available
  350. * 2. skip if quantity === 0
  351. */
  352. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  353. {
  354. continue;
  355. }
  356. /* @var $cos ClassOfService*/
  357. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  358. }
  359. }
  360. }
  361. /**
  362. * @return bool|mixed
  363. */
  364. protected function getFilteredCosConfigurableOptions()
  365. {
  366. $configoptions = $this->getWhmcsParamByKey('configoptions');
  367. foreach($configoptions as $key => $value)
  368. {
  369. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  370. {
  371. unset($configoptions[$key]);
  372. }
  373. }
  374. return $configoptions;
  375. }
  376. }