AccountDataProvider.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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'] = $this->formData['quota'];
  100. $account['diskSizeLimit']['limit']['unit'] = $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. logModuleCall(
  136. 'kerioEmail',
  137. __FUNCTION__,
  138. $result1,
  139. 'Debug Add Account',
  140. $result2
  141. );
  142. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  143. }
  144. public function updateStatus()
  145. {
  146. }
  147. public function update()
  148. {
  149. /**
  150. * hosting id
  151. */
  152. $hid = $this->request->get('id');
  153. /**
  154. * product manager allow to check product settings
  155. */
  156. $productManager = new ProductManager();
  157. $productManager->loadByHostingId($hid);
  158. /**
  159. * run service for each id
  160. */
  161. foreach($this->request->get('massActions') as $id)
  162. {
  163. $service->setFormData(['status' => $this->formData['status'], 'id' => $id]);
  164. }
  165. /**
  166. * return success
  167. */
  168. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  169. }
  170. /**
  171. *
  172. * read data per cos_name
  173. */
  174. protected function readCosParams()
  175. {
  176. $hid = $this->request->get('id');
  177. /**
  178. *
  179. * load product manager
  180. */
  181. $productManager = new ProductManager();
  182. $productManager->loadByHostingId($hid);
  183. /**
  184. *
  185. * check if class of services should be selectable
  186. */
  187. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  188. {
  189. /**
  190. *
  191. * get soap create domain service
  192. */
  193. $api =(new KerioManager())
  194. ->getApiByHosting($hid)
  195. ->soap;
  196. /**
  197. *
  198. * get cos from API
  199. */
  200. $classOfServices = $api->repository()->cos->all();
  201. /**
  202. *
  203. * load configurable options coses
  204. */
  205. $supportedCos = $productManager->getSettingCos();
  206. /**
  207. *
  208. * add COS to array
  209. */
  210. $configoptions = $this->getFilteredCosConfigurableOptions();
  211. foreach($classOfServices as $cos)
  212. {
  213. /**
  214. *
  215. *
  216. * skip COS which is not used in configurable options
  217. */
  218. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  219. {
  220. continue;
  221. }
  222. /**
  223. * 1. check if config opts are available
  224. * 2. skip if class of services doesnt exists in config option list
  225. */
  226. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  227. {
  228. continue;
  229. }
  230. /**
  231. * 1. check if config opts are available
  232. * 2. skip not purchased as CO
  233. */
  234. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  235. {
  236. continue;
  237. }
  238. /**
  239. * 1. check if config opts are not available
  240. * 2. skip if quantity === 0
  241. */
  242. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  243. {
  244. continue;
  245. }
  246. /* @var $cos ClassOfService*/
  247. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  248. }
  249. return $this;
  250. }
  251. /**
  252. *
  253. * check if class of service is choosen by config opt
  254. */
  255. if($productManager->get('cos_name') === ClassOfServices::KERIO_CONFIG_OPTIONS)
  256. {
  257. $this->data['cosId'] = key($productManager->getSettingCos());
  258. return $this;
  259. }
  260. /**
  261. *
  262. * if cos_name is dedicated (loaded from API)
  263. */
  264. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_KERIO)
  265. {
  266. /**
  267. * if dedicated class of service has been selected
  268. */
  269. $this->data['cosId'] = $productManager->get('cos_name');
  270. return $this;
  271. }
  272. return $this;
  273. }
  274. /**
  275. * @return bool|mixed
  276. */
  277. protected function getFilteredCosConfigurableOptions()
  278. {
  279. $configoptions = $this->getWhmcsParamByKey('configoptions');
  280. foreach($configoptions as $key => $value)
  281. {
  282. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  283. {
  284. unset($configoptions[$key]);
  285. }
  286. }
  287. return $configoptions;
  288. }
  289. }