EditAccountDataProvider.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. $this->data['id'] = $account[0]['id'];
  111. $this->data['username'] = $account[0]['loginName'];
  112. $this->data['domain'] = $this->getWhmcsParamByKey('customfields')['maildomain'];
  113. $this->data['firstname'] = $address['contacts'][0]['firstName'];
  114. $this->data['lastname'] = $address['contacts'][0]['surName'];
  115. $this->data['display_name'] = $address['contacts'][0]['commonName'];
  116. $this->data['status'] = $account['isEnabled'];
  117. $this->data['title'] = $address['contacts'][0]['titleBefore'];
  118. $this->data['profession'] = $address['contacts'][0]['profession'];
  119. $this->data['work_phone'] = $address['contacts'][0]['phoneNumberWorkVoice'];
  120. $this->data['mobile_phone'] = $address['contacts'][0]['phoneNumberMobile'];
  121. $this->data['department'] = $address['contacts'][0]['departmentName'];
  122. $this->data['office'] = $address['contacts'][0]['postalAddressWork']['extendedAddress'];
  123. $lang = di('lang');
  124. $this->availableValues['status'] = [
  125. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  126. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed')
  127. ];
  128. $this->availableValues['unit'] = [
  129. 'MegaBytes' => 'MB',
  130. 'GigaBytes' => 'GB',
  131. ];
  132. }
  133. /**
  134. * @return HtmlDataJsonResponse
  135. */
  136. public function update()
  137. {
  138. /**
  139. * hosting id
  140. */
  141. $hid = $this->request->get('id');
  142. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  143. foreach ($this->formData as $field => &$value)
  144. {
  145. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  146. }
  147. /**
  148. * product manager allow to check product settings
  149. */
  150. $productManager = new ProductManager();
  151. $productManager->loadByHostingId($hid);
  152. $account['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  153. if ($this->formData['quota'] > 0) {
  154. $account['diskSizeLimit']['isActive'] = TRUE;
  155. $account['diskSizeLimit']['limit']['value'] = $this->formData['quota'];
  156. $account['diskSizeLimit']['limit']['unit'] = $this->formData['unit'];
  157. } else {
  158. $account['diskSizeLimit']['isActive'] = FALSE;
  159. }
  160. $fields['firstName'] = $this->formData['firstname'];
  161. $fields['surName'] = $this->formData['lastname'];
  162. $fields['commonName'] = $this->formData['display_name'];
  163. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  164. $fields['titleBefore'] = $this->formData['title'];
  165. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  166. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  167. $fields['departmentName'] = $this->formData['department'];
  168. $fields['profession'] = $this->formData['profession'];
  169. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  170. try {
  171. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  172. } catch (KerioApiException $error) {
  173. logModuleCall(
  174. 'kerioEmail',
  175. __FUNCTION__,
  176. $error,
  177. 'Debug Error',
  178. $error->getMessage()
  179. );
  180. return ['error' => $error->getMessage()];
  181. }
  182. try {
  183. $result = $api->modifyUser($this->formData['id'], $account);
  184. } catch (KerioApiException $error) {
  185. logModuleCall(
  186. 'kerioEmail',
  187. __FUNCTION__,
  188. $error,
  189. 'Debug Error',
  190. $error->getMessage()
  191. );
  192. return ['error' => $error->getMessage()];
  193. }
  194. try {
  195. $result = $api->setAddress($this->formData['id'], $fields);
  196. } catch (KerioApiException $error) {
  197. logModuleCall(
  198. 'kerioEmail',
  199. __FUNCTION__,
  200. $error,
  201. 'Debug Error',
  202. $error->getMessage()
  203. );
  204. return ['error' => $error->getMessage()];
  205. }
  206. $api->logout();
  207. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  208. }
  209. /**
  210. * @return HtmlDataJsonResponse
  211. */
  212. public function updateStatus()
  213. {
  214. /**
  215. * hosting id
  216. */
  217. $hid = $this->request->get('id');
  218. /**
  219. * product manager allow to check product settings
  220. */
  221. $productManager = new ProductManager();
  222. $productManager->loadByHostingId($hid);
  223. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  224. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  225. try {
  226. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  227. } catch (KerioApiException $error) {
  228. logModuleCall(
  229. 'kerioEmail',
  230. __FUNCTION__,
  231. $error,
  232. 'Debug Error',
  233. $error->getMessage()
  234. );
  235. return ['error' => $error->getMessage()];
  236. }
  237. try {
  238. $result = $api->modifyUser($this->formData['id'], $status);
  239. } catch (KerioApiException $error) {
  240. logModuleCall(
  241. 'kerioEmail',
  242. __FUNCTION__,
  243. $error,
  244. 'Debug Error',
  245. $error->getMessage()
  246. );
  247. return ['error' => $error->getMessage()];
  248. }
  249. $api->logout();
  250. /**
  251. * return success
  252. */
  253. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  254. }
  255. /**
  256. * @return HtmlDataJsonResponse
  257. */
  258. public function changePassword()
  259. {
  260. /**
  261. * hosting id
  262. */
  263. $hid = $this->request->get('id');
  264. /**
  265. * product manager allow to check product settings
  266. */
  267. $productManager = new ProductManager();
  268. $productManager->loadByHostingId($hid);
  269. $fields['password'] = $this->formData['password'];
  270. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  271. try {
  272. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  273. } catch (KerioApiException $error) {
  274. logModuleCall(
  275. 'kerioEmail',
  276. __FUNCTION__,
  277. $error,
  278. 'Debug Error',
  279. $error->getMessage()
  280. );
  281. return ['error' => $error->getMessage()];
  282. }
  283. try {
  284. $result = $api->modifyUser($this->formData['id'], $fields);
  285. } catch (KerioApiException $error) {
  286. logModuleCall(
  287. 'kerioEmail',
  288. __FUNCTION__,
  289. $error,
  290. 'Debug Error',
  291. $error->getMessage()
  292. );
  293. return ['error' => $error->getMessage()];
  294. }
  295. $api->logout();
  296. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  297. }
  298. /**
  299. *
  300. */
  301. public function readCosParams()
  302. {
  303. $hid = $this->getRequestValue('id');
  304. /**
  305. * product manager allow to check product settings
  306. */
  307. $productManager = new ProductManager();
  308. $productManager->loadByHostingId($hid);
  309. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  310. {
  311. /**
  312. *
  313. * get soap create domain service
  314. */
  315. $api =(new KerioManager())
  316. ->getApiByHosting($hid)
  317. ->soap;
  318. /**
  319. *
  320. * get cos from API
  321. */
  322. $classOfServices = $api->repository()->cos->all();
  323. /**
  324. *
  325. * load configurable options coses
  326. */
  327. $supportedCos = $productManager->getSettingCos();
  328. /**
  329. *
  330. * add COS to array
  331. */
  332. $configoptions = $this->getFilteredCosConfigurableOptions();
  333. foreach($classOfServices as $cos)
  334. {
  335. /**
  336. *
  337. *
  338. * skip COS which is not used in configurable options
  339. */
  340. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  341. {
  342. continue;
  343. }
  344. /**
  345. *
  346. * skip if class of services doesnt exists in config option list
  347. */
  348. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  349. {
  350. continue;
  351. }
  352. /**
  353. * skip not purchased as CO
  354. */
  355. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  356. {
  357. continue;
  358. }
  359. /**
  360. * 1. check if config opts are not available
  361. * 2. skip if quantity === 0
  362. */
  363. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  364. {
  365. continue;
  366. }
  367. /* @var $cos ClassOfService*/
  368. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  369. }
  370. }
  371. }
  372. /**
  373. * @return bool|mixed
  374. */
  375. protected function getFilteredCosConfigurableOptions()
  376. {
  377. $configoptions = $this->getWhmcsParamByKey('configoptions');
  378. foreach($configoptions as $key => $value)
  379. {
  380. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  381. {
  382. unset($configoptions[$key]);
  383. }
  384. }
  385. return $configoptions;
  386. }
  387. }