EditAccountDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. logModuleCall(
  51. 'zimbraEmail',
  52. __FUNCTION__,
  53. $result,
  54. 'Debug Account',
  55. ''
  56. );
  57. $mailBoxParams = explode('@', $result->getName());
  58. $this->data['id'] = $result->getId();
  59. $this->data['username'] = $mailBoxParams[0];
  60. $this->data['domain'] = $mailBoxParams[1];
  61. $this->data['firstname'] = $result->getDataResourceA(Account::ATTR_FIRSTNAME);
  62. $this->data['lastname'] = $result->getDataResourceA(Account::ATTR_LASTNAME);
  63. $this->data['display_name'] = $result->getDataResourceA(Account::ATTR_DISPLAY_NAME);
  64. $this->data['status'] = $result->getDataResourceA(Account::ATTR_ACCOUNT_STATUS);
  65. $this->data['company'] = $result->getDataResourceA(Account::ATTR_COMPANY);
  66. $this->data['title'] = $result->getDataResourceA(Account::ATTR_PROF_TITLE);
  67. $this->data['phone'] = $result->getDataResourceA(Account::ATTR_PHONE);
  68. $this->data['home_phone'] = $result->getDataResourceA(Account::ATTR_HOME_PHONE);
  69. $this->data['mobile_phone'] = $result->getDataResourceA(Account::ATTR_MOBILE_PHONE);
  70. $this->data['fax'] = $result->getDataResourceA(Account::ATTR_FAX);
  71. $this->data['pager'] = $result->getDataResourceA(Account::ATTR_PAGER);
  72. $this->data['country'] = $result->getDataResourceA(Account::ATTR_COUNTRY);
  73. $this->data['city'] = $result->getDataResourceA(Account::ATTR_CITY);
  74. $this->data['street'] = $result->getDataResourceA(Account::ATTR_STREET);
  75. $this->data['post_code'] = $result->getDataResourceA(Account::ATTR_POSTAL_CODE);
  76. $this->data['currentCosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  77. $this->data['cosId'] = $result->getDataResourceA(Account::ATTR_CLASS_OF_SERVICE_ID);
  78. $this->data['state'] = $result->getDataResourceA(Account::ATTR_STATE);
  79. $lang = di('lang');
  80. $this->availableValues['status'] = [
  81. Zimbra::ACC_STATUS_ACTIVE => $lang->absoluteT('zimbra','account','status','active'),
  82. Zimbra::ACC_STATUS_LOCKED => $lang->absoluteT('zimbra','account','status','locked'),
  83. Zimbra::ACC_STATUS_MAINTENANCE => $lang->absoluteT('zimbra','account','status','maintenance'),
  84. Zimbra::ACC_STATUS_CLOSED => $lang->absoluteT('zimbra','account','status','closed'),
  85. Zimbra::ACC_STATUS_LOCKOUT => $lang->absoluteT('zimbra','account','status','lockout'),
  86. Zimbra::ACC_STATUS_PENDING => $lang->absoluteT('zimbra','account','status','pending')
  87. ];
  88. $this->readCosParams();
  89. }
  90. /**
  91. * @return HtmlDataJsonResponse
  92. */
  93. public function update()
  94. {
  95. /**
  96. * hosting id
  97. */
  98. $hid = $this->request->get('id');
  99. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  100. foreach ($this->formData as $field => &$value)
  101. {
  102. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  103. }
  104. /**
  105. * product manager allow to check product settings
  106. */
  107. $productManager = new ProductManager();
  108. $productManager->loadByHostingId($hid);
  109. /**
  110. *
  111. * get soap create domain service
  112. */
  113. $service =(new ZimbraManager())
  114. ->getApiByHosting($hid)
  115. ->soap
  116. ->service()
  117. ->updateAccount($productManager->get('cos_name'));
  118. /**
  119. *
  120. * set product manager & form data to service
  121. */
  122. $service
  123. ->setProductManager($productManager)
  124. ->setFormData($this->formData);
  125. /**
  126. * run service
  127. */
  128. $result = $service->run();
  129. /**
  130. * return success or error response
  131. */
  132. if(!$result)
  133. {
  134. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  135. }
  136. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
  137. }
  138. /**
  139. * @return HtmlDataJsonResponse
  140. */
  141. public function updateStatus()
  142. {
  143. /**
  144. * hosting id
  145. */
  146. $hid = $this->request->get('id');
  147. /**
  148. * product manager allow to check product settings
  149. */
  150. $productManager = new ProductManager();
  151. $productManager->loadByHostingId($hid);
  152. /**
  153. *
  154. * get soap create domain service
  155. */
  156. $service =(new ZimbraManager())
  157. ->getApiByHosting($hid)
  158. ->soap
  159. ->service()
  160. ->updateAccountStatus()
  161. ->setProductManager($productManager)
  162. ;
  163. /**
  164. *
  165. * set product manager & form data to service
  166. */
  167. /**
  168. * run service for each id
  169. */
  170. $service->setFormData($this->formData);
  171. $result = $service->run();
  172. if(!$result)
  173. {
  174. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  175. }
  176. /**
  177. * return success
  178. */
  179. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
  180. }
  181. /**
  182. * @return HtmlDataJsonResponse
  183. */
  184. public function changePassword()
  185. {
  186. /**
  187. * hosting id
  188. */
  189. $hid = $this->request->get('id');
  190. /**
  191. * product manager allow to check product settings
  192. */
  193. $productManager = new ProductManager();
  194. $productManager->loadByHostingId($hid);
  195. /**
  196. *
  197. * get soap create domain service
  198. */
  199. $service =(new ZimbraManager())
  200. ->getApiByHosting($hid)
  201. ->soap
  202. ->service()
  203. ->updateAccountPassword()
  204. ->setProductManager($productManager)
  205. ;
  206. /**
  207. *
  208. * set product manager & form data to service
  209. */
  210. /**
  211. * run service for each id
  212. */
  213. $service->setFormData($this->formData);
  214. $result = $service->run();
  215. if(!$result)
  216. {
  217. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  218. }
  219. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  220. }
  221. /**
  222. *
  223. */
  224. public function readCosParams()
  225. {
  226. $hid = $this->getRequestValue('id');
  227. /**
  228. * product manager allow to check product settings
  229. */
  230. $productManager = new ProductManager();
  231. $productManager->loadByHostingId($hid);
  232. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  233. {
  234. /**
  235. *
  236. * get soap create domain service
  237. */
  238. $api =(new ZimbraManager())
  239. ->getApiByHosting($hid)
  240. ->soap;
  241. /**
  242. *
  243. * get cos from API
  244. */
  245. $classOfServices = $api->repository()->cos->all();
  246. /**
  247. *
  248. * load configurable options coses
  249. */
  250. $supportedCos = $productManager->getSettingCos();
  251. /**
  252. *
  253. * add COS to array
  254. */
  255. $configoptions = $this->getFilteredCosConfigurableOptions();
  256. foreach($classOfServices as $cos)
  257. {
  258. /**
  259. *
  260. *
  261. * skip COS which is not used in configurable options
  262. */
  263. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  264. {
  265. continue;
  266. }
  267. /**
  268. *
  269. * skip if class of services doesnt exists in config option list
  270. */
  271. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  272. {
  273. continue;
  274. }
  275. /**
  276. * skip not purchased as CO
  277. */
  278. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  279. {
  280. continue;
  281. }
  282. /**
  283. * 1. check if config opts are not available
  284. * 2. skip if quantity === 0
  285. */
  286. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  287. {
  288. continue;
  289. }
  290. /* @var $cos ClassOfService*/
  291. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  292. }
  293. }
  294. }
  295. /**
  296. * @return bool|mixed
  297. */
  298. protected function getFilteredCosConfigurableOptions()
  299. {
  300. $configoptions = $this->getWhmcsParamByKey('configoptions');
  301. foreach($configoptions as $key => $value)
  302. {
  303. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  304. {
  305. unset($configoptions[$key]);
  306. }
  307. }
  308. return $configoptions;
  309. }
  310. }