AccountDataProvider.php 10 KB

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