EditAccountDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. logModuleCall(
  93. 'kerioEmail',
  94. __FUNCTION__,
  95. $account,
  96. 'Debug Data',
  97. $this->data
  98. );
  99. $this->data['id'] = $account[0]['id'];
  100. $this->data['username'] = $account[0]['loginName'];
  101. $this->data['domain'] = $domain;
  102. $this->data['firstname'] = $address['contacts'][0]['firstName'];
  103. $this->data['lastname'] = $address['contacts'][0]['surName'];
  104. $this->data['display_name'] = $address['contacts'][0]['commonName'];
  105. $this->data['status'] = $account['isEnabled'];
  106. $this->data['title'] = $address['contacts'][0]['titleBefore'];
  107. $this->data['profession'] = $address['contacts'][0]['profession'];
  108. $this->data['work_phone'] = $address['contacts'][0]['phoneNumberWorkVoice'];
  109. $this->data['mobile_phone'] = $address['contacts'][0]['phoneNumberMobile'];
  110. $this->data['department'] = $address['contacts'][0]['departmentName'];
  111. $this->data['office'] = $address['contacts'][0]['postalAddressWork']['extendedAddress'];
  112. if ($account[0]['diskSizeLimit']['isActive'] === TRUE) {
  113. $this->data['quota'] = $account[0]['diskSizeLimit']['limit']['value'];
  114. $this->data['unit'] = $account[0]['diskSizeLimit']['limit']['units'];
  115. } else {
  116. $this->data['quota'] = 0;
  117. }
  118. if ($account[0]['emailForwarding']['mode'] === 'UForwardYes') {
  119. $this->data['forward'] = 'on';
  120. $this->data['target'] = $account[0]['emailForwarding']['emailAddresses'][0];
  121. }
  122. $lang = di('lang');
  123. if ($this->data['status'] == 1) {
  124. $this->availableValues['status'] = [
  125. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  126. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed')
  127. ];
  128. } else {
  129. $this->availableValues['status'] = [
  130. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  131. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active')
  132. ];
  133. }
  134. $this->availableValues['unit'] = [
  135. 'MegaBytes' => 'MB',
  136. 'GigaBytes' => 'GB',
  137. ];
  138. }
  139. /**
  140. * @return HtmlDataJsonResponse
  141. */
  142. public function update()
  143. {
  144. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  145. foreach ($this->formData as $field => &$value)
  146. {
  147. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  148. }
  149. $account['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  150. if ($this->formData['quota'] > 0) {
  151. $account['diskSizeLimit']['isActive'] = TRUE;
  152. } else {
  153. $account['diskSizeLimit']['isActive'] = FALSE;
  154. }
  155. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  156. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  157. if ( $this->formData['forward'] == 'on') {
  158. $account['emailForwarding']['mode'] = 'UForwardYes';
  159. $account['emailForwarding']['emailAddresses'] = [ $this->formData['target'] ];
  160. } else {
  161. $account['emailForwarding']['mode'] = 'UForwardNone';
  162. $account['emailForwarding']['emailAddresses'] = [];
  163. }
  164. $fields['firstName'] = $this->formData['firstname'];
  165. $fields['surName'] = $this->formData['lastname'];
  166. $fields['commonName'] = $this->formData['display_name'];
  167. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  168. $fields['titleBefore'] = $this->formData['title'];
  169. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  170. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  171. $fields['departmentName'] = $this->formData['department'];
  172. $fields['profession'] = $this->formData['profession'];
  173. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  174. try {
  175. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  176. } catch (KerioApiException $error) {
  177. logModuleCall(
  178. 'kerioEmail',
  179. __FUNCTION__,
  180. $error,
  181. 'Debug Error',
  182. $error->getMessage()
  183. );
  184. return ['error' => $error->getMessage()];
  185. }
  186. try {
  187. $result = $api->modifyUser($this->formData['id'], $account);
  188. } catch (KerioApiException $error) {
  189. logModuleCall(
  190. 'kerioEmail',
  191. __FUNCTION__,
  192. $error,
  193. 'Debug Error',
  194. $error->getMessage()
  195. );
  196. return ['error' => $error->getMessage()];
  197. }
  198. try {
  199. $result = $api->setAddress($this->formData['id'], $fields);
  200. } catch (KerioApiException $error) {
  201. logModuleCall(
  202. 'kerioEmail',
  203. __FUNCTION__,
  204. $error,
  205. 'Debug Error',
  206. $error->getMessage()
  207. );
  208. return ['error' => $error->getMessage()];
  209. }
  210. $api->logout();
  211. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  212. }
  213. /**
  214. * @return HtmlDataJsonResponse
  215. */
  216. public function updateStatus()
  217. {
  218. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  219. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  220. try {
  221. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  222. } catch (KerioApiException $error) {
  223. logModuleCall(
  224. 'kerioEmail',
  225. __FUNCTION__,
  226. $error,
  227. 'Debug Error',
  228. $error->getMessage()
  229. );
  230. return ['error' => $error->getMessage()];
  231. }
  232. try {
  233. $result = $api->modifyUser($this->formData['id'], $status);
  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. $api->logout();
  245. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  246. }
  247. /**
  248. * @return HtmlDataJsonResponse
  249. */
  250. public function changePassword()
  251. {
  252. $fields['password'] = $this->formData['password'];
  253. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  254. try {
  255. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  256. } catch (KerioApiException $error) {
  257. logModuleCall(
  258. 'kerioEmail',
  259. __FUNCTION__,
  260. $error,
  261. 'Debug Error',
  262. $error->getMessage()
  263. );
  264. return ['error' => $error->getMessage()];
  265. }
  266. try {
  267. $result = $api->modifyUser($this->formData['id'], $fields);
  268. } catch (KerioApiException $error) {
  269. logModuleCall(
  270. 'kerioEmail',
  271. __FUNCTION__,
  272. $error,
  273. 'Debug Error',
  274. $error->getMessage()
  275. );
  276. return ['error' => $error->getMessage()];
  277. }
  278. $api->logout();
  279. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  280. }
  281. }