EditAccountDataProvider.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /**
  18. *
  19. * Created by PhpStorm.
  20. * User: ThurData
  21. * Date: 18.09.19
  22. * Time: 09:35
  23. * Class EditAccountDataProvider
  24. */
  25. class EditAccountDataProvider extends BaseDataProvider
  26. {
  27. /**
  28. *
  29. */
  30. public function read()
  31. {
  32. /**
  33. * hosting id
  34. */
  35. $hid = $this->getRequestValue('id');
  36. logModuleCall(
  37. 'kerioEmail',
  38. __FUNCTION__,
  39. $this->actionElementId,
  40. 'Debug Error',
  41. $hid
  42. );
  43. /**
  44. * load hosting
  45. */
  46. $hosting = Hosting::where('id', $hid)->first();
  47. /**
  48. * load api
  49. */
  50. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  51. try {
  52. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  53. // $domains = $api->getDomains(['id','name']);
  54. } catch (KerioApiException $error) {
  55. logModuleCall(
  56. 'kerioEmail',
  57. __FUNCTION__,
  58. $error,
  59. 'Debug Error',
  60. $error->getMessage()
  61. );
  62. return ['error' => $error->getMessage()];
  63. }
  64. $api->logout();
  65. /*
  66. $result = $repository->accounts->getAccountOptionsById($this->actionElementId);
  67. if($result instanceof Response && $result->getLastError())
  68. {
  69. throw new \Exception($result->getLastError());
  70. }
  71. $mailBoxParams = explode('@', $result->getName());
  72. $this->data['id'] = $result->getId();
  73. $this->data['username'] = $mailBoxParams[0];
  74. $this->data['domain'] = $mailBoxParams[1];
  75. $this->data['firstname'] = $result->getDataResourceA(Account::ATTR_FIRSTNAME);
  76. $this->data['lastname'] = $result->getDataResourceA(Account::ATTR_LASTNAME);
  77. $this->data['display_name'] = $result->getDataResourceA(Account::ATTR_DISPLAY_NAME);
  78. $this->data['status'] = $result->getDataResourceA(Account::ATTR_ACCOUNT_STATUS);
  79. $this->data['company'] = $result->getDataResourceA(Account::ATTR_COMPANY);
  80. $this->data['title'] = $result->getDataResourceA(Account::ATTR_PROF_TITLE);
  81. $this->data['phone'] = $result->getDataResourceA(Account::ATTR_PHONE);
  82. $this->data['home_phone'] = $result->getDataResourceA(Account::ATTR_HOME_PHONE);
  83. $this->data['mobile_phone'] = $result->getDataResourceA(Account::ATTR_MOBILE_PHONE);
  84. $this->data['fax'] = $result->getDataResourceA(Account::ATTR_FAX);
  85. $this->data['pager'] = $result->getDataResourceA(Account::ATTR_PAGER);
  86. $this->data['country'] = $result->getDataResourceA(Account::ATTR_COUNTRY);
  87. $this->data['city'] = $result->getDataResourceA(Account::ATTR_CITY);
  88. $this->data['street'] = $result->getDataResourceA(Account::ATTR_STREET);
  89. $this->data['post_code'] = $result->getDataResourceA(Account::ATTR_POSTAL_CODE);
  90. $this->data['currentCosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  91. $this->data['cosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  92. $this->data['state'] = $result->getDataResourceA(Account::ATTR_STATE);
  93. $lang = di('lang');
  94. $this->availableValues['status'] = [
  95. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  96. Kerio::ACC_STATUS_LOCKED => $lang->absoluteT('kerio','account','status','locked'),
  97. Kerio::ACC_STATUS_MAINTENANCE => $lang->absoluteT('kerio','account','status','maintenance'),
  98. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  99. Kerio::ACC_STATUS_LOCKOUT => $lang->absoluteT('kerio','account','status','lockout'),
  100. Kerio::ACC_STATUS_PENDING => $lang->absoluteT('kerio','account','status','pending')
  101. ];
  102. $this->readCosParams(); */
  103. }
  104. /**
  105. * @return HtmlDataJsonResponse
  106. */
  107. public function update()
  108. {
  109. /**
  110. * hosting id
  111. */
  112. $hid = $this->request->get('id');
  113. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  114. foreach ($this->formData as $field => &$value)
  115. {
  116. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  117. }
  118. /**
  119. * product manager allow to check product settings
  120. */
  121. $productManager = new ProductManager();
  122. $productManager->loadByHostingId($hid);
  123. /**
  124. *
  125. * get soap create domain service
  126. */
  127. $service =(new KerioManager())
  128. ->getApiByHosting($hid)
  129. ->soap
  130. ->service()
  131. ->updateAccount($productManager->get('cos_name'));
  132. /**
  133. *
  134. * set product manager & form data to service
  135. */
  136. $service
  137. ->setProductManager($productManager)
  138. ->setFormData($this->formData);
  139. /**
  140. * run service
  141. */
  142. $result = $service->run();
  143. /**
  144. * return success or error response
  145. */
  146. if(!$result)
  147. {
  148. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  149. }
  150. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  151. }
  152. /**
  153. * @return HtmlDataJsonResponse
  154. */
  155. public function updateStatus()
  156. {
  157. /**
  158. * hosting id
  159. */
  160. $hid = $this->request->get('id');
  161. /**
  162. * product manager allow to check product settings
  163. */
  164. $productManager = new ProductManager();
  165. $productManager->loadByHostingId($hid);
  166. /**
  167. *
  168. * get soap create domain service
  169. */
  170. $service =(new KerioManager())
  171. ->getApiByHosting($hid)
  172. ->soap
  173. ->service()
  174. ->updateAccountStatus()
  175. ->setProductManager($productManager)
  176. ;
  177. /**
  178. *
  179. * set product manager & form data to service
  180. */
  181. /**
  182. * run service for each id
  183. */
  184. $service->setFormData($this->formData);
  185. $result = $service->run();
  186. if(!$result)
  187. {
  188. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  189. }
  190. /**
  191. * return success
  192. */
  193. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  194. }
  195. /**
  196. * @return HtmlDataJsonResponse
  197. */
  198. public function changePassword()
  199. {
  200. /**
  201. * hosting id
  202. */
  203. $hid = $this->request->get('id');
  204. /**
  205. * product manager allow to check product settings
  206. */
  207. $productManager = new ProductManager();
  208. $productManager->loadByHostingId($hid);
  209. /**
  210. *
  211. * get soap create domain service
  212. */
  213. $service =(new KerioManager())
  214. ->getApiByHosting($hid)
  215. ->soap
  216. ->service()
  217. ->updateAccountPassword()
  218. ->setProductManager($productManager)
  219. ;
  220. /**
  221. *
  222. * set product manager & form data to service
  223. */
  224. /**
  225. * run service for each id
  226. */
  227. $service->setFormData($this->formData);
  228. $result = $service->run();
  229. if(!$result)
  230. {
  231. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  232. }
  233. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  234. }
  235. /**
  236. *
  237. */
  238. public function readCosParams()
  239. {
  240. $hid = $this->getRequestValue('id');
  241. /**
  242. * product manager allow to check product settings
  243. */
  244. $productManager = new ProductManager();
  245. $productManager->loadByHostingId($hid);
  246. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  247. {
  248. /**
  249. *
  250. * get soap create domain service
  251. */
  252. $api =(new KerioManager())
  253. ->getApiByHosting($hid)
  254. ->soap;
  255. /**
  256. *
  257. * get cos from API
  258. */
  259. $classOfServices = $api->repository()->cos->all();
  260. /**
  261. *
  262. * load configurable options coses
  263. */
  264. $supportedCos = $productManager->getSettingCos();
  265. /**
  266. *
  267. * add COS to array
  268. */
  269. $configoptions = $this->getFilteredCosConfigurableOptions();
  270. foreach($classOfServices as $cos)
  271. {
  272. /**
  273. *
  274. *
  275. * skip COS which is not used in configurable options
  276. */
  277. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  278. {
  279. continue;
  280. }
  281. /**
  282. *
  283. * skip if class of services doesnt exists in config option list
  284. */
  285. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  286. {
  287. continue;
  288. }
  289. /**
  290. * skip not purchased as CO
  291. */
  292. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  293. {
  294. continue;
  295. }
  296. /**
  297. * 1. check if config opts are not available
  298. * 2. skip if quantity === 0
  299. */
  300. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  301. {
  302. continue;
  303. }
  304. /* @var $cos ClassOfService*/
  305. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  306. }
  307. }
  308. }
  309. /**
  310. * @return bool|mixed
  311. */
  312. protected function getFilteredCosConfigurableOptions()
  313. {
  314. $configoptions = $this->getWhmcsParamByKey('configoptions');
  315. foreach($configoptions as $key => $value)
  316. {
  317. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  318. {
  319. unset($configoptions[$key]);
  320. }
  321. }
  322. return $configoptions;
  323. }
  324. }