EditAccountDataProvider.php 15 KB

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