EditAccountDataProvider.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  5. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  6. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  7. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  8. /**
  9. *
  10. * Created by PhpStorm.
  11. * User: ThurData
  12. * Date: 18.09.19
  13. * Time: 09:35
  14. * Class EditAccountDataProvider
  15. */
  16. class EditAccountDataProvider extends BaseDataProvider
  17. {
  18. /**
  19. *
  20. */
  21. public function read()
  22. {
  23. $domain = $this->getWhmcsParamByKey('domain');
  24. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  25. $fields =array(
  26. 'id',
  27. 'companyContactId',
  28. 'loginName',
  29. 'fullName',
  30. 'description',
  31. 'isEnabled',
  32. 'emailAddresses',
  33. 'diskSizeLimit',
  34. );
  35. $cond = array(
  36. array(
  37. "fieldName" => "id",
  38. "comparator" => "Eq",
  39. "value" => $this->actionElementId
  40. )
  41. );
  42. try {
  43. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  44. } catch (KerioApiException $error) {
  45. logModuleCall(
  46. 'kerioEmail',
  47. __FUNCTION__,
  48. $error,
  49. 'Debug Error',
  50. $error->getMessage()
  51. );
  52. return ['error' => $error->getMessage()];
  53. }
  54. try {
  55. $domainID = $api->getDomainId($domain);
  56. } catch (KerioApiException $error) {
  57. logModuleCall(
  58. 'kerioEmail',
  59. __FUNCTION__,
  60. $error,
  61. 'Debug Error',
  62. $error->getMessage()
  63. );
  64. return ['error' => $error->getMessage()];
  65. }
  66. try {
  67. $account = $api->getUsers($fields,$domainID,$cond);
  68. } catch (KerioApiException $error) {
  69. logModuleCall(
  70. 'kerioEmail',
  71. __FUNCTION__,
  72. $error,
  73. 'Debug Error',
  74. $error->getMessage()
  75. );
  76. return ['error' => $error->getMessage()];
  77. }
  78. try {
  79. $address = $api->getAddress($this->actionElementId);
  80. } catch (KerioApiException $error) {
  81. logModuleCall(
  82. 'kerioEmail',
  83. __FUNCTION__,
  84. $error,
  85. 'Debug Error',
  86. $error->getMessage()
  87. );
  88. return ['error' => $error->getMessage()];
  89. }
  90. $api->logout();
  91. $this->data['id'] = $account[0]['id'];
  92. $this->data['username'] = $account[0]['loginName'];
  93. $this->data['domain'] = $domain;
  94. $this->data['firstname'] = $address['contacts'][0]['firstName'];
  95. $this->data['lastname'] = $address['contacts'][0]['surName'];
  96. $this->data['display_name'] = $address['contacts'][0]['commonName'];
  97. $this->data['status'] = $account['isEnabled'];
  98. $this->data['title'] = $address['contacts'][0]['titleBefore'];
  99. $this->data['profession'] = $address['contacts'][0]['profession'];
  100. $this->data['work_phone'] = $address['contacts'][0]['phoneNumberWorkVoice'];
  101. $this->data['mobile_phone'] = $address['contacts'][0]['phoneNumberMobile'];
  102. $this->data['department'] = $address['contacts'][0]['departmentName'];
  103. $this->data['office'] = $address['contacts'][0]['postalAddressWork']['extendedAddress'];
  104. if ($account[0]['diskSizeLimit']['isActive'] === TRUE) {
  105. $this->data['quota'] = $account[0]['diskSizeLimit']['limit']['value'];
  106. $this->data['unit'] = $account[0]['diskSizeLimit']['limit']['units'];
  107. } else {
  108. $this->data['quota'] = 0;
  109. }
  110. $lang = di('lang');
  111. $this->availableValues['status'] = [
  112. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  113. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed')
  114. ];
  115. $this->availableValues['unit'] = [
  116. 'MegaBytes' => 'MB',
  117. 'GigaBytes' => 'GB',
  118. ];
  119. }
  120. /**
  121. * @return HtmlDataJsonResponse
  122. */
  123. public function update()
  124. {
  125. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  126. foreach ($this->formData as $field => &$value)
  127. {
  128. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  129. }
  130. $account['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  131. if ($this->formData['quota'] > 0) {
  132. $account['diskSizeLimit']['isActive'] = TRUE;
  133. } else {
  134. $account['diskSizeLimit']['isActive'] = FALSE;
  135. }
  136. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  137. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  138. logModuleCall(
  139. 'kerioEmail',
  140. __FUNCTION__,
  141. $this->formdata,
  142. 'Debug Formdata',
  143. ''
  144. );
  145. $fields['firstName'] = $this->formData['firstname'];
  146. $fields['surName'] = $this->formData['lastname'];
  147. $fields['commonName'] = $this->formData['display_name'];
  148. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  149. $fields['titleBefore'] = $this->formData['title'];
  150. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  151. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  152. $fields['departmentName'] = $this->formData['department'];
  153. $fields['profession'] = $this->formData['profession'];
  154. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  155. try {
  156. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  157. } catch (KerioApiException $error) {
  158. logModuleCall(
  159. 'kerioEmail',
  160. __FUNCTION__,
  161. $error,
  162. 'Debug Error',
  163. $error->getMessage()
  164. );
  165. return ['error' => $error->getMessage()];
  166. }
  167. try {
  168. $result = $api->modifyUser($this->formData['id'], $account);
  169. } catch (KerioApiException $error) {
  170. logModuleCall(
  171. 'kerioEmail',
  172. __FUNCTION__,
  173. $error,
  174. 'Debug Error',
  175. $error->getMessage()
  176. );
  177. return ['error' => $error->getMessage()];
  178. }
  179. try {
  180. $result = $api->setAddress($this->formData['id'], $fields);
  181. } catch (KerioApiException $error) {
  182. logModuleCall(
  183. 'kerioEmail',
  184. __FUNCTION__,
  185. $error,
  186. 'Debug Error',
  187. $error->getMessage()
  188. );
  189. return ['error' => $error->getMessage()];
  190. }
  191. $api->logout();
  192. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  193. }
  194. /**
  195. * @return HtmlDataJsonResponse
  196. */
  197. public function updateStatus()
  198. {
  199. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  200. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  201. try {
  202. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  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. try {
  214. $result = $api->modifyUser($this->formData['id'], $status);
  215. } catch (KerioApiException $error) {
  216. logModuleCall(
  217. 'kerioEmail',
  218. __FUNCTION__,
  219. $error,
  220. 'Debug Error',
  221. $error->getMessage()
  222. );
  223. return ['error' => $error->getMessage()];
  224. }
  225. $api->logout();
  226. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  227. }
  228. /**
  229. * @return HtmlDataJsonResponse
  230. */
  231. public function changePassword()
  232. {
  233. $fields['password'] = $this->formData['password'];
  234. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  235. try {
  236. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  237. } catch (KerioApiException $error) {
  238. logModuleCall(
  239. 'kerioEmail',
  240. __FUNCTION__,
  241. $error,
  242. 'Debug Error',
  243. $error->getMessage()
  244. );
  245. return ['error' => $error->getMessage()];
  246. }
  247. try {
  248. $result = $api->modifyUser($this->formData['id'], $fields);
  249. } catch (KerioApiException $error) {
  250. logModuleCall(
  251. 'kerioEmail',
  252. __FUNCTION__,
  253. $error,
  254. 'Debug Error',
  255. $error->getMessage()
  256. );
  257. return ['error' => $error->getMessage()];
  258. }
  259. $api->logout();
  260. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  261. }
  262. }