AccountDataProvider.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /**
  20. *
  21. * Created by PhpStorm.
  22. * User: ThurData
  23. * Date: 10.09.19
  24. * Time: 13:06
  25. * Class AccountDataProvider
  26. */
  27. class AccountDataProvider extends BaseDataProvider
  28. {
  29. public function read()
  30. {
  31. /**
  32. * hosting id
  33. */
  34. $hid = $this->request->get('id');
  35. $hosting = Hosting::where('id', $hid)->first();
  36. $this->data['domain'] = $this->getWhmcsParamByKey('customfields')['maildomain'];
  37. $lang = di('lang');
  38. $this->availableValues['status'] = [
  39. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  40. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  41. ];
  42. $this->availableValues['unit'] = [
  43. 'MegaBytes' => 'MB',
  44. 'GigaBytes' => 'GB',
  45. ];
  46. }
  47. public function create()
  48. {
  49. /**
  50. * hosting id
  51. */
  52. $hid = $this->request->get('id');
  53. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  54. foreach ($this->formData as $field => &$value)
  55. {
  56. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  57. }
  58. /**
  59. * product manager allow to check product settings
  60. */
  61. $productManager = new ProductManager();
  62. $productManager->loadByHostingId($hid);
  63. $maildomain = $this->getWhmcsParamByKey('customfields')['maildomain'];
  64. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  65. try {
  66. $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
  67. $domainID = $api->getDomainId($maildomain);
  68. } catch (KerioApiException $error) {
  69. logModuleCall(
  70. 'kerioEmail',
  71. __FUNCTION__,
  72. $error,
  73. 'Debug Error',
  74. $error->getMessage()
  75. );
  76. return ['error' => $error->getMessage()];
  77. }
  78. if ($domainID === FALSE) {
  79. return "Error: Domain $maildomain not found";
  80. }
  81. try {
  82. $userID = $api->createUser($domainID, $this->formData['username'], $this->formData['password']);
  83. } catch (KerioApiException $error) {
  84. logModuleCall(
  85. 'kerioEmail',
  86. __FUNCTION__,
  87. $error,
  88. 'Debug Error',
  89. $error->getMessage()
  90. );
  91. return ['error' => $error->getMessage()];
  92. }
  93. $account['fullName'] = $this->formData['display_name'];
  94. if ($this->formData['quota'] > 0) {
  95. $account['diskSizeLimit']['isActive'] = TRUE;
  96. $account['diskSizeLimit']['limit']['value'] = $this->formData['quota'];
  97. $account['diskSizeLimit']['limit']['unit'] = $this->formData['unit'];
  98. }
  99. $fields['firstName'] = $this->formData['firstname'];
  100. $fields['surName'] = $this->formData['lastname'];
  101. $fields['commonName'] = $this->formData['display_name'];
  102. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  103. $fields['titleBefore'] = $this->formData['title'];
  104. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  105. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  106. $fields['departmentName'] = $this->formData['department'];
  107. $fields['profession'] = $this->formData['profession'];
  108. logModuleCall(
  109. 'kerioEmail',
  110. __FUNCTION__,
  111. $domainID,
  112. 'Debug Add Account',
  113. $userID
  114. );
  115. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  116. }
  117. public function updateStatus()
  118. {
  119. }
  120. public function update()
  121. {
  122. /**
  123. * hosting id
  124. */
  125. $hid = $this->request->get('id');
  126. /**
  127. * product manager allow to check product settings
  128. */
  129. $productManager = new ProductManager();
  130. $productManager->loadByHostingId($hid);
  131. /**
  132. * run service for each id
  133. */
  134. foreach($this->request->get('massActions') as $id)
  135. {
  136. $service->setFormData(['status' => $this->formData['status'], 'id' => $id]);
  137. }
  138. /**
  139. * return success
  140. */
  141. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  142. }
  143. /**
  144. *
  145. * read data per cos_name
  146. */
  147. protected function readCosParams()
  148. {
  149. $hid = $this->request->get('id');
  150. /**
  151. *
  152. * load product manager
  153. */
  154. $productManager = new ProductManager();
  155. $productManager->loadByHostingId($hid);
  156. /**
  157. *
  158. * check if class of services should be selectable
  159. */
  160. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  161. {
  162. /**
  163. *
  164. * get soap create domain service
  165. */
  166. $api =(new KerioManager())
  167. ->getApiByHosting($hid)
  168. ->soap;
  169. /**
  170. *
  171. * get cos from API
  172. */
  173. $classOfServices = $api->repository()->cos->all();
  174. /**
  175. *
  176. * load configurable options coses
  177. */
  178. $supportedCos = $productManager->getSettingCos();
  179. /**
  180. *
  181. * add COS to array
  182. */
  183. $configoptions = $this->getFilteredCosConfigurableOptions();
  184. foreach($classOfServices as $cos)
  185. {
  186. /**
  187. *
  188. *
  189. * skip COS which is not used in configurable options
  190. */
  191. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  192. {
  193. continue;
  194. }
  195. /**
  196. * 1. check if config opts are available
  197. * 2. skip if class of services doesnt exists in config option list
  198. */
  199. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  200. {
  201. continue;
  202. }
  203. /**
  204. * 1. check if config opts are available
  205. * 2. skip not purchased as CO
  206. */
  207. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  208. {
  209. continue;
  210. }
  211. /**
  212. * 1. check if config opts are not available
  213. * 2. skip if quantity === 0
  214. */
  215. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  216. {
  217. continue;
  218. }
  219. /* @var $cos ClassOfService*/
  220. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  221. }
  222. return $this;
  223. }
  224. /**
  225. *
  226. * check if class of service is choosen by config opt
  227. */
  228. if($productManager->get('cos_name') === ClassOfServices::KERIO_CONFIG_OPTIONS)
  229. {
  230. $this->data['cosId'] = key($productManager->getSettingCos());
  231. return $this;
  232. }
  233. /**
  234. *
  235. * if cos_name is dedicated (loaded from API)
  236. */
  237. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_KERIO)
  238. {
  239. /**
  240. * if dedicated class of service has been selected
  241. */
  242. $this->data['cosId'] = $productManager->get('cos_name');
  243. return $this;
  244. }
  245. return $this;
  246. }
  247. /**
  248. * @return bool|mixed
  249. */
  250. protected function getFilteredCosConfigurableOptions()
  251. {
  252. $configoptions = $this->getWhmcsParamByKey('configoptions');
  253. foreach($configoptions as $key => $value)
  254. {
  255. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  256. {
  257. unset($configoptions[$key]);
  258. }
  259. }
  260. return $configoptions;
  261. }
  262. }