EditAccountDataProvider.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\EmailAccount\Providers;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
  5. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
  6. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account;
  7. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;
  8. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
  9. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository\ClassOfServices;
  10. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response;
  11. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Update\UpdateAccount;
  12. use ModulesGarden\Servers\ZimbraEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\di;
  14. use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
  15. use ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  17. /**
  18. *
  19. * Created by PhpStorm.
  20. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  21. * Date: 18.09.19
  22. * Time: 09:35
  23. * Class EditAccountDataProvider
  24. */
  25. class EditAccountDataProvider extends BaseDataProvider
  26. {
  27. /**
  28. *
  29. */
  30. public function read()
  31. {
  32. /**
  33. * hosting id
  34. */
  35. $hid = $this->getRequestValue('id');
  36. /**
  37. * load hosting
  38. */
  39. $hosting = Hosting::where('id', $hid)->first();
  40. /**
  41. * load api
  42. */
  43. $api = (new ZimbraManager())->getApiByServer($hosting->server);
  44. $repository = new Repository($api->soap);
  45. $result = $repository->accounts->getAccountOptionsById($this->actionElementId);
  46. if($result instanceof Response && $result->getLastError())
  47. {
  48. throw new \Exception($result->getLastError());
  49. }
  50. $mailBoxParams = explode('@', $result->getName());
  51. $this->data['id'] = $result->getId();
  52. $this->data['username'] = $mailBoxParams[0];
  53. $this->data['domain'] = $mailBoxParams[1];
  54. $this->data['firstname'] = $result->getDataResourceA(Account::ATTR_FIRSTNAME);
  55. $this->data['lastname'] = $result->getDataResourceA(Account::ATTR_LASTNAME);
  56. $this->data['display_name'] = $result->getDataResourceA(Account::ATTR_DISPLAY_NAME);
  57. $this->data['status'] = $result->getDataResourceA(Account::ATTR_ACCOUNT_STATUS);
  58. $this->data['company'] = $result->getDataResourceA(Account::ATTR_COMPANY);
  59. $this->data['title'] = $result->getDataResourceA(Account::ATTR_PROF_TITLE);
  60. $this->data['phone'] = $result->getDataResourceA(Account::ATTR_PHONE);
  61. $this->data['home_phone'] = $result->getDataResourceA(Account::ATTR_HOME_PHONE);
  62. $this->data['mobile_phone'] = $result->getDataResourceA(Account::ATTR_MOBILE_PHONE);
  63. $this->data['fax'] = $result->getDataResourceA(Account::ATTR_FAX);
  64. $this->data['pager'] = $result->getDataResourceA(Account::ATTR_PAGER);
  65. $this->data['country'] = $result->getDataResourceA(Account::ATTR_COUNTRY);
  66. $this->data['city'] = $result->getDataResourceA(Account::ATTR_CITY);
  67. $this->data['street'] = $result->getDataResourceA(Account::ATTR_STREET);
  68. $this->data['post_code'] = $result->getDataResourceA(Account::ATTR_POSTAL_CODE);
  69. $this->data['currentCosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  70. $this->data['cosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  71. $this->data['state'] = $result->getDataResourceA(Account::ATTR_STATE);
  72. $this->data['zimbraPrefMailForwardingAddress'] = $result->getDataResourceA(Account::ATTR_MAIL_FORWARD);
  73. if (empty($this->data['zimbraPrefMailForwardingAddress'])) {
  74. $this->data['forward'] = 'off';
  75. } else {
  76. $this->data['forward'] = 'on';
  77. }
  78. $lang = di('lang');
  79. $this->availableValues['status'] = [
  80. Zimbra::ACC_STATUS_ACTIVE => $lang->absoluteT('zimbra','account','status','active'),
  81. Zimbra::ACC_STATUS_LOCKED => $lang->absoluteT('zimbra','account','status','locked'),
  82. Zimbra::ACC_STATUS_MAINTENANCE => $lang->absoluteT('zimbra','account','status','maintenance'),
  83. Zimbra::ACC_STATUS_CLOSED => $lang->absoluteT('zimbra','account','status','closed'),
  84. Zimbra::ACC_STATUS_LOCKOUT => $lang->absoluteT('zimbra','account','status','lockout'),
  85. Zimbra::ACC_STATUS_PENDING => $lang->absoluteT('zimbra','account','status','pending')
  86. ];
  87. $this->readCosParams();
  88. }
  89. /**
  90. * @return HtmlDataJsonResponse
  91. */
  92. public function update()
  93. {
  94. /**
  95. * hosting id
  96. */
  97. $hid = $this->request->get('id');
  98. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  99. foreach ($this->formData as $field => &$value)
  100. {
  101. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  102. }
  103. if (empty($this->formData['zimbraPrefMailForwardingAddress'])) {
  104. $this->formData['zimbraPrefMailLocalDeliveryDisabled'] = FALSE;
  105. } else {
  106. $this->formData['zimbraPrefMailLocalDeliveryDisabled'] = TRUE;
  107. }
  108. unset($this->formData['forward']);
  109. /**
  110. * product manager allow to check product settings
  111. */
  112. $productManager = new ProductManager();
  113. $productManager->loadByHostingId($hid);
  114. /**
  115. *
  116. * get soap create domain service
  117. */
  118. $service =(new ZimbraManager())
  119. ->getApiByHosting($hid)
  120. ->soap
  121. ->service()
  122. ->updateAccount($productManager->get('cos_name'));
  123. /**
  124. *
  125. * set product manager & form data to service
  126. */
  127. $service
  128. ->setProductManager($productManager)
  129. ->setFormData($this->formData);
  130. /**
  131. * run service
  132. */
  133. $result = $service->run();
  134. logModuleCall(
  135. 'zimbraEmail',
  136. __FUNCTION__,
  137. $result,
  138. 'Debug Update',
  139. $this->formData
  140. );
  141. /**
  142. * return success or error response
  143. */
  144. if(!$result)
  145. {
  146. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  147. }
  148. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  149. }
  150. /**
  151. * @return HtmlDataJsonResponse
  152. */
  153. public function updateStatus()
  154. {
  155. /**
  156. * hosting id
  157. */
  158. $hid = $this->request->get('id');
  159. /**
  160. * product manager allow to check product settings
  161. */
  162. $productManager = new ProductManager();
  163. $productManager->loadByHostingId($hid);
  164. /**
  165. *
  166. * get soap create domain service
  167. */
  168. $service =(new ZimbraManager())
  169. ->getApiByHosting($hid)
  170. ->soap
  171. ->service()
  172. ->updateAccountStatus()
  173. ->setProductManager($productManager)
  174. ;
  175. /**
  176. *
  177. * set product manager & form data to service
  178. */
  179. /**
  180. * run service for each id
  181. */
  182. $service->setFormData($this->formData);
  183. $result = $service->run();
  184. if(!$result)
  185. {
  186. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  187. }
  188. /**
  189. * return success
  190. */
  191. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  192. }
  193. /**
  194. * @return HtmlDataJsonResponse
  195. */
  196. public function changePassword()
  197. {
  198. /**
  199. * hosting id
  200. */
  201. $hid = $this->request->get('id');
  202. /**
  203. * product manager allow to check product settings
  204. */
  205. $productManager = new ProductManager();
  206. $productManager->loadByHostingId($hid);
  207. /**
  208. *
  209. * get soap create domain service
  210. */
  211. $service =(new ZimbraManager())
  212. ->getApiByHosting($hid)
  213. ->soap
  214. ->service()
  215. ->updateAccountPassword()
  216. ->setProductManager($productManager)
  217. ;
  218. /**
  219. *
  220. * set product manager & form data to service
  221. */
  222. /**
  223. * run service for each id
  224. */
  225. $service->setFormData($this->formData);
  226. $result = $service->run();
  227. if(!$result)
  228. {
  229. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  230. }
  231. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  232. }
  233. /**
  234. *
  235. */
  236. public function readCosParams()
  237. {
  238. $hid = $this->getRequestValue('id');
  239. /**
  240. * product manager allow to check product settings
  241. */
  242. $productManager = new ProductManager();
  243. $productManager->loadByHostingId($hid);
  244. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  245. {
  246. /**
  247. *
  248. * get soap create domain service
  249. */
  250. $api =(new ZimbraManager())
  251. ->getApiByHosting($hid)
  252. ->soap;
  253. /**
  254. *
  255. * get cos from API
  256. */
  257. $classOfServices = $api->repository()->cos->all();
  258. /**
  259. *
  260. * load configurable options coses
  261. */
  262. $supportedCos = $productManager->getSettingCos();
  263. /**
  264. *
  265. * add COS to array
  266. */
  267. $configoptions = $this->getFilteredCosConfigurableOptions();
  268. foreach($classOfServices as $cos)
  269. {
  270. /**
  271. *
  272. *
  273. * skip COS which is not used in configurable options
  274. */
  275. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  276. {
  277. continue;
  278. }
  279. /**
  280. *
  281. * skip if class of services doesnt exists in config option list
  282. */
  283. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  284. {
  285. continue;
  286. }
  287. /**
  288. * skip not purchased as CO
  289. */
  290. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  291. {
  292. continue;
  293. }
  294. /**
  295. * 1. check if config opts are not available
  296. * 2. skip if quantity === 0
  297. */
  298. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  299. {
  300. continue;
  301. }
  302. /* @var $cos ClassOfService*/
  303. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  304. }
  305. }
  306. }
  307. /**
  308. * @return bool|mixed
  309. */
  310. protected function getFilteredCosConfigurableOptions()
  311. {
  312. $configoptions = $this->getWhmcsParamByKey('configoptions');
  313. foreach($configoptions as $key => $value)
  314. {
  315. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  316. {
  317. unset($configoptions[$key]);
  318. }
  319. }
  320. return $configoptions;
  321. }
  322. }