EditAccountDataProvider.php 12 KB

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