AccountDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  4. use ThurData\Servers\KerioEmail\App\Enums\Size;
  5. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  6. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  7. use ThurData\Servers\KerioEmail\App\Http\Admin\ProductConfiguration;
  8. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\ServiceFactory;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  12. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  13. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  14. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  15. use ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  17. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  18. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  19. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  20. /**
  21. *
  22. * Created by PhpStorm.
  23. * User: ThurData
  24. * Date: 10.09.19
  25. * Time: 13:06
  26. * Class AccountDataProvider
  27. */
  28. class AccountDataProvider extends BaseDataProvider
  29. {
  30. public function read()
  31. {
  32. /**
  33. * hosting id
  34. */
  35. $hid = $this->request->get('id');
  36. $hosting = Hosting::where('id', $hid)->first();
  37. $this->data['domain'] = $this->getWhmcsParamByKey('customfields')['maildomain'];
  38. $this->data['quota'] = 0;
  39. $lang = di('lang');
  40. $this->availableValues['status'] = [
  41. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  42. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  43. ];
  44. $this->availableValues['unit'] = [
  45. 'MegaBytes' => 'MB',
  46. 'GigaBytes' => 'GB',
  47. ];
  48. }
  49. public function create()
  50. {
  51. /**
  52. * hosting id
  53. */
  54. $hid = $this->request->get('id');
  55. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  56. foreach ($this->formData as $field => &$value)
  57. {
  58. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  59. }
  60. /**
  61. * product manager allow to check product settings
  62. */
  63. $productManager = new ProductManager();
  64. $productManager->loadByHostingId($hid);
  65. $maildomain = $this->getWhmcsParamByKey('customfields')['maildomain'];
  66. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  67. try {
  68. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  69. $domainID = $api->getDomainId($maildomain);
  70. } catch (KerioApiException $error) {
  71. logModuleCall(
  72. 'kerioEmail',
  73. __FUNCTION__,
  74. $error,
  75. 'Debug Error',
  76. $error->getMessage()
  77. );
  78. return ['error' => $error->getMessage()];
  79. }
  80. if ($domainID === FALSE) {
  81. return "Error: Domain $maildomain not found";
  82. }
  83. $active = $this->formData['status'] === 'active' ? TRUE : FALSE;
  84. try {
  85. $userID = $api->createUser($domainID, $this->formData['username'], $this->formData['password'], $active)['result'][0]['id'];
  86. } catch (KerioApiException $error) {
  87. logModuleCall(
  88. 'kerioEmail',
  89. __FUNCTION__,
  90. $error,
  91. 'Debug Error',
  92. $error->getMessage()
  93. );
  94. return ['error' => $error->getMessage()];
  95. }
  96. $account['fullName'] = $this->formData['display_name'];
  97. if ($this->formData['quota'] > 0) {
  98. $account['diskSizeLimit']['isActive'] = TRUE;
  99. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  100. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  101. }
  102. try {
  103. $result1 = $api->modifyUser($userID, $account);
  104. } catch (KerioApiException $error) {
  105. logModuleCall(
  106. 'kerioEmail',
  107. __FUNCTION__,
  108. $error,
  109. 'Debug Error',
  110. $error->getMessage()
  111. );
  112. return ['error' => $error->getMessage()];
  113. }
  114. $fields['firstName'] = $this->formData['firstname'];
  115. $fields['surName'] = $this->formData['lastname'];
  116. $fields['commonName'] = $this->formData['display_name'];
  117. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  118. $fields['titleBefore'] = $this->formData['title'];
  119. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  120. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  121. $fields['departmentName'] = $this->formData['department'];
  122. $fields['profession'] = $this->formData['profession'];
  123. try {
  124. $result2 = $api->setAddress($userID, $fields);
  125. } catch (KerioApiException $error) {
  126. logModuleCall(
  127. 'kerioEmail',
  128. __FUNCTION__,
  129. $error,
  130. 'Debug Error',
  131. $error->getMessage()
  132. );
  133. return ['error' => $error->getMessage()];
  134. }
  135. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  136. }
  137. public function updateStatus()
  138. {
  139. }
  140. public function update()
  141. {
  142. /**
  143. * hosting id
  144. */
  145. $hid = $this->request->get('id');
  146. /**
  147. * product manager allow to check product settings
  148. */
  149. $productManager = new ProductManager();
  150. $productManager->loadByHostingId($hid);
  151. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  152. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  153. try {
  154. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  155. } catch (KerioApiException $error) {
  156. logModuleCall(
  157. 'kerioEmail',
  158. __FUNCTION__,
  159. $error,
  160. 'Debug Error',
  161. $error->getMessage()
  162. );
  163. return ['error' => $error->getMessage()];
  164. }
  165. /**
  166. * run service for each id
  167. */
  168. foreach($this->request->get('massActions') as $id)
  169. {
  170. try {
  171. $result = $api->modifyUser($id, $status);
  172. } catch (KerioApiException $error) {
  173. logModuleCall(
  174. 'kerioEmail',
  175. __FUNCTION__,
  176. $error,
  177. 'Debug Error',
  178. $error->getMessage()
  179. );
  180. return ['error' => $error->getMessage()];
  181. }
  182. }
  183. $api->logout();
  184. logModuleCall(
  185. 'kerioEmail',
  186. __FUNCTION__,
  187. $this->request->get('massActions'),
  188. 'Debug Data',
  189. $this
  190. );
  191. /**
  192. * return success
  193. */
  194. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  195. }
  196. /**
  197. *
  198. * read data per cos_name
  199. */
  200. protected function readCosParams()
  201. {
  202. $hid = $this->request->get('id');
  203. /**
  204. *
  205. * load product manager
  206. */
  207. $productManager = new ProductManager();
  208. $productManager->loadByHostingId($hid);
  209. /**
  210. *
  211. * check if class of services should be selectable
  212. */
  213. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  214. {
  215. /**
  216. *
  217. * get soap create domain service
  218. */
  219. $api =(new KerioManager())
  220. ->getApiByHosting($hid)
  221. ->soap;
  222. /**
  223. *
  224. * get cos from API
  225. */
  226. $classOfServices = $api->repository()->cos->all();
  227. /**
  228. *
  229. * load configurable options coses
  230. */
  231. $supportedCos = $productManager->getSettingCos();
  232. /**
  233. *
  234. * add COS to array
  235. */
  236. $configoptions = $this->getFilteredCosConfigurableOptions();
  237. foreach($classOfServices as $cos)
  238. {
  239. /**
  240. *
  241. *
  242. * skip COS which is not used in configurable options
  243. */
  244. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  245. {
  246. continue;
  247. }
  248. /**
  249. * 1. check if config opts are available
  250. * 2. skip if class of services doesnt exists in config option list
  251. */
  252. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  253. {
  254. continue;
  255. }
  256. /**
  257. * 1. check if config opts are available
  258. * 2. skip not purchased as CO
  259. */
  260. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  261. {
  262. continue;
  263. }
  264. /**
  265. * 1. check if config opts are not available
  266. * 2. skip if quantity === 0
  267. */
  268. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  269. {
  270. continue;
  271. }
  272. /* @var $cos ClassOfService*/
  273. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  274. }
  275. return $this;
  276. }
  277. /**
  278. *
  279. * check if class of service is choosen by config opt
  280. */
  281. if($productManager->get('cos_name') === ClassOfServices::KERIO_CONFIG_OPTIONS)
  282. {
  283. $this->data['cosId'] = key($productManager->getSettingCos());
  284. return $this;
  285. }
  286. /**
  287. *
  288. * if cos_name is dedicated (loaded from API)
  289. */
  290. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_KERIO)
  291. {
  292. /**
  293. * if dedicated class of service has been selected
  294. */
  295. $this->data['cosId'] = $productManager->get('cos_name');
  296. return $this;
  297. }
  298. return $this;
  299. }
  300. /**
  301. * @return bool|mixed
  302. */
  303. protected function getFilteredCosConfigurableOptions()
  304. {
  305. $configoptions = $this->getWhmcsParamByKey('configoptions');
  306. foreach($configoptions as $key => $value)
  307. {
  308. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  309. {
  310. unset($configoptions[$key]);
  311. }
  312. }
  313. return $configoptions;
  314. }
  315. }