Skip to content

Commit

Permalink
On user creation, warn if the user already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
kasunchathuranga committed Apr 13, 2013
1 parent f9859a0 commit 57cb844
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
26 changes: 26 additions & 0 deletions js/server_privileges.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ function addUser($form)
* Unbind all event handlers before tearing down a page
*/
AJAX.registerTeardown('server_privileges.js', function() {
$("#fieldset_add_user_login input[name='username']").die("focusout");
$("#fieldset_add_user a.ajax").die("click");
$('form[name=usersForm]').unbind('submit');
$("#reload_privileges_anchor.ajax").die("click");
Expand All @@ -179,6 +180,31 @@ AJAX.registerTeardown('server_privileges.js', function() {
});

AJAX.registerOnload('server_privileges.js', function() {
/**
* Display a warning if there is already a user by the name entered as the username.
*/
$("#fieldset_add_user_login input[name='username']").live("focusout", function() {
var username = $(this).val();
var $warning = $("#user_exists_warning");
if ($("#select_pred_username").val() == 'userdefined' && username != '') {
var href = $("form[name='usersForm']").attr('action');
var params = {
'ajax_request' : true,
'token' : PMA_commonParams.get('token'),
'validate_username' : true,
'username' : username
};
$.get(href, params, function(data) {
if (data['user_exists']) {
$warning.show();
} else {
$warning.hide();
}
});
} else {
$warning.hide();
}
});
/**
* AJAX event handler for 'Add a New User'
*
Expand Down
25 changes: 23 additions & 2 deletions libraries/server_privileges.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new')
$html_output .= ' onchange="'
. 'if (this.value == \'any\') {'
. ' username.value = \'\'; '
. ' user_exists_warning.style.display = \'none\'; '
. '} else if (this.value == \'userdefined\') {'
. ' username.focus(); username.select(); '
. '}">' . "\n";
Expand Down Expand Up @@ -1068,8 +1069,15 @@ function PMA_getHtmlForDisplayLoginInformationFields($mode = 'new')
: $GLOBALS['username']
) . '"'
)
. ' onchange="pred_username.value = \'userdefined\';" />' . "\n"
. '</div>' . "\n";
. ' onchange="pred_username.value = \'userdefined\';" />' . "\n";

$html_output .= '<div id="user_exists_warning"'
. ' name="user_exists_warning" style="display:none;">'
. PMA_Message::notice(
__('Another account with the same username already exists, maybe you would like to check that.')
)->getDisplay()
. '</div>';
$html_output .= '</div>';

$html_output .= '<div class="item">' . "\n"
. '<label for="select_pred_hostname">' . "\n"
Expand Down Expand Up @@ -1905,6 +1913,19 @@ function PMA_getExtraDataForAjaxBehavior($password, $link_export, $sql_query,

$extra_data['new_privileges'] = $new_privileges;
}

if (isset($_REQUEST['validate_username'])) {
$sql_query = "SELECT * FROM `mysql`.`user` WHERE `User` = '"
. $_REQUEST['username'] . "';";
$res = PMA_DBI_query($sql_query);
$row = PMA_DBI_fetch_row($res);
if (empty($row)) {
$extra_data['user_exists'] = false;
} else {
$extra_data['user_exists'] = true;
}
}

return $extra_data;
}

Expand Down
4 changes: 4 additions & 0 deletions server_privileges.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@
$message = PMA_Message::success(__('The privileges were reloaded successfully.'));
}

if (isset($_REQUEST['validate_username'])) {
$message = PMA_Message::success();
}

/**
* some standard links
*/
Expand Down

0 comments on commit 57cb844

Please sign in to comment.