Skip to content

Commit

Permalink
Extract method to get edit user group modal form
Browse files Browse the repository at this point in the history
Creates a new route: /server/user-groups/edit-form

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Apr 27, 2021
1 parent f28dc34 commit 5750dfb
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 96 deletions.
4 changes: 1 addition & 3 deletions js/src/server/privileges.js
Expand Up @@ -37,11 +37,9 @@ function editUserGroup (event) {
const username = button.getAttribute('data-username');

$.get(
'index.php?route=/server/privileges',
'index.php?route=/server/user-groups/edit-form',
{
'username': username,
'ajax_request': true,
'edit_user_group_dialog': true,
'server': CommonParams.get('server')
},
data => {
Expand Down
13 changes: 0 additions & 13 deletions libraries/classes/Controllers/Server/PrivilegesController.php
Expand Up @@ -338,7 +338,6 @@ public function index(): void
|| $_GET['initial'] === '')
|| (isset($_POST['delete']) && $_POST['delete'] === __('Go')))
&& ! isset($_GET['showall'])
&& ! isset($_GET['edit_user_group_dialog'])
) {
$extra_data = $serverPrivileges->getExtraDataForAjaxBehavior(
($password ?? ''),
Expand Down Expand Up @@ -383,18 +382,6 @@ public function index(): void
unset($GLOBALS['message']);
}

if (! empty($_GET['edit_user_group_dialog']) && $cfgRelation['menuswork']) {
$dialog = $serverPrivileges->getHtmlToChooseUserGroup($username ?? null);

if ($this->response->isAjax()) {
$this->response->addJSON('message', $dialog);

return;
}

$this->response->addHTML($dialog);
}

// export user definition
if (
isset($_GET['export'])
Expand Down
72 changes: 72 additions & 0 deletions libraries/classes/Controllers/Server/UserGroupsController.php
Expand Up @@ -4,13 +4,18 @@

namespace PhpMyAdmin\Controllers\Server;

use PhpMyAdmin\CheckUserPrivileges;
use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Response;
use PhpMyAdmin\Server\UserGroups;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;

use function sprintf;
use function strlen;

/**
* Displays the 'User groups' sub page under 'Users' page.
Expand Down Expand Up @@ -99,4 +104,71 @@ public function index(): void

$this->response->addHTML('</div>');
}

public function editUserGroupModalForm(): void
{
$this->response->setAjax(true);

if (! isset($_GET['username']) || strlen((string) $_GET['username']) === 0) {
$this->response->setRequestStatus(false);
$this->response->setHttpResponseCode(400);
$this->response->addJSON('message', __('Missing parameter:') . ' username');

return;
}

$username = $_GET['username'];

$checkUserPrivileges = new CheckUserPrivileges($this->dbi);
$checkUserPrivileges->getPrivileges();

$cfgRelation = $this->relation->getRelationsParam();

if (! $cfgRelation['menuswork']) {
$this->response->setRequestStatus(false);
$this->response->setHttpResponseCode(400);
$this->response->addJSON('message', __('User groups management is not enabled.'));

return;
}

$form = $this->getHtmlToChooseUserGroup($username, $cfgRelation);

$this->response->addJSON('message', $form);
}

/**
* Displays a dropdown to select the user group with menu items configured to each of them.
*
* @param array<string, mixed> $cfgRelation
*/
private function getHtmlToChooseUserGroup(string $username, array $cfgRelation): string
{
$groupTable = Util::backquote($cfgRelation['db']) . '.' . Util::backquote($cfgRelation['usergroups']);
$userTable = Util::backquote($cfgRelation['db']) . '.' . Util::backquote($cfgRelation['users']);

$sqlQuery = sprintf(
'SELECT `usergroup` FROM %s WHERE `username` = \'%s\'',
$userTable,
$this->dbi->escapeString($username)
);
$userGroup = $this->dbi->fetchValue($sqlQuery, 0, 0, DatabaseInterface::CONNECT_CONTROL);

$allUserGroups = [];
$sqlQuery = 'SELECT DISTINCT `usergroup` FROM ' . $groupTable;
$result = $this->relation->queryAsControlUser($sqlQuery, false);
if ($result) {
while ($row = $this->dbi->fetchRow($result)) {
$allUserGroups[$row[0]] = $row[0];
}
}

$this->dbi->freeResult($result);

return $this->template->render('server/privileges/choose_user_group', [
'all_user_groups' => $allUserGroups,
'user_group' => $userGroup,
'params' => ['username' => $username],
]);
}
}
46 changes: 0 additions & 46 deletions libraries/classes/Server/Privileges.php
Expand Up @@ -536,52 +536,6 @@ public function getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname
. " AND `Table_name` = '" . $this->dbi->escapeString($table) . "';";
}

/**
* Displays a dropdown to select the user group
* with menu items configured to each of them.
*
* @param string $username username
*
* @return string html to select the user group
*/
public function getHtmlToChooseUserGroup($username)
{
$cfgRelation = $this->relation->getRelationsParam();
$groupTable = Util::backquote($cfgRelation['db'])
. '.' . Util::backquote($cfgRelation['usergroups']);
$userTable = Util::backquote($cfgRelation['db'])
. '.' . Util::backquote($cfgRelation['users']);

$userGroup = '';
if (isset($GLOBALS['username'])) {
$sqlQuery = 'SELECT `usergroup` FROM ' . $userTable
. " WHERE `username` = '" . $this->dbi->escapeString($username) . "'";
$userGroup = $this->dbi->fetchValue(
$sqlQuery,
0,
0,
DatabaseInterface::CONNECT_CONTROL
);
}

$allUserGroups = [];
$sqlQuery = 'SELECT DISTINCT `usergroup` FROM ' . $groupTable;
$result = $this->relation->queryAsControlUser($sqlQuery, false);
if ($result) {
while ($row = $this->dbi->fetchRow($result)) {
$allUserGroups[$row[0]] = $row[0];
}
}

$this->dbi->freeResult($result);

return $this->template->render('server/privileges/choose_user_group', [
'all_user_groups' => $allUserGroups,
'user_group' => $userGroup,
'params' => ['username' => $username],
]);
}

/**
* Sets the user group from request values
*
Expand Down
5 changes: 4 additions & 1 deletion libraries/routes.php
Expand Up @@ -247,7 +247,10 @@
$routes->get('/queries', [QueriesController::class, 'index']);
$routes->addRoute(['GET', 'POST'], '/variables', [StatusVariables::class, 'index']);
});
$routes->addRoute(['GET', 'POST'], '/user-groups', [UserGroupsController::class, 'index']);
$routes->addGroup('/user-groups', static function (RouteCollector $routes): void {
$routes->addRoute(['GET', 'POST'], '', [UserGroupsController::class, 'index']);
$routes->get('/edit-form', [UserGroupsController::class, 'editUserGroupModalForm']);
});
$routes->addGroup('/variables', static function (RouteCollector $routes): void {
$routes->get('', [VariablesController::class, 'index']);
$routes->get('/get/{name}', [VariablesController::class, 'getValue']);
Expand Down
9 changes: 2 additions & 7 deletions psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.7.0@d4377c0baf3ffbf0b1ec6998e8d1be2a40971005">
<files psalm-version="4.7.1@cd53e047a58f71f646dd6bf45476076ab07b5d44">
<file src="libraries/classes/Bookmark.php">
<RedundantCastGivenDocblockType occurrences="1">
<code>(int) $this-&gt;id</code>
Expand Down Expand Up @@ -392,7 +392,7 @@
<PossiblyInvalidArgument occurrences="1">
<code>$dbname</code>
</PossiblyInvalidArgument>
<PossiblyNullArgument occurrences="11">
<PossiblyNullArgument occurrences="10">
<code>$dbname</code>
<code>$hostname</code>
<code>$hostname</code>
Expand All @@ -403,7 +403,6 @@
<code>$username</code>
<code>$username</code>
<code>$username ?? null</code>
<code>$username ?? null</code>
</PossiblyNullArgument>
<TypeDoesNotContainNull occurrences="1">
<code>''</code>
Expand Down Expand Up @@ -2496,10 +2495,6 @@
</TypeDoesNotContainType>
</file>
<file src="libraries/classes/Table/ColumnsDefinition.php">
<PossiblyFalseArgument occurrences="2">
<code>$form_params['db']</code>
<code>$form_params['table']</code>
</PossiblyFalseArgument>
<PossiblyUndefinedVariable occurrences="1">
<code>$submit_fulltext</code>
</PossiblyUndefinedVariable>
Expand Down
26 changes: 0 additions & 26 deletions test/classes/Server/PrivilegesTest.php
Expand Up @@ -318,32 +318,6 @@ public function testGetGrantsArray(): void
);
}

/**
* Test for getHtmlToChooseUserGroup
*/
public function testGetHtmlToChooseUserGroup(): void
{
$username = 'pma_username';

$html = $this->serverPrivileges->getHtmlToChooseUserGroup($username);
$this->assertStringContainsString(
'<form class="ajax" id="changeUserGroupForm"',
$html
);
//Url::getHiddenInputs
$params = ['username' => $username];
$html_output = Url::getHiddenInputs($params);
$this->assertStringContainsString(
$html_output,
$html
);
//__('User group')
$this->assertStringContainsString(
__('User group'),
$html
);
}

/**
* Test for getSqlQueryForDisplayPrivTable
*/
Expand Down

0 comments on commit 5750dfb

Please sign in to comment.