Skip to content

Commit

Permalink
Extract HTML from new user ajax call
Browse files Browse the repository at this point in the history
Server\Privileges::getExtraDataForAjaxBehavior

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Oct 4, 2019
1 parent b075f07 commit 5cc6709
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 88 deletions.
104 changes: 16 additions & 88 deletions libraries/classes/Server/Privileges.php
Expand Up @@ -1647,22 +1647,6 @@ public function getUserLink(
return $html;
}

/**
* Returns user group edit link
*
* @param string $username User name
*
* @return string HTML code with link
*/
public function getUserGroupEditLink($username)
{
return '<a class="edit_user_group_anchor ajax"'
. ' href="' . Url::getFromRoute('/server/privileges', ['username' => $username])
. '">'
. Util::getIcon('b_usrlist', __('Edit user group'))
. '</a>';
}

/**
* Returns number of defined user groups
*
Expand Down Expand Up @@ -1737,6 +1721,8 @@ public function getExtraDataForAjaxBehavior(
$hostname,
$username
) {
global $is_grantuser;

if (isset($GLOBALS['dbname'])) {
//if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
if (preg_match('/(?<!\\\\)(?:_|%)/', $GLOBALS['dbname'])) {
Expand All @@ -1757,79 +1743,21 @@ public function getExtraDataForAjaxBehavior(
}

if (isset($_POST['change_copy'])) {
/**
* generate html on the fly for the new user that was just created.
*/
$new_user_string = '<tr>' . "\n"
. '<td> <input type="checkbox" name="selected_usr[]" '
. 'id="checkbox_sel_users_"'
. 'value="'
. htmlspecialchars($username)
. '&amp;#27;' . htmlspecialchars($hostname) . '">'
. '</td>' . "\n"
. '<td><label for="checkbox_sel_users_">'
. (empty($_POST['username'])
? '<span style="color: #FF0000">' . __('Any') . '</span>'
: htmlspecialchars($username) ) . '</label></td>' . "\n"
. '<td>' . htmlspecialchars($hostname) . '</td>' . "\n";

$new_user_string .= '<td>';

if (! empty($password) || isset($_POST['pma_pw'])) {
$new_user_string .= __('Yes');
} else {
$new_user_string .= '<span style="color: #FF0000">'
. __('No')
. '</span>';
}

$new_user_string .= '</td>' . "\n";
$new_user_string .= '<td>'
. '<code>' . implode(', ', $this->extractPrivInfo(null, true)) . '</code>'
. '</td>'; //Fill in privileges here

// if $cfg['Servers'][$i]['users'] and $cfg['Servers'][$i]['usergroups'] are
// enabled
$cfgRelation = $this->relation->getRelationsParam();
if (! empty($cfgRelation['users']) && ! empty($cfgRelation['usergroups'])) {
$new_user_string .= '<td class="usrGroup"></td>';
}

$new_user_string .= '<td>';
if (isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y') {
$new_user_string .= __('Yes');
} else {
$new_user_string .= __('No');
}
$new_user_string .= '</td>';

if ($GLOBALS['is_grantuser']) {
$new_user_string .= '<td>'
. $this->getUserLink('edit', $username, $hostname)
. '</td>' . "\n";
}

if ($cfgRelation['menuswork'] && $user_group_count > 0) {
$new_user_string .= '<td>'
. $this->getUserGroupEditLink($username)
. '</td>' . "\n";
}

$new_user_string .= '<td>'
. $this->getUserLink(
'export',
$username,
$hostname,
'',
'',
'',
isset($_GET['initial']) ? $_GET['initial'] : ''
)
. '</td>' . "\n";

$new_user_string .= '</tr>';

$extra_data['new_user_string'] = $new_user_string;
$user = [
'name' => $username,
'host' => $hostname,
'has_password' => ! empty($password) || isset($_POST['pma_pw']),
'privileges' => implode(', ', $this->extractPrivInfo(null, true)),
'has_group' => ! empty($cfgRelation['users']) && ! empty($cfgRelation['usergroups']),
'has_group_edit' => $cfgRelation['menuswork'] && $user_group_count > 0,
'has_grant' => isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y',
];
$extra_data['new_user_string'] = $this->template->render('server/privileges/new_user_ajax', [
'user' => $user,
'is_grantuser' => $is_grantuser,
'initial' => $_GET['initial'] ?? '',
]);

/**
* Generate the string for this alphabet's initial, to update the user
Expand Down
66 changes: 66 additions & 0 deletions templates/server/privileges/new_user_ajax.twig
@@ -0,0 +1,66 @@
<tr>
<td>
<input type="checkbox" name="selected_usr[]" id="checkbox_sel_users_" value="
{{- user.name ~ '&amp;#27;' ~ user.host }}">
</td>
<td>
<label for="checkbox_sel_users_">
{% if user.name is empty %}
<span class="text-danger">{% trans 'Any' %}</span>
{% else %}
{{ user.name }}
{% endif %}
</label>
</td>
<td>
{{ user.host }}
</td>
<td>
{% if user.has_password %}
{% trans 'Yes' %}
{% else %}
<span class="text-danger">{% trans 'No' %}</span>
{% endif %}
</td>
<td>
<code>
{{ user.privileges|raw }}
</code>
</td>
{% if user.has_group %}
<td class="usrGroup"></td>
{% endif %}
<td>
{{ user.has_grant ? 'Yes'|trans : 'No'|trans }}
</td>
{% if is_grantuser %}
<td>
<a class="edit_user_anchor" href="{{ url('/server/privileges', {
'username': user.name,
'hostname': user.host,
'dbname': '',
'tablename': '',
'routinename': ''
}) }}">
{{ get_icon('b_usredit', 'Edit privileges'|trans) }}
</a>
</td>
{% endif %}
{% if user.has_group_edit %}
<td>
<a class="edit_user_group_anchor ajax" href="{{ url('/server/privileges', {'username': user.name}) }}">
{{ get_icon('b_usrlist', 'Edit user group'|trans) }}
</a>
</td>
{% endif %}
<td>
<a class="export_user_anchor ajax" href="{{ url('/server/privileges', {
'username': user.name,
'hostname': user.host,
'export': true,
'initial': initial
}) }}">
{{ get_icon('b_tblexport', 'Export'|trans) }}
</a>
</td>
</tr>

0 comments on commit 5cc6709

Please sign in to comment.