[domainbulk] remove reqiurement for customer-select in webinterface as it is an API-parameter

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2020-10-31 09:45:50 +01:00
parent 15a13a7783
commit 36eb3cc1aa
5 changed files with 101 additions and 99 deletions

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -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");

View File

@@ -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',

View File

@@ -0,0 +1,79 @@
<?php
use PHPUnit\Framework\TestCase;
use Froxlor\Api\Commands\Domains;
use Froxlor\Bulk\DomainBulkAction;
/**
*
* @covers \Froxlor\Bulk\BulkAction
* @covers \Froxlor\Bulk\DomainBulkAction
*/
class DomainBulkTest extends TestCase
{
public function testNoImportFile()
{
global $admin_userdata;
$this->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 = <<<EOC
domain;loginname;
imported-a.com;test1;
imported-b.com;test1;
imported-c.com;test2;
EOC;
file_put_contents('/tmp/import-test.csv', $content);
$bulk = new DomainBulkAction("/tmp/import-test.csv", $admin_userdata);
$result = $bulk->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 = <<<EOC
domain;loginname;
imported-a.com;test1;
imported-b.com;test1;
imported-c.com;test2;
EOC;
file_put_contents('/tmp/import-test.csv', $content);
$bulk = new DomainBulkAction("/tmp/import-test.csv", $admin_userdata);
$result = $bulk->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']);
}
}