Merge pull request #11569 from devenbansod/refactor_auth_plugins

Refactor code for generation available auth plugins HTML
This commit is contained in:
Marc Delisle 2015-10-13 10:59:15 -04:00
commit 17c4bb184c
4 changed files with 168 additions and 101 deletions

View File

@ -4766,7 +4766,7 @@ AJAX.registerOnload('functions.js', function(){
* method is selected
* Used in user_password.php (Change Password link on index.php)
*/
$(document).on("change", 'input[type=radio][name="pw_hash"]', function() {
$(document).on("change", 'select#select_authentication_plugin_cp', function() {
if (this.value === 'sha256_password') {
$('#ssl_reqd_warning_cp').show();
} else {

View File

@ -79,52 +79,31 @@ function PMA_getHtmlForChangePassword($username, $hostname)
. '</tr>';
$html .= '<tr class="vmiddle">'
. '<td>' . __('Password Hashing:') . '</td>';
. '<td>' . __('Password Hashing:') . '</td><td>';
$serverType = PMA_Util::getServerType();
$orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
'change',
$username,
$hostname
);
if (($serverType == 'MySQL'
&& PMA_MYSQL_INT_VERSION >= 50507)
|| ($serverType == 'MariaDB'
&& PMA_MYSQL_INT_VERSION >= 50200)
) {
$active_auth_plugins = PMA_getActiveAuthPlugins();
$default_auth_plugin = PMA_getCurrentAuthenticationPlugin(
'change', $username, $hostname
$auth_plugin_dropdown = PMA_getHtmlForAuthPluginsDropdown(
$username, $hostname, $orig_auth_plugin, 'change_pw', 'new'
);
$iter = 0;
$total_plugins = count($active_auth_plugins);
foreach ($active_auth_plugins as $plugin) {
if ($plugin['PLUGIN_NAME'] == 'mysql_old_password') {
continue;
}
if ($iter != 0) {
$html .= '<td>&nbsp;</td>';
}
$html .= '<td>'
. '<input type="radio" name="pw_hash" value="'
. $plugin['PLUGIN_NAME'] . '"'
. ($default_auth_plugin == $plugin['PLUGIN_NAME'] ? 'checked="checked" ' : '')
. ' id="radio_pw_hash_' . $plugin['PLUGIN_NAME'] . '" />'
. '<label for="radio_pw_hash_' . $plugin['PLUGIN_NAME'] . '" >'
. __($plugin['PLUGIN_DESCRIPTION']) . ' </label></td></tr>';
if ($iter == $total_plugins - 2) {
$html .= '<tr id="tr_element_before_generate_password">';
} else if ($iter != $total_plugins - 1) {
$html .= '<tr>';
}
$iter++;
}
$html .= '</tr>';
$html .= $auth_plugin_dropdown;
$html .= '</td></tr>';
$html .= '<tr id="tr_element_before_generate_password"></tr>';
$html .= '</table>';
$html .= '<div '
. ($default_auth_plugin != 'sha256_password' ? 'style="display:none"' : '')
. ($orig_auth_plugin != 'sha256_password' ? 'style="display:none"' : '')
. ' id="ssl_reqd_warning_cp">'
. PMA_Message::notice(
__(
@ -137,11 +116,11 @@ function PMA_getHtmlForChangePassword($username, $hostname)
->getDisplay()
. '</div>';
} else {
$html .= '<td>'
. '<input type="radio" name="pw_hash" value="mysql_native_password"'
. 'checked="checked" id="radio_pw_hash_native" />'
. '<label for="radio_pw_hash_native" >'
. __('MySQL Native Authentication') . ' </label></td></tr>'
$auth_plugin_dropdown = PMA_getHtmlForAuthPluginsDropdown(
$username, $hostname, $orig_auth_plugin, 'change_pw', 'old'
);
$html .= $auth_plugin_dropdown . '</td></tr>'
. '<tr id="tr_element_before_generate_password"></tr>'
. '</table>';
}

View File

@ -1411,6 +1411,50 @@ function PMA_getHtmlForGlobalPrivTableWithCheckboxes(
return $html_output;
}
/**
* Gets the currently active authentication plugins
*
* @param string $username User name
* @param string $hostname Host name
* @param string $orig_auth_plugin Default Authentication plugin
* @param string $mode are we creating a new user or are we just
* changing one?
* (allowed values: 'new', 'edit', 'change_pw')
* @param string $versions Is MySQL version newer or older than 5.5.7
*
* @return string $html_output
*/
function PMA_getHtmlForAuthPluginsDropdown(
$username,
$hostname,
$orig_auth_plugin,
$mode = 'new',
$versions = 'new'
) {
$html_output = '<select '
. 'id="select_authentication_plugin'
. ($mode =='change_pw' ? '_cp' : '') . '" '
. 'name="authentication_plugin" >';
if ($versions == 'new') {
$active_auth_plugins = PMA_getActiveAuthPlugins();
foreach ($active_auth_plugins as $plugin) {
if ($plugin['PLUGIN_NAME'] == 'mysql_old_password') {
continue;
}
$html_output .= '<option value="' . $plugin['PLUGIN_NAME'] . '"'
. ($orig_auth_plugin == $plugin['PLUGIN_NAME'] ? 'selected ' : '')
. '>' . __($plugin['PLUGIN_DESCRIPTION']) . '</option>';
}
$html_output .= '</select>';
} else {
$html_output .= '<option value="mysql_native_password" >'
. __('MySQL Native Authentication') . '</option>'
. '</select>';
}
return $html_output;
}
/**
* Gets the currently active authentication plugins
*
@ -1710,6 +1754,12 @@ function PMA_getHtmlForLoginInformationFields(
. '<label for="select_authentication_plugin" >';
$serverType = PMA_Util::getServerType();
$auth_plugin_dropdown = '';
$orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
$mode,
$username,
$hostname
);
if (($serverType == 'MySQL'
&& PMA_MYSQL_INT_VERSION >= 50507)
@ -1717,49 +1767,36 @@ function PMA_getHtmlForLoginInformationFields(
&& PMA_MYSQL_INT_VERSION >= 50200)
) {
$html_output .= __('Authentication Plugin')
. '</label><span class="options">&nbsp;</span>' . "\n"
. '<select id="select_authentication_plugin" name="authentication_plugin" >';
. '</label><span class="options">&nbsp;</span>' . "\n";
$active_auth_plugins = PMA_getActiveAuthPlugins();
$orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
$mode, $username, $hostname
$auth_plugin_dropdown = PMA_getHtmlForAuthPluginsDropdown(
$username, $hostname, $orig_auth_plugin, $mode, 'new'
);
foreach ($active_auth_plugins as $plugin) {
if ($plugin['PLUGIN_NAME'] == 'mysql_old_password') {
continue;
}
$html_output .= '<option value="' . $plugin['PLUGIN_NAME'] . '"'
. ($orig_auth_plugin == $plugin['PLUGIN_NAME'] ? 'selected ' : '')
. '>' . __($plugin['PLUGIN_DESCRIPTION']) . '</option>';
}
$html_output .= '</select>'
. '<div id="ssl_reqd_warning" '
. ($orig_auth_plugin == 'sha256_password' ? '' : ' style="display:none"')
. ' >'
. PMA_Message::notice(
__(
'This method requires using an \'<i>SSL connection</i>\' '
. 'or an \'<i>unencrypted connection that encrypts the password '
. 'using RSA</i>\'; while connecting to the server.'
)
. PMA_Util::showMySQLDocu('sha256-authentication-plugin')
)->getDisplay()
. '</div>';
} else {
$html_output .= __('Password Hashing Method')
. '</label><span class="options">&nbsp;</span>' . "\n"
. '<select id="select_authentication_plugin" '
. 'name="authentication_plugin" >'
. '<option value="mysql_native_password" >'
. __('MySQL Native Authentication') . '</option>'
. '</select>';
. '</label><span class="options">&nbsp;</span>' . "\n";
$auth_plugin_dropdown = PMA_getHtmlForAuthPluginsDropdown(
$username, $hostname, $orig_auth_plugin, $mode, 'old'
);
}
$html_output .= $auth_plugin_dropdown;
$html_output .= '<div '
. ($orig_auth_plugin != 'sha256_password' ? 'style="display:none"' : '')
. ' id="ssl_reqd_warning">'
. PMA_Message::notice(
__(
'This method requires using an \'<i>SSL connection</i>\' '
. 'or an \'<i>unencrypted connection that encrypts the password '
. 'using RSA</i>\'; while connecting to the server.'
)
. PMA_Util::showMySQLDocu('sha256-authentication-plugin')
)
->getDisplay()
. '</div>';
$html_output .= '</div>' . "\n"
// Generate password added here via jQuery
// Generate password added here via jQuery
. '</fieldset>' . "\n";
return $html_output;
@ -1898,14 +1935,19 @@ function PMA_updatePassword($err_url, $username, $hostname)
// here $nopass could be == 1
if (empty($message)) {
$hashing_function = 'PASSWORD';
if (PMA_Util::getServerType() == 'MySQL'
&& PMA_MYSQL_INT_VERSION >= 50706
) {
if (! empty($_REQUEST['pw_hash']) && $_REQUEST['pw_hash'] != 'old') {
if (isset($_REQUEST['authentication_plugin'])
&& $_REQUEST['authentication_plugin'] != 'mysql_old_password'
) {
$query_prefix = "ALTER USER '"
. PMA_Util::sqlAddSlashes($username)
. "'@'" . PMA_Util::sqlAddSlashes($hostname) . "'"
. " IDENTIFIED WITH " . $_REQUEST['pw_hash']
. " IDENTIFIED WITH "
. $_REQUEST['authentication_plugin']
. " BY '";
} else {
$query_prefix = "ALTER USER '"
@ -1919,28 +1961,64 @@ function PMA_updatePassword($err_url, $username, $hostname)
$local_query = $query_prefix
. PMA_Util::sqlAddSlashes($_POST['pma_pw']) . "'";
} else {
if (! empty($_REQUEST['pw_hash'])
&& $_REQUEST['pw_hash'] == 'mysql_old_password'
) {
$hashing_function = 'OLD_PASSWORD';
} elseif (! empty($_REQUEST['pw_hash'])
&& $_REQUEST['pw_hash'] == 'sha256_password'
) {
$hashing_function = 'PASSWORD';
} else if ((PMA_Util::getServerType() == 'MySQL'
&& PMA_MYSQL_INT_VERSION >= 50507)
|| (PMA_Util::getServerType() == 'MariaDB'
&& PMA_MYSQL_INT_VERSION >= 50200)
) {
// Backup the old value, to be reset later
$row = $GLOBALS['dbi']->fetchSingleRow(
'SELECT @@old_passwords;'
);
$orig_value = $row['@@old_passwords'];
// Backup the old value, to be reset later
$row = $GLOBALS['dbi']->fetchSingleRow(
'SELECT @@old_passwords;'
);
$orig_value = $row['@@old_passwords'];
if (isset($_REQUEST['authentication_plugin'])
&& $_REQUEST['authentication_plugin'] == 'mysql_native_password'
) {
// Set the hashing method used by PASSWORD()
// to be 'mysql_native_password' type
$GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
} else if (isset($_REQUEST['authentication_plugin'])
&& $_REQUEST['authentication_plugin'] == 'sha256_password'
) {
// Set the hashing method used by PASSWORD()
// to be 'sha256_password' type
$GLOBALS['dbi']->tryQuery('SET old_passwords = 2;');
} else {
$hashing_function = 'PASSWORD';
$GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
}
$update_plugin_query = "UPDATE `mysql`.`user` SET"
. " `plugin` = '" . $_REQUEST['authentication_plugin'] . "'"
. " WHERE `User` = '" . $username . "' AND Host = '"
. $hostname . "';";
// Update the plugin for the user
$GLOBALS['dbi']->tryQuery($update_plugin_query)
or PMA_Util::mysqlDie(
$GLOBALS['dbi']->getError(),
$update_plugin_query,
false, $err_url
);
$sql_query = 'SET PASSWORD FOR \''
. PMA_Util::sqlAddSlashes($username)
. '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\' = '
. (($_POST['pma_pw'] == '')
? '\'\''
: $hashing_function . '(\''
. preg_replace('@.@s', '*', $_POST['pma_pw']) . '\')');
$local_query = 'SET PASSWORD FOR \''
. PMA_Util::sqlAddSlashes($username)
. '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\' = '
. (($_POST['pma_pw'] == '') ? '\'\'' : $hashing_function
. '(\'' . PMA_Util::sqlAddSlashes($_POST['pma_pw']) . '\')');
} else {
if (isset($_REQUEST['authentication_plugin'])
&& $_REQUEST['authentication_plugin'] == 'mysql_native_password'
) {
$GLOBALS['dbi']->tryQuery('SET `old_passwords` = 0;');
}
$sql_query = 'SET PASSWORD FOR \''
. PMA_Util::sqlAddSlashes($username)
. '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\' = '

View File

@ -144,10 +144,16 @@ function PMA_changePassword($password, $message, $change_password_message)
$curr_user = $row['user'];
list($username, $hostname) = explode('@', $curr_user);
if (PMA_Util::getServerType() === 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
$serverType = PMA_Util::getServerType();
if (isset($_REQUEST['pw_hash']) && ! empty($_REQUEST['pw_hash'])) {
$orig_auth_plugin = $_REQUEST['pw_hash'];
if ($serverType === 'MySQL'
&& PMA_MYSQL_INT_VERSION >= 50706
) {
if (isset($_REQUEST['authentication_plugin'])
&& ! empty($_REQUEST['authentication_plugin'])
) {
$orig_auth_plugin = $_REQUEST['authentication_plugin'];
} else {
$orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
'change', $username, $hostname
@ -158,11 +164,15 @@ function PMA_changePassword($password, $message, $change_password_message)
. '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
. (($password == '') ? '\'\'' : '\'***\'');
} else {
// For MySQL versions 5.6.6+,
// For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
// explicitly set value of `old_passwords` so that
// it does not give an error while using
// the PASSWORD() function
if (PMA_MYSQL_INT_VERSION >= 50606) {
if (($serverType == 'MySQL'
&& PMA_MYSQL_INT_VERSION >= 50507)
|| ($serverType == 'MariaDB'
&& PMA_MYSQL_INT_VERSION >= 50200)
) {
$orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
'change', $username, $hostname
);