EditRessourceDataProvider.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateRessource;
  12. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  14. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  15. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  17. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  18. /**
  19. *
  20. * Created by PhpStorm.
  21. * User: ThurData
  22. * Date: 18.09.19
  23. * Time: 09:35
  24. * Class EditRessourceDataProvider
  25. */
  26. class EditRessourceDataProvider extends BaseDataProvider
  27. {
  28. /**
  29. *
  30. */
  31. public function read()
  32. {
  33. $fields = array(
  34. "id",
  35. "name",
  36. "description",
  37. "type",
  38. "isEnabled",
  39. "manager"
  40. );
  41. $cond = array(
  42. "fieldName" => "id",
  43. "comparator" => "Eq",
  44. "value" => $this->actionElementId
  45. );
  46. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  47. try {
  48. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  49. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  50. } catch (KerioApiException $error) {
  51. logModuleCall(
  52. 'kerioEmail',
  53. __FUNCTION__,
  54. $error,
  55. 'Debug Error',
  56. $error->getMessage()
  57. );
  58. return ['error' => $error->getMessage()];
  59. }
  60. try {
  61. $ressource = $api->getResources($fields,$domainID,[ $cond ]);
  62. } catch (KerioApiException $error) {
  63. logModuleCall(
  64. 'kerioEmail',
  65. __FUNCTION__,
  66. $error,
  67. 'Debug Error',
  68. $error->getMessage()
  69. );
  70. return ['error' => $error->getMessage()];
  71. }
  72. $attr = array(
  73. "id",
  74. "loginName",
  75. "isEnabled");
  76. try {
  77. $users = $api->getUsers($attr,$domainID);
  78. } catch (KerioApiException $error) {
  79. logModuleCall(
  80. 'kerioEmail',
  81. __FUNCTION__,
  82. $error,
  83. 'Debug Error',
  84. $error->getMessage()
  85. );
  86. return ['error' => $error->getMessage()];
  87. }
  88. $api->logout();
  89. foreach($users as $user) {
  90. if($user['isEnabled']){
  91. $this->availableValues['manager'][ $user['id']] = $user['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  92. }
  93. }
  94. $lang = di('lang');
  95. $this->data['id'] = $ressource[0]['id'];
  96. $this->data['name'] = $ressource[0]['name'];
  97. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  98. $this->data['status'] = $ressource[0]['isEnabled'] == true ? Kerio::ACC_STATUS_ACTIVE : Kerio::ACC_STATUS_CLOSED;
  99. $this->data['type'] = $ressource[0]['type'] === 'Room' ? di('lang')->absoluteT('kerio','ressource','type','location') : di('lang')->absoluteT('kerio','ressource','type','equipment');;
  100. $this->data['description'] = $ressource[0]['description'];
  101. $this->data['manager'] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  102. $this->availableValues['status'] = [
  103. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  104. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  105. ];
  106. $this->availableValues['type'] = [
  107. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  108. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  109. ];
  110. }
  111. /**
  112. * @return HtmlDataJsonResponse
  113. */
  114. public function update()
  115. {
  116. $fieldToProtection = [
  117. 'name',
  118. 'status',
  119. 'type',
  120. 'description',
  121. 'managert'
  122. ];
  123. logModuleCall(
  124. 'kerioEmail',
  125. __FUNCTION__,
  126. $this->formData,
  127. 'Debug Error',
  128. $this->actionElementId
  129. );
  130. foreach ($this->formData as $field => &$value)
  131. {
  132. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  133. }
  134. $attr = array(
  135. 'description' => $this->formData['description'],
  136. 'type' => $this->formData['type'],
  137. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  138. 'manager' => array(
  139. 'id' => $this->formData['manager'],
  140. 'type' => 'UserPrincipal'
  141. )
  142. );
  143. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  144. try {
  145. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  146. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  147. } catch (KerioApiException $error) {
  148. logModuleCall(
  149. 'kerioEmail',
  150. __FUNCTION__,
  151. $error,
  152. 'Debug Error',
  153. $error->getMessage()
  154. );
  155. return ['error' => $error->getMessage()];
  156. }
  157. try {
  158. $result = $api->updateResouce($attr,$this->formData['id']);
  159. } catch (KerioApiException $error) {
  160. logModuleCall(
  161. 'kerioEmail',
  162. __FUNCTION__,
  163. $error,
  164. 'Debug Error',
  165. $error->getMessage()
  166. );
  167. return ['error' => $error->getMessage()];
  168. }
  169. $api->logout();
  170. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  171. }
  172. /**
  173. * @return HtmlDataJsonResponse
  174. */
  175. public function updateStatus()
  176. {
  177. /**
  178. * hosting id
  179. */
  180. $hid = $this->request->get('id');
  181. /**
  182. * product manager allow to check product settings
  183. */
  184. $productManager = new ProductManager();
  185. $productManager->loadByHostingId($hid);
  186. /**
  187. *
  188. * get soap create domain service
  189. */
  190. $service =(new KerioManager())
  191. ->getApiByHosting($hid)
  192. ->soap
  193. ->service()
  194. ->updateRessourceStatus()
  195. ->setProductManager($productManager)
  196. ;
  197. /**
  198. *
  199. * set product manager & form data to service
  200. */
  201. /**
  202. * run service for each id
  203. */
  204. $service->setFormData($this->formData);
  205. $result = $service->run();
  206. if(!$result)
  207. {
  208. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  209. }
  210. /**
  211. * return success
  212. */
  213. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  214. }
  215. /**
  216. * @return HtmlDataJsonResponse
  217. */
  218. public function changePassword()
  219. {
  220. /**
  221. * hosting id
  222. */
  223. $hid = $this->request->get('id');
  224. /**
  225. * product manager allow to check product settings
  226. */
  227. $productManager = new ProductManager();
  228. $productManager->loadByHostingId($hid);
  229. /**
  230. *
  231. * get soap create domain service
  232. */
  233. $service =(new KerioManager())
  234. ->getApiByHosting($hid)
  235. ->soap
  236. ->service()
  237. ->updateRessourcePassword()
  238. ->setProductManager($productManager)
  239. ;
  240. /**
  241. *
  242. * set product manager & form data to service
  243. */
  244. /**
  245. * run service for each id
  246. */
  247. $service->setFormData($this->formData);
  248. $result = $service->run();
  249. if(!$result)
  250. {
  251. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  252. }
  253. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  254. }
  255. }