EditAccountDataProvider.php 14 KB

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