andre 2 жил өмнө
parent
commit
34246c3bec

+ 33 - 8
api/KerioWhmcs.php

@@ -450,16 +450,27 @@ class KerioWhmcs extends KerioConnectApi {
 			'userIds' => [ $userId ],
 			'pattern' => $attr
 		);
+		$result = $this->sendRequest('Users.set', $params);
+		return $result;
+	}
 
-		logModuleCall(
-			'kerioEmail',
-			__FUNCTION__,
-			$params,
-			'Debug Api',
-			$attr
+	/**
+	 * Delete user.
+	 *
+	 * @param	string	User ID
+	 * @return	array	Result
+	 */
+	function deleteUser($userId) {
+		$params = array(
+			'requests' => array(
+				'userId' => $userId,
+				'mode' => 'DSModeDeactivate',
+				'method' => 'UDeleteFolder',
+				'targetUserId' => '',
+				'removeReferences' => TRUE,
+			),
 		);
-
-		$result = $this->sendRequest('Users.set', $params);
+		$result = $this->sendRequest('Users.remove', $params);
 		return $result;
 	}
 
@@ -495,6 +506,20 @@ class KerioWhmcs extends KerioConnectApi {
 		return $result;
 	}
 	
+	/**
+	 * Delete domain.
+	 *
+	 * @param	string	DomainID
+	 * @return	array	Result
+	 */
+	function deleteDomain($domainID) {
+		$params = array(
+			'domainIds' => array($domainID),
+		);
+		$result = $this->sendRequest('Domains.remove', $params);
+		return $result;
+	}
+	
 	/**
 	 * Get list of IP addresses from a file.
 	 *

+ 59 - 24
app/Http/Actions/TerminateAccount.php

@@ -9,6 +9,7 @@ use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Distri
 use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
 use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
 use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
+use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
 
 /**
  *
@@ -52,33 +53,67 @@ class TerminateAccount extends AddonController
      */
     protected function kerioRunService($params = null)
     {
-        /**
-         *
-         * get soap create domain  service
-         */
-        $service =(new KerioManager())
-            ->getApiByServer($params['serverid'])
-            ->soap
-            ->service()
-            ->deleteDomain()
-            ->setFormData($params)
-        ;
-
-        /**
-         *
-         * set params
-         * run service (Create domain in Kerio API)
-         */
-        $result = $service->run();
+        $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
+        try {
+            $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
+            $domainID = $api->getDomainId($params['domain']);
+        } catch (KerioApiException $error) {
+            logModuleCall(
+                'kerioEmail',
+                __FUNCTION__,
+                $error,
+                'Debug Error',
+                $error->getMessage()
+            );
+            return ['error' => $error->getMessage()];
+        }             
+        if ($domainID === FALSE) {
+            return "Error: Domain $domain not found";
+        }
+        // remove all users from domain
+        try {
+            $users = $api->getUsers(['id', 'loginName'], $domainID);
+        } catch (KerioApiException $error) {
+            logModuleCall(
+                'kerioEmail',
+                __FUNCTION__,
+                $error,
+                'Debug Error',
+                $error->getMessage()
+            );
+            return ['error' => $error->getMessage()];
+        }             
 
-        /**
-         * check service result & return error
-         */
-        if(!$result)
-        {
-            return $service->getError();
+        foreach($users as $user) {
+            try {
+                $api->deleteUser($user['id']);
+            } catch (KerioApiException $error) {
+                logModuleCall(
+                    'kerioEmail',
+                    __FUNCTION__,
+                    $error,
+                    'Debug Error',
+                    $error->getMessage()
+                );
+                return ['error' => $error->getMessage()];
+            }             
         }
 
+        // finally remove domain
+        try {
+            $result = $api->deleteDomain($domainID);
+        } catch (KerioApiException $error) {
+            logModuleCall(
+                'kerioEmail',
+                __FUNCTION__,
+                $error,
+                'Debug Error',
+                $error->getMessage()
+            );
+            return ['error' => $error->getMessage()];
+        }             
+        $api->logout();
+
         /**
          * return success response
          */