From 36eb3cc1aa8f1f5b13fab3eda4e4bebc0f514568 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Sat, 31 Oct 2020 09:45:50 +0100 Subject: [PATCH] [domainbulk] remove reqiurement for customer-select in webinterface as it is an API-parameter Signed-off-by: Michael Kaufmann --- admin_domains.php | 16 +--- lib/Froxlor/Bulk/BulkAction.php | 80 ++++--------------- lib/Froxlor/Bulk/DomainBulkAction.php | 19 ++--- .../domains/formfield.domains_import.php | 6 -- tests/Bulk/DomainBulkTest.php | 79 ++++++++++++++++++ 5 files changed, 101 insertions(+), 99 deletions(-) create mode 100644 tests/Bulk/DomainBulkTest.php diff --git a/admin_domains.php b/admin_domains.php index d232f131..16734d20 100644 --- a/admin_domains.php +++ b/admin_domains.php @@ -616,7 +616,6 @@ if ($page == 'domains' || $page == 'overview') { if (isset($_POST['send']) && $_POST['send'] == 'send') { - $customerid = intval($_POST['customerid']); $separator = \Froxlor\Validate\Validate::validate($_POST['separator'], 'separator'); $offset = (int) \Froxlor\Validate\Validate::validate($_POST['offset'], 'offset', "/[0-9]/i"); @@ -625,7 +624,7 @@ if ($page == 'domains' || $page == 'overview') { $result = array(); try { - $bulk = new \Froxlor\Bulk\DomainBulkAction($file_name, $customerid); + $bulk = new \Froxlor\Bulk\DomainBulkAction($file_name, $userinfo); $result = $bulk->doImport($separator, $offset); } catch (Exception $e) { \Froxlor\UI\Response::standard_error('domain_import_error', $e->getMessage()); @@ -647,19 +646,6 @@ if ($page == 'domains' || $page == 'overview') { 'page' => 'domains' )); } else { - $customers = \Froxlor\UI\HTML::makeoption($lng['panel']['please_choose'], 0, 0, true); - $result_customers_stmt = Database::prepare(" - SELECT `customerid`, `loginname`, `name`, `firstname`, `company` - FROM `" . TABLE_PANEL_CUSTOMERS . "` " . ($userinfo['customers_see_all'] ? '' : " WHERE `adminid` = '" . (int) $userinfo['adminid'] . "' ") . " ORDER BY `name` ASC"); - $params = array(); - if ($userinfo['customers_see_all'] == '0') { - $params['adminid'] = $userinfo['adminid']; - } - Database::pexecute($result_customers_stmt, $params); - - while ($row_customer = $result_customers_stmt->fetch(PDO::FETCH_ASSOC)) { - $customers .= \Froxlor\UI\HTML::makeoption(\Froxlor\User::getCorrectFullUserDetails($row_customer) . ' (' . $row_customer['loginname'] . ')', $row_customer['customerid']); - } $domain_import_data = include_once dirname(__FILE__) . '/lib/formfields/admin/domains/formfield.domains_import.php'; $domain_import_form = \Froxlor\UI\HtmlForm::genHTMLForm($domain_import_data); diff --git a/lib/Froxlor/Bulk/BulkAction.php b/lib/Froxlor/Bulk/BulkAction.php index cd591d20..457ef46b 100644 --- a/lib/Froxlor/Bulk/BulkAction.php +++ b/lib/Froxlor/Bulk/BulkAction.php @@ -35,20 +35,6 @@ abstract class BulkAction */ private $impFile = null; - /** - * customer id of the user the entity is being added to - * - * @var int - */ - private $custId = null; - - /** - * array of customer data read from the database - * - * @var array - */ - private $custData = null; - /** * api-function to call for addingg entity * @@ -70,20 +56,27 @@ abstract class BulkAction */ private $errors = array(); + /** + * logged in user + * + * @var array + */ + protected $userinfo = array(); + /** * class constructor, optionally sets file and customer-id * * @param string $import_file - * @param int $customer_id + * @param array $userinfo * * @return object BulkAction instance */ - protected function __construct($import_file = null, $customer_id = 0) + protected function __construct($import_file = null, $userinfo = array()) { if (! empty($import_file)) { $this->impFile = \Froxlor\FileDir::makeCorrectFile($import_file); } - $this->custId = $customer_id; + $this->userinfo = $userinfo; } /** @@ -109,18 +102,6 @@ abstract class BulkAction $this->impFile = \Froxlor\FileDir::makeCorrectFile($import_file); } - /** - * setter for customer-id - * - * @param int $customer_id - * - * @return void - */ - public function setCustomer($customer_id = 0) - { - $this->custId = $customer_id; - } - /** * return the list of errors * @@ -145,7 +126,7 @@ abstract class BulkAction protected function importEntity($data_array = null) { - global $userinfo; + if (empty($data_array)) return null; $module = '\\Froxlor\\Api\\Commands\\' . substr($this->api_call, 0, strpos($this->api_call, ".")); $function = substr($this->api_call, strpos($this->api_call, ".") + 1); @@ -159,7 +140,7 @@ abstract class BulkAction $result = null; try { - $json_result = $module::getLocal($userinfo, $new_data)->$function(); + $json_result = $module::getLocal($this->userinfo, $new_data)->$function(); $result = json_decode($json_result, true)['data']; } catch (\Exception $e) { $this->errors[] = $e->getMessage(); @@ -189,6 +170,10 @@ abstract class BulkAction throw new \Exception("Unable to read file '" . $this->impFile . "'"); } + if (empty($separator) || strlen($separator) != 1) { + throw new \Exception("Invalid separator specified: '" . $separator . "'"); + } + $file_data = array(); $is_params_line = true; $fh = @fopen($this->impFile, "r"); @@ -218,37 +203,4 @@ abstract class BulkAction return $file_data; } - /** - * to be called first in doImport() to read in customer and entity data - */ - protected function preImport() - { - $this->readCustomerData(); - - if ($this->custId <= 0) { - throw new \Exception("Invalid customer selected"); - } - - if (is_null($this->custData)) { - throw new \Exception("Failed to read customer data"); - } - } - - /** - * reads customer data from panel_customer by $_custId - * - * @return bool - */ - protected function readCustomerData() - { - $cust_stmt = \Froxlor\Database\Database::prepare("SELECT * FROM `" . TABLE_PANEL_CUSTOMERS . "` WHERE `customerid` = :cid"); - $this->custData = \Froxlor\Database\Database::pexecute_first($cust_stmt, array( - 'cid' => $this->custId - )); - if (is_array($this->custData) && isset($this->custData['customerid']) && $this->custData['customerid'] == $this->custId) { - return true; - } - $this->custData = null; - return false; - } } diff --git a/lib/Froxlor/Bulk/DomainBulkAction.php b/lib/Froxlor/Bulk/DomainBulkAction.php index 15bbe2b1..2c0cf280 100644 --- a/lib/Froxlor/Bulk/DomainBulkAction.php +++ b/lib/Froxlor/Bulk/DomainBulkAction.php @@ -32,9 +32,9 @@ class DomainBulkAction extends BulkAction * * @return object DomainBulkAction instance */ - public function __construct($import_file = null, $customer_id = 0) + public function __construct($import_file = null, $userinfo) { - parent::__construct($import_file, $customer_id); + parent::__construct($import_file, $userinfo); $this->setApiCall('Domains.add'); } @@ -49,23 +49,14 @@ class DomainBulkAction extends BulkAction */ public function doImport($separator = ";", $offset = 0) { - $this->preImport(); - - // get the admins userinfo to check for domains_used, etc. - global $userinfo; - - if ($userinfo['domains'] == "-1") { + if ($this->userinfo['domains'] == "-1") { $dom_unlimited = true; } else { $dom_unlimited = false; } - $domains_used = (int) $userinfo['domains_used']; - $domains_avail = (int) $userinfo['domains']; - - if (empty($separator) || strlen($separator) != 1) { - throw new \Exception("Invalid separator specified: '" . $separator . "'"); - } + $domains_used = (int) $this->userinfo['domains_used']; + $domains_avail = (int) $this->userinfo['domains']; if (! is_int($offset) || $offset < 0) { throw new \Exception("Invalid offset specified"); diff --git a/lib/formfields/admin/domains/formfield.domains_import.php b/lib/formfields/admin/domains/formfield.domains_import.php index 060b8111..fd7b3372 100644 --- a/lib/formfields/admin/domains/formfield.domains_import.php +++ b/lib/formfields/admin/domains/formfield.domains_import.php @@ -23,12 +23,6 @@ return array( 'title' => $lng['domains']['domain_import'], 'image' => 'icons/domain_add.png', 'fields' => array( - 'customerid' => array( - 'label' => $lng['admin']['customer'], - 'type' => 'select', - 'select_var' => $customers, - 'mandatory' => true - ), 'separator' => array( 'label' => $lng['domains']['import_separator'], 'type' => 'text', diff --git a/tests/Bulk/DomainBulkTest.php b/tests/Bulk/DomainBulkTest.php new file mode 100644 index 00000000..2a084660 --- /dev/null +++ b/tests/Bulk/DomainBulkTest.php @@ -0,0 +1,79 @@ +expectExceptionMessage("No file was given for import"); + $bulk = new DomainBulkAction(null, $admin_userdata); + $bulk->doImport(";", 0); + } + + public function testImportFileDoesNotExist() + { + global $admin_userdata; + $this->expectExceptionMessage("The file '/tmp/nonexisting.csv' could not be found"); + $bulk = new DomainBulkAction("/tmp/nonexisting.csv", $admin_userdata); + $bulk->doImport(";", 0); + } + + public function testImportDomains() + { + global $admin_userdata; + + $content = <<doImport(";", 0); + + $this->assertEquals(3, $result['all']); + $this->assertEquals(2, $result['imported']); + $this->assertEquals("Customer with loginname 'test2' could not be found", $bulk->getErrors()[0]); + + // now check whether the domain really exists for test1 user + $data = [ + 'domain' => 'imported-a.com' + ]; + $json_result = Domains::getLocal($admin_userdata, $data)->get(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals('imported-a.com', $result['domain']); + $this->assertEquals(1, $result['customerid']); + } + + public function testImportDomainsMaxAlloc() + { + global $admin_userdata; + + // fake allocation restriction + $admin_userdata['domains'] = 1; + + $content = <<doImport(";", 0); + + $this->assertEquals(3, $result['all']); + $this->assertEquals(0, $result['imported']); + $this->assertEquals("You have reached your maximum allocation of domains (" . $admin_userdata['domains'] . ")", $result['notes']); + } +}