From 87912a9e072b360f9df2066753b9572ac01398c4 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Sat, 24 Feb 2018 16:06:53 +0100 Subject: [PATCH] refactored moveCustomerToAdmin() function to Customers.move ApiCommand Signed-off-by: Michael Kaufmann (d00p) --- lib/classes/api/commands/class.Customers.php | 80 ++++++++++++++++++- lib/classes/api/commands/class.Domains.php | 2 +- .../froxlor/function.moveCustomerToAdmin.php | 62 -------------- 3 files changed, 79 insertions(+), 65 deletions(-) delete mode 100644 lib/functions/froxlor/function.moveCustomerToAdmin.php diff --git a/lib/classes/api/commands/class.Customers.php b/lib/classes/api/commands/class.Customers.php index 4bea264a..d3a762e4 100644 --- a/lib/classes/api/commands/class.Customers.php +++ b/lib/classes/api/commands/class.Customers.php @@ -1207,12 +1207,16 @@ class Customers extends ApiCommand implements ResourceEntity * move customer to another admin/reseller; #1166 */ if ($move_to_admin > 0 && $move_to_admin != $result['adminid']) { - $move_result = moveCustomerToAdmin($id, $move_to_admin); + $json_result = Customers::getLocal($this->getUserData(), array( + 'id' => $result['customerid'], + 'adminid' => $move_to_admin + ))->move(); + $move_result = json_decode($json_result, true)['data']; if ($move_result != true) { standard_error('moveofcustomerfailed', $move_result, true); } } - + return $this->response(200, "successfull", $upd_data); } throw new Exception("Not allowed to execute given command.", 403); @@ -1510,4 +1514,76 @@ class Customers extends ApiCommand implements ResourceEntity } throw new Exception("Not allowed to execute given command.", 403); } + + /** + * Function to move a given customer to a given admin/reseller + * and update all its references accordingly + * + * @param int $id + * customer-id + * @param int $adminid + * target-admin-id + * + * @access admin + * @throws Exception + * @return bool true on success, error-message on failure + */ + public function move() + { + if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) { + $id = $this->getParam('id'); + $adminid = $this->getParam('adminid'); + + // get customer + $json_result = Admins::getLocal($this->getUserData(), array( + 'id' => $id + ))->get(); + $c_result = json_decode($json_result, true)['data']; + + // check if target-admin is the current admin + if ($adminid == $c_result['adminid']) { + throw new Exception("Cannot move customer to the same admin/reseller as he currently is assigned to", 406); + } + + // get target admin + $json_result = Customers::getLocal($this->getUserData(), array( + 'id' => $adminid + ))->get(); + $a_result = json_decode($json_result, true)['data']; + + // Update customer entry + $updCustomer_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `adminid` = :adminid WHERE `customerid` = :cid + "); + Database::pexecute($updCustomer_stmt, array( + 'adminid' => $adminid, + 'cid' => $id + ), true, true); + + // Update customer-domains + $updDomains_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_DOMAINS . "` SET `adminid` = :adminid WHERE `customerid` = :cid + "); + Database::pexecute($updDomains_stmt, array( + 'adminid' => $adminid, + 'cid' => $id + ), true, true); + + // Update customer-tickets + $updTickets_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_TICKETS . "` SET `adminid` = :adminid WHERE `customerid` = :cid + "); + Database::pexecute($updTickets_stmt, array( + 'adminid' => $adminid, + 'cid' => $id + ), true, true); + + // now, recalculate the resource-usage for the old and the new admin + updateCounters(false); + + $log->logAction(ADM_ACTION, LOG_INFO, "[API] moved user '" . $c_result['loginname'] . "' from admin/reseller '" . $c_result['adminname'] . " to admin/reseller '" . $a_result['loginname'] . "'"); + return $this->response(200, "successfull", true); + } + throw new Exception("Not allowed to execute given command.", 403); + } } diff --git a/lib/classes/api/commands/class.Domains.php b/lib/classes/api/commands/class.Domains.php index b363c19e..1e135c2c 100644 --- a/lib/classes/api/commands/class.Domains.php +++ b/lib/classes/api/commands/class.Domains.php @@ -61,7 +61,7 @@ class Domains extends ApiCommand implements ResourceEntity * optional, the domain-id * @param string $domainname * optional, the domainname - * @param boolean $no_std_subdomain + * @param bool $no_std_subdomain * optional, default false * * @access admin diff --git a/lib/functions/froxlor/function.moveCustomerToAdmin.php b/lib/functions/froxlor/function.moveCustomerToAdmin.php deleted file mode 100644 index 0c2018bc..00000000 --- a/lib/functions/froxlor/function.moveCustomerToAdmin.php +++ /dev/null @@ -1,62 +0,0 @@ - $id - ) ); - - $log->logAction(ADM_ACTION, LOG_INFO, "moved user #" . $id . " from admin/reseller #".$cAdmin['adminid']." to admin/reseller #".$adminid); - - // Update customer entry - $updCustomer_stmt = Database::prepare ( " - UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `adminid` = :adminid WHERE `customerid` = :cid - " ); - Database::pexecute ( $updCustomer_stmt, array ( - 'adminid' => $adminid, - 'cid' => $id - ) ); - - // Update customer-domains - $updDomains_stmt = Database::prepare ( " - UPDATE `" . TABLE_PANEL_DOMAINS . "` SET `adminid` = :adminid WHERE `customerid` = :cid - " ); - Database::pexecute ( $updDomains_stmt, array ( - 'adminid' => $adminid, - 'cid' => $id - ) ); - - // Update customer-tickets - $updTickets_stmt = Database::prepare ( " - UPDATE `" . TABLE_PANEL_TICKETS . "` SET `adminid` = :adminid WHERE `customerid` = :cid - " ); - Database::pexecute ( $updTickets_stmt, array ( - 'adminid' => $adminid, - 'cid' => $id - ) ); - - // now, recalculate the resource-usage for the old and the new admin - updateCounters ( false ); - - return true; -}