AccountDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /**
  185. * return success
  186. */
  187. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  188. }
  189. /**
  190. *
  191. * read data per cos_name
  192. */
  193. protected function readCosParams()
  194. {
  195. $hid = $this->request->get('id');
  196. /**
  197. *
  198. * load product manager
  199. */
  200. $productManager = new ProductManager();
  201. $productManager->loadByHostingId($hid);
  202. /**
  203. *
  204. * check if class of services should be selectable
  205. */
  206. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  207. {
  208. /**
  209. *
  210. * get soap create domain service
  211. */
  212. $api =(new KerioManager())
  213. ->getApiByHosting($hid)
  214. ->soap;
  215. /**
  216. *
  217. * get cos from API
  218. */
  219. $classOfServices = $api->repository()->cos->all();
  220. /**
  221. *
  222. * load configurable options coses
  223. */
  224. $supportedCos = $productManager->getSettingCos();
  225. /**
  226. *
  227. * add COS to array
  228. */
  229. $configoptions = $this->getFilteredCosConfigurableOptions();
  230. foreach($classOfServices as $cos)
  231. {
  232. /**
  233. *
  234. *
  235. * skip COS which is not used in configurable options
  236. */
  237. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  238. {
  239. continue;
  240. }
  241. /**
  242. * 1. check if config opts are available
  243. * 2. skip if class of services doesnt exists in config option list
  244. */
  245. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  246. {
  247. continue;
  248. }
  249. /**
  250. * 1. check if config opts are available
  251. * 2. skip not purchased as CO
  252. */
  253. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  254. {
  255. continue;
  256. }
  257. /**
  258. * 1. check if config opts are not available
  259. * 2. skip if quantity === 0
  260. */
  261. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  262. {
  263. continue;
  264. }
  265. /* @var $cos ClassOfService*/
  266. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  267. }
  268. return $this;
  269. }
  270. /**
  271. *
  272. * check if class of service is choosen by config opt
  273. */
  274. if($productManager->get('cos_name') === ClassOfServices::KERIO_CONFIG_OPTIONS)
  275. {
  276. $this->data['cosId'] = key($productManager->getSettingCos());
  277. return $this;
  278. }
  279. /**
  280. *
  281. * if cos_name is dedicated (loaded from API)
  282. */
  283. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_KERIO)
  284. {
  285. /**
  286. * if dedicated class of service has been selected
  287. */
  288. $this->data['cosId'] = $productManager->get('cos_name');
  289. return $this;
  290. }
  291. return $this;
  292. }
  293. /**
  294. * @return bool|mixed
  295. */
  296. protected function getFilteredCosConfigurableOptions()
  297. {
  298. $configoptions = $this->getWhmcsParamByKey('configoptions');
  299. foreach($configoptions as $key => $value)
  300. {
  301. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  302. {
  303. unset($configoptions[$key]);
  304. }
  305. }
  306. return $configoptions;
  307. }
  308. }