refactored moveCustomerToAdmin() function to Customers.move ApiCommand

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2018-02-24 16:06:53 +01:00
parent 0bf430e0c1
commit 87912a9e07
3 changed files with 79 additions and 65 deletions

View File

@@ -1207,12 +1207,16 @@ class Customers extends ApiCommand implements ResourceEntity
* move customer to another admin/reseller; #1166 * move customer to another admin/reseller; #1166
*/ */
if ($move_to_admin > 0 && $move_to_admin != $result['adminid']) { 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) { if ($move_result != true) {
standard_error('moveofcustomerfailed', $move_result, true); standard_error('moveofcustomerfailed', $move_result, true);
} }
} }
return $this->response(200, "successfull", $upd_data); return $this->response(200, "successfull", $upd_data);
} }
throw new Exception("Not allowed to execute given command.", 403); 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); 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);
}
} }

View File

@@ -61,7 +61,7 @@ class Domains extends ApiCommand implements ResourceEntity
* optional, the domain-id * optional, the domain-id
* @param string $domainname * @param string $domainname
* optional, the domainname * optional, the domainname
* @param boolean $no_std_subdomain * @param bool $no_std_subdomain
* optional, default false * optional, default false
* *
* @access admin * @access admin

View File

@@ -1,62 +0,0 @@
<?php
/**
* 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
*
* @return true on success, error-message on failure
*/
function moveCustomerToAdmin($id = 0, $adminid = 0) {
global $log;
if ($id <= 0 || $adminid <= 0) {
return "no valid id's given";
}
// get current admin-id
$cAdmin_stmt = Database::prepare ( "
SELECT `adminid` FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE `customerid` = :cid
" );
$cAdmin = Database::pexecute_first ( $cAdmin_stmt, array (
'cid' => $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;
}