EditAccountDataProvider.php 12 KB

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