Factorize code into a function dolJSToSetRandomPassword()

This commit is contained in:
Laurent Destailleur
2022-07-27 13:42:32 +02:00
parent a04bef1909
commit fc9b3e3d70
16 changed files with 96 additions and 185 deletions

View File

@@ -449,7 +449,7 @@ function encodedecode_dbpassconf($level = 0)
* @param array $replaceambiguouschars Discard ambigous characters. For example array('I').
* @param int $length Length of random string (Used only if $generic is true)
* @return string New value for password
* @see dol_hash()
* @see dol_hash(), dolJSToSetRandomPassword()
*/
function getRandomPassword($generic = false, $replaceambiguouschars = null, $length = 32)
{
@@ -527,3 +527,34 @@ function getRandomPassword($generic = false, $replaceambiguouschars = null, $len
return $generated_password;
}
/**
* Ouput javacript to autoset a generated password using default module into a HTML element.
*
* @param string $htmlname HTML name of element to insert key into
* @param string $htmlnameofbutton HTML name of button
* @return string HTML javascript code to set a password
* @see getRandomPassword()
*/
function dolJSToSetRandomPassword($htmlname, $htmlnameofbutton = 'generate_token')
{
global $conf;
if (!empty($conf->use_javascript_ajax)) {
print "\n".'<!-- Js code to suggest a security key --><script type="text/javascript">';
print '$(document).ready(function () {
$("#'.dol_escape_js($htmlnameofbutton).'").click(function() {
console.log("We click on the button to suggest a key");
$.get( "'.DOL_URL_ROOT.'/core/ajax/security.php", {
action: \'getrandompassword\',
generic: true,
token: \''.dol_escape_js(newToken()).'\'
},
function(result) {
$("#'.dol_escape_js($htmlname).'").val(result);
});
});
});'."\n";
print '</script>';
}
}