EditAccountDataProvider.php 14 KB

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