EditAccountDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. 'emailForwarding',
  32. 'isEnabled',
  33. 'emailAddresses',
  34. 'diskSizeLimit',
  35. );
  36. $cond = array(
  37. array(
  38. "fieldName" => "id",
  39. "comparator" => "Eq",
  40. "value" => $this->actionElementId
  41. )
  42. );
  43. try {
  44. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  45. } catch (KerioApiException $error) {
  46. logModuleCall(
  47. 'kerioEmail',
  48. __FUNCTION__,
  49. $error,
  50. 'Debug Error',
  51. $error->getMessage()
  52. );
  53. return ['error' => $error->getMessage()];
  54. }
  55. try {
  56. $domainID = $api->getDomainId($domain);
  57. } catch (KerioApiException $error) {
  58. logModuleCall(
  59. 'kerioEmail',
  60. __FUNCTION__,
  61. $error,
  62. 'Debug Error',
  63. $error->getMessage()
  64. );
  65. return ['error' => $error->getMessage()];
  66. }
  67. try {
  68. $account = $api->getUsers($fields,$domainID,$cond);
  69. } catch (KerioApiException $error) {
  70. logModuleCall(
  71. 'kerioEmail',
  72. __FUNCTION__,
  73. $error,
  74. 'Debug Error',
  75. $error->getMessage()
  76. );
  77. return ['error' => $error->getMessage()];
  78. }
  79. try {
  80. $address = $api->getAddress($this->actionElementId);
  81. } catch (KerioApiException $error) {
  82. logModuleCall(
  83. 'kerioEmail',
  84. __FUNCTION__,
  85. $error,
  86. 'Debug Error',
  87. $error->getMessage()
  88. );
  89. return ['error' => $error->getMessage()];
  90. }
  91. $api->logout();
  92. $this->data['id'] = $account[0]['id'];
  93. $this->data['username'] = $account[0]['loginName'];
  94. $this->data['domain'] = $domain;
  95. $this->data['firstname'] = $address['contacts'][0]['firstName'];
  96. $this->data['lastname'] = $address['contacts'][0]['surName'];
  97. $this->data['display_name'] = $address['contacts'][0]['commonName'];
  98. $this->data['status'] = $account['isEnabled'];
  99. $this->data['title'] = $address['contacts'][0]['titleBefore'];
  100. $this->data['profession'] = $address['contacts'][0]['profession'];
  101. $this->data['work_phone'] = $address['contacts'][0]['phoneNumberWorkVoice'];
  102. $this->data['mobile_phone'] = $address['contacts'][0]['phoneNumberMobile'];
  103. $this->data['department'] = $address['contacts'][0]['departmentName'];
  104. $this->data['office'] = $address['contacts'][0]['postalAddressWork']['extendedAddress'];
  105. if ($account[0]['diskSizeLimit']['isActive'] === TRUE) {
  106. $this->data['quota'] = $account[0]['diskSizeLimit']['limit']['value'];
  107. $this->data['unit'] = $account[0]['diskSizeLimit']['limit']['units'];
  108. } else {
  109. $this->data['quota'] = 0;
  110. }
  111. if ($account[0]['emailForwarding']['mode'] === 'UForwardYes') {
  112. $this->data['forward'] = 'on';
  113. $this->data['target'] = $account[0]['emailForwarding']['emailAddresses'][0];
  114. }
  115. $lang = di('lang');
  116. if ($this->data['status'] == 1) {
  117. $this->availableValues['status'] = [
  118. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  119. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed')
  120. ];
  121. } else {
  122. $this->availableValues['status'] = [
  123. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  124. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active')
  125. ];
  126. }
  127. $this->availableValues['unit'] = [
  128. 'MegaBytes' => 'MB',
  129. 'GigaBytes' => 'GB',
  130. ];
  131. }
  132. /**
  133. * @return HtmlDataJsonResponse
  134. */
  135. public function update()
  136. {
  137. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  138. foreach ($this->formData as $field => &$value)
  139. {
  140. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  141. }
  142. $account['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  143. if ($this->formData['quota'] > 0) {
  144. $account['diskSizeLimit']['isActive'] = TRUE;
  145. } else {
  146. $account['diskSizeLimit']['isActive'] = FALSE;
  147. }
  148. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  149. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  150. if ( $this->formData['forward'] == 'on') {
  151. $account['emailForwarding']['mode'] = 'UForwardYes';
  152. $account['emailForwarding']['emailAddresses'] = [ $this->formData['target'] ];
  153. } else {
  154. $account['emailForwarding']['mode'] = 'UForwardNone';
  155. $account['emailForwarding']['emailAddresses'] = [];
  156. }
  157. $fields['firstName'] = $this->formData['firstname'];
  158. $fields['surName'] = $this->formData['lastname'];
  159. $fields['commonName'] = $this->formData['display_name'];
  160. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  161. $fields['titleBefore'] = $this->formData['title'];
  162. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  163. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  164. $fields['departmentName'] = $this->formData['department'];
  165. $fields['profession'] = $this->formData['profession'];
  166. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  167. try {
  168. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  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->modifyUser($this->formData['id'], $account);
  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. try {
  192. $result = $api->setAddress($this->formData['id'], $fields);
  193. } catch (KerioApiException $error) {
  194. logModuleCall(
  195. 'kerioEmail',
  196. __FUNCTION__,
  197. $error,
  198. 'Debug Error',
  199. $error->getMessage()
  200. );
  201. return ['error' => $error->getMessage()];
  202. }
  203. $api->logout();
  204. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  205. }
  206. /**
  207. * @return HtmlDataJsonResponse
  208. */
  209. public function updateStatus()
  210. {
  211. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  212. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  213. try {
  214. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  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. try {
  226. $result = $api->modifyUser($this->formData['id'], $status);
  227. } catch (KerioApiException $error) {
  228. logModuleCall(
  229. 'kerioEmail',
  230. __FUNCTION__,
  231. $error,
  232. 'Debug Error',
  233. $error->getMessage()
  234. );
  235. return ['error' => $error->getMessage()];
  236. }
  237. $api->logout();
  238. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  239. }
  240. /**
  241. * @return HtmlDataJsonResponse
  242. */
  243. public function changePassword()
  244. {
  245. $fields['password'] = $this->formData['password'];
  246. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  247. try {
  248. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  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. try {
  260. $result = $api->modifyUser($this->formData['id'], $fields);
  261. } catch (KerioApiException $error) {
  262. logModuleCall(
  263. 'kerioEmail',
  264. __FUNCTION__,
  265. $error,
  266. 'Debug Error',
  267. $error->getMessage()
  268. );
  269. return ['error' => $error->getMessage()];
  270. }
  271. $api->logout();
  272. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  273. }
  274. }