SuspendAccount.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\Http\Actions;
  3. use ThurData\Servers\KerioEmail\App\Enums\Response;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
  6. use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
  7. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  8. /**
  9. *
  10. * Created by PhpStorm.
  11. * User: ThurData
  12. * Date: 09.09.19
  13. * Time: 15:15
  14. * Class SuspendAccount
  15. */
  16. class SuspendAccount extends AddonController
  17. {
  18. use ExtensionsCheckerTrait;
  19. public function execute($params = null)
  20. {
  21. try{
  22. /**
  23. * check if extensions are installed
  24. */
  25. $this->checkExtensionOrThrowError();
  26. /**
  27. * run kerio service
  28. */
  29. $result = $this->kerioRunService($params);
  30. return $result;
  31. }catch (\Exception $ex)
  32. {
  33. /**
  34. * return some crit error
  35. */
  36. return $ex->getMessage();
  37. }
  38. }
  39. /**
  40. * @param null $params
  41. * @return mixed|string
  42. */
  43. protected function kerioRunService($params = null)
  44. {
  45. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  46. try {
  47. $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
  48. $domainID = $api->getDomainId($params['domain']);
  49. } catch (KerioApiException $error) {
  50. logModuleCall(
  51. 'kerioEmail',
  52. __FUNCTION__,
  53. $error,
  54. 'Debug Error',
  55. $error->getMessage()
  56. );
  57. return ['error' => $error->getMessage()];
  58. }
  59. if ($domainID === FALSE) {
  60. return "Error: Domain $domain not found";
  61. }
  62. try {
  63. $users = $api->getUsers(['id', 'loginName'], $domainID);
  64. } catch (KerioApiException $error) {
  65. logModuleCall(
  66. 'kerioEmail',
  67. __FUNCTION__,
  68. $error,
  69. 'Debug Error',
  70. $error->getMessage()
  71. );
  72. return ['error' => $error->getMessage()];
  73. }
  74. foreach($users as $user) {
  75. try {
  76. $api->modifyUser($user['id'], ['isEnabled' => FALSE]);
  77. } catch (KerioApiException $error) {
  78. logModuleCall(
  79. 'kerioEmail',
  80. __FUNCTION__,
  81. $error,
  82. 'Debug Error',
  83. $error->getMessage()
  84. );
  85. return ['error' => $error->getMessage()];
  86. }
  87. }
  88. $api->logout();
  89. /**
  90. * return success response
  91. */
  92. return Response::SUCCESS;
  93. }
  94. }