EditAccountDataProvider.php 11 KB

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