EditAccountDataProvider.php 14 KB

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