add testing for mysql-server/customer-update of allowed_mysqlserver; beautify config-command/file details

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-04-29 16:53:01 +02:00
parent bff499caed
commit 449897039c
12 changed files with 84 additions and 9 deletions

View File

@@ -8,7 +8,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
php-versions: ['7.4', '8.0'] php-versions: ['7.4', '8.1']
mariadb-version: [10.5, 10.4] mariadb-version: [10.5, 10.4]
steps: steps:
- name: Checkout - name: Checkout

View File

@@ -8,7 +8,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
php-versions: ['7.4', '8.0'] php-versions: ['7.4', '8.1']
mysql-version: [8.0, 5.7] mysql-version: [8.0, 5.7]
steps: steps:
- name: Checkout - name: Checkout

View File

@@ -479,7 +479,7 @@ abstract class ApiCommand extends ApiParameter
{ {
$customer_ids = []; $customer_ids = [];
if ($this->isAdmin()) { if ($this->isAdmin()) {
// if we're an admin, list all ftp-users of all the admins customers // if we're an admin, list all of the admins customers
// or optionally for one specific customer identified by id or loginname // or optionally for one specific customer identified by id or loginname
$customerid = $this->getParam('customerid', true, 0); $customerid = $this->getParam('customerid', true, 0);
$loginname = $this->getParam('loginname', true, ''); $loginname = $this->getParam('loginname', true, '');

View File

@@ -1117,7 +1117,21 @@ class Customers extends ApiCommand implements ResourceEntity
} }
// validate allowed_mysqls whether the customer has databases on a removed, now disallowed db-server and abort if true // validate allowed_mysqls whether the customer has databases on a removed, now disallowed db-server and abort if true
// @todo $former_allowed_mysqlserver = json_decode($result['allowed_mysqlserver'], true);
if ($allowed_mysqlserver != $former_allowed_mysqlserver) {
$to_remove_mysqlserver = array_diff($former_allowed_mysqlserver, $allowed_mysqlserver);
if (count($to_remove_mysqlserver) > 0) {
foreach ($to_remove_mysqlserver as $mysqlserver_check) {
$result_ms = $this->apiCall('MysqlServer.databasesOnServer', [
'mysql_server' => $mysqlserver_check,
'customerid' => $id
]);
if ($result_ms['count'] > 0) {
Response::standardError('mysqlserverstillhasdbs', '', true);
}
}
}
}
if ($email == '') { if ($email == '') {
Response::standardError([ Response::standardError([

View File

@@ -31,6 +31,7 @@ use PDOException;
use Froxlor\Froxlor; use Froxlor\Froxlor;
use Froxlor\Api\ApiCommand; use Froxlor\Api\ApiCommand;
use Froxlor\Api\ResourceEntity; use Froxlor\Api\ResourceEntity;
use Froxlor\Database\Database;
use Froxlor\Validate\Validate; use Froxlor\Validate\Validate;
class MysqlServer extends ApiCommand implements ResourceEntity class MysqlServer extends ApiCommand implements ResourceEntity
@@ -274,6 +275,31 @@ class MysqlServer extends ApiCommand implements ResourceEntity
throw new Exception('@TODO Later', 303); throw new Exception('@TODO Later', 303);
} }
/**
* check whether a given customer / current user (as customer) has
* databases on the given dbserver
*
* @param int mysql_server
* @param int $customerid
* optional, admin-only, select ftp-users of a specific customer by id
* @param string $loginname
* optional, admin-only, select ftp-users of a specific customer by loginname
*
* @access admin, customer
* @return string json-encoded array count
*/
public function databasesOnServer()
{
$dbserver = $this->getParam('mysql_server');
$customer_ids = $this->getAllowedCustomerIds();
$result_stmt = Database::prepare("
SELECT COUNT(*) num_dbs FROM `" . TABLE_PANEL_DATABASES . "`
WHERE `customerid` IN (" . implode(", ", $customer_ids) . ") AND `dbserver` = :dbserver");
$result = Database::pexecute_first($result_stmt, ['dbserver' => $dbserver], true, true);
return $this->response(['count' => $result['num_dbs']]);
}
private function generateNewUserData(array $sql, array $sql_root) private function generateNewUserData(array $sql, array $sql_root)
{ {
$content = '<?php' . PHP_EOL; $content = '<?php' . PHP_EOL;

View File

@@ -846,7 +846,7 @@ class SubDomains extends ApiCommand implements ResourceEntity
public function listing() public function listing()
{ {
if ($this->isAdmin()) { if ($this->isAdmin()) {
// if we're an admin, list all databases of all the admins customers // if we're an admin, list all subdomains of all the admins customers
// or optionally for one specific customer identified by id or loginname // or optionally for one specific customer identified by id or loginname
$customerid = $this->getParam('customerid', true, 0); $customerid = $this->getParam('customerid', true, 0);
$loginname = $this->getParam('loginname', true, ''); $loginname = $this->getParam('loginname', true, '');

View File

@@ -874,6 +874,7 @@ return [
'invaliddnsforletsencrypt' => 'Die DNS-Einträge der Domain enhalten keine der gewählten IP Adressen. Let\'s Encrypt Zertifikats-Erstellung ist nicht möglich.', 'invaliddnsforletsencrypt' => 'Die DNS-Einträge der Domain enhalten keine der gewählten IP Adressen. Let\'s Encrypt Zertifikats-Erstellung ist nicht möglich.',
'notallowedphpconfigused' => 'Nutzung einer PHP-Konfiguration welche nicht dem Kunden zugeordnet ist', 'notallowedphpconfigused' => 'Nutzung einer PHP-Konfiguration welche nicht dem Kunden zugeordnet ist',
'pathmustberelative' => 'Der Benutzer hat nicht die benötigten Berechtigungen, um Pfade außerhalb des Kunden-Heimatverzeichnisses anzugeben. Bitte einen relativen Pfad angeben (kein führendes /).', 'pathmustberelative' => 'Der Benutzer hat nicht die benötigten Berechtigungen, um Pfade außerhalb des Kunden-Heimatverzeichnisses anzugeben. Bitte einen relativen Pfad angeben (kein führendes /).',
'mysqlserverstillhasdbs' => 'Datenbank-Server kann für den Kunden nicht entfernt werden, da sich dort noch Datenbanken befinden.',
], ],
'extras' => [ 'extras' => [
'description' => 'Hier können Sie zusätzliche Extras einrichten, wie zum Beispiel einen Verzeichnisschutz.<br />Die Änderungen sind erst nach einer kurzen Zeit wirksam.', 'description' => 'Hier können Sie zusätzliche Extras einrichten, wie zum Beispiel einen Verzeichnisschutz.<br />Die Änderungen sind erst nach einer kurzen Zeit wirksam.',

View File

@@ -1171,6 +1171,7 @@ return [
'invaliddnsforletsencrypt' => 'The domains DNS does not include any of the chosen IP addresses. Let\'s Encrypt certificate generation not possible.', 'invaliddnsforletsencrypt' => 'The domains DNS does not include any of the chosen IP addresses. Let\'s Encrypt certificate generation not possible.',
'notallowedphpconfigused' => 'Trying to use php-config which is not assigned to customer', 'notallowedphpconfigused' => 'Trying to use php-config which is not assigned to customer',
'pathmustberelative' => 'The user does not have the permission to specify directories outside the customers home-directory. Please specify a relative path (no leading /).', 'pathmustberelative' => 'The user does not have the permission to specify directories outside the customers home-directory. Please specify a relative path (no leading /).',
'mysqlserverstillhasdbs' => 'Cannot remove database server from customers allow-list as there are still databases on it.',
], ],
'extras' => [ 'extras' => [
'description' => 'Here you can add some extras, for example directory protection.<br />The system will need some time to apply the new settings after every change.', 'description' => 'Here you can add some extras, for example directory protection.<br />The system will need some time to apply the new settings after every change.',

View File

@@ -1 +1 @@
<textarea class="form-control bg-secondary text-light" rows="{{ numbrows }}" readonly>{{ commands|raw }}</textarea> <textarea class="form-control bg-secondary text-light mb-2" rows="{{ numbrows }}" readonly>{{ commands|raw }}</textarea>

View File

@@ -1,2 +1,2 @@
<textarea class="form-control bg-secondary text-light" rows="1" readonly>{{ distro_editor }} {{ realname }}</textarea> <textarea class="form-control bg-secondary text-light mb-2" rows="1" readonly>{{ distro_editor }} {{ realname }}</textarea>
<textarea class="form-control" rows="{% if numbrows <= 20 %}{{ numbrows }}{% else %}21{% endif %}" readonly>{{ file_content|raw }}</textarea> <textarea class="form-control mb-2" rows="{% if numbrows <= 20 %}{{ numbrows }}{% else %}21{% endif %}" readonly>{{ file_content|raw }}</textarea>

View File

@@ -1,5 +1,5 @@
<fieldset class="file"> <fieldset class="file">
<legend>{{ realname }}</legend> {# <legend>{{ realname }}</legend> #}
{{ commands_pre|raw }} {{ commands_pre|raw }}
{{ commands_file|raw }} {{ commands_file|raw }}
{{ commands_post|raw }} {{ commands_post|raw }}

View File

@@ -5,6 +5,7 @@ use Froxlor\Settings;
use Froxlor\Api\Commands\Admins; use Froxlor\Api\Commands\Admins;
use Froxlor\Api\Commands\Customers; use Froxlor\Api\Commands\Customers;
use Froxlor\Api\Commands\Mysqls; use Froxlor\Api\Commands\Mysqls;
use Froxlor\Api\Commands\MysqlServer;
use Froxlor\Database\Database; use Froxlor\Database\Database;
use Froxlor\Settings\Store; use Froxlor\Settings\Store;
@@ -13,6 +14,7 @@ use Froxlor\Settings\Store;
* @covers \Froxlor\Api\ApiCommand * @covers \Froxlor\Api\ApiCommand
* @covers \Froxlor\Api\ApiParameter * @covers \Froxlor\Api\ApiParameter
* @covers \Froxlor\Api\Commands\Mysqls * @covers \Froxlor\Api\Commands\Mysqls
* @covers \Froxlor\Api\Commands\MysqlServer
* @covers \Froxlor\Api\Commands\Customers * @covers \Froxlor\Api\Commands\Customers
* @covers \Froxlor\Api\Commands\Admins * @covers \Froxlor\Api\Commands\Admins
* @covers \Froxlor\Database\DbManager * @covers \Froxlor\Database\DbManager
@@ -212,6 +214,36 @@ class MysqlsTest extends TestCase
$json_result = Mysqls::getLocal($customer_userdata)->listingCount(); $json_result = Mysqls::getLocal($customer_userdata)->listingCount();
$result = json_decode($json_result, true)['data']; $result = json_decode($json_result, true)['data'];
$this->assertEquals(2, $result); $this->assertEquals(2, $result);
$data = [
'mysql_server' => '0'
];
$json_result = MysqlServer::getLocal($admin_userdata, $data)->databasesOnServer();
$result = json_decode($json_result, true)['data'];
$this->assertEquals('2', $result['count']);
$data = [
'mysql_server' => '1'
];
$json_result = MysqlServer::getLocal($admin_userdata, $data)->databasesOnServer();
$result = json_decode($json_result, true)['data'];
$this->assertEquals('0', $result['count']);
}
/**
* @depends testCustomerMysqlsList
*/
public function testUpdateCustomerAllowedMysqlWithExistingDbs()
{
global $admin_userdata;
$this->expectExceptionMessage("Cannot remove database server from customers allow-list as there are still databases on it.");
// reactivate customer
// get customer
Customers::getLocal($admin_userdata, array(
'loginname' => 'test1',
'allowed_mysqls' => [1]
))->update();
} }
/** /**
@@ -315,4 +347,5 @@ class MysqlsTest extends TestCase
$this->assertEquals($testdata['password'], $passwd); $this->assertEquals($testdata['password'], $passwd);
} }
} }
} }