EditRessourceDataProvider.php 9.2 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. $this->availableValues['manager'][$ressource[0]['manager']['id']] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  90. foreach($users as $user) {
  91. if($user['isEnabled']){
  92. $this->availableValues['manager'][ $user['id']] = $user['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  93. }
  94. }
  95. $lang = di('lang');
  96. $this->data['id'] = $ressource[0]['id'];
  97. $this->data['name'] = $ressource[0]['name'];
  98. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  99. $this->data['status'] = $ressource[0]['isEnabled'] == true ? Kerio::ACC_STATUS_ACTIVE : Kerio::ACC_STATUS_CLOSED;
  100. $this->data['type'] = $ressource[0]['type'] === 'Room' ? Kerio::RES_TYPE_LOCATION : Kerio::RES_TYPE_EQUIPMENT;
  101. $this->data['description'] = $ressource[0]['description'];
  102. $this->data['manager'] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  103. $this->availableValues['status'] = [
  104. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  105. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  106. ];
  107. $this->availableValues['type'] = [
  108. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  109. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  110. ];
  111. }
  112. /**
  113. * @return HtmlDataJsonResponse
  114. */
  115. public function update()
  116. {
  117. $fieldToProtection = [
  118. 'name',
  119. 'status',
  120. 'type',
  121. 'description',
  122. 'managert'
  123. ];
  124. logModuleCall(
  125. 'kerioEmail',
  126. __FUNCTION__,
  127. $this->formData,
  128. 'Debug Error',
  129. $this->actionElementId
  130. );
  131. foreach ($this->formData as $field => &$value)
  132. {
  133. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  134. }
  135. $attr = array(
  136. 'description' => $this->formData['description'],
  137. 'type' => $this->formData['type'],
  138. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  139. 'manager' => array(
  140. 'id' => $this->formData['manager'],
  141. 'type' => 'UserPrincipal'
  142. )
  143. );
  144. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  145. try {
  146. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  147. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  148. } catch (KerioApiException $error) {
  149. logModuleCall(
  150. 'kerioEmail',
  151. __FUNCTION__,
  152. $error,
  153. 'Debug Error',
  154. $error->getMessage()
  155. );
  156. return ['error' => $error->getMessage()];
  157. }
  158. try {
  159. $result = $api->updateResouce($attr,$this->formData['id']);
  160. } catch (KerioApiException $error) {
  161. logModuleCall(
  162. 'kerioEmail',
  163. __FUNCTION__,
  164. $error,
  165. 'Debug Error',
  166. $error->getMessage()
  167. );
  168. return ['error' => $error->getMessage()];
  169. }
  170. $api->logout();
  171. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  172. }
  173. /**
  174. * @return HtmlDataJsonResponse
  175. */
  176. public function updateStatus()
  177. {
  178. /**
  179. * hosting id
  180. */
  181. $hid = $this->request->get('id');
  182. /**
  183. * product manager allow to check product settings
  184. */
  185. $productManager = new ProductManager();
  186. $productManager->loadByHostingId($hid);
  187. /**
  188. *
  189. * get soap create domain service
  190. */
  191. $service =(new KerioManager())
  192. ->getApiByHosting($hid)
  193. ->soap
  194. ->service()
  195. ->updateRessourceStatus()
  196. ->setProductManager($productManager)
  197. ;
  198. /**
  199. *
  200. * set product manager & form data to service
  201. */
  202. /**
  203. * run service for each id
  204. */
  205. $service->setFormData($this->formData);
  206. $result = $service->run();
  207. if(!$result)
  208. {
  209. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  210. }
  211. /**
  212. * return success
  213. */
  214. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  215. }
  216. /**
  217. * @return HtmlDataJsonResponse
  218. */
  219. public function changePassword()
  220. {
  221. /**
  222. * hosting id
  223. */
  224. $hid = $this->request->get('id');
  225. /**
  226. * product manager allow to check product settings
  227. */
  228. $productManager = new ProductManager();
  229. $productManager->loadByHostingId($hid);
  230. /**
  231. *
  232. * get soap create domain service
  233. */
  234. $service =(new KerioManager())
  235. ->getApiByHosting($hid)
  236. ->soap
  237. ->service()
  238. ->updateRessourcePassword()
  239. ->setProductManager($productManager)
  240. ;
  241. /**
  242. *
  243. * set product manager & form data to service
  244. */
  245. /**
  246. * run service for each id
  247. */
  248. $service->setFormData($this->formData);
  249. $result = $service->run();
  250. if(!$result)
  251. {
  252. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  253. }
  254. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  255. }
  256. }