Skip to content

Commit

Permalink
NEW Add parameter replaceambiguouschars on getRandomPassword function
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Dec 21, 2018
1 parent 44a6644 commit 8a70cf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 12 additions & 3 deletions htdocs/core/lib/security2.lib.php
Expand Up @@ -443,11 +443,12 @@ function encodedecode_dbpassconf($level=0)
/**
* Return a generated password using default module
*
* @param boolean $generic true=Create generic password (32 chars/numbers), false=Use the configured password generation module
* @return string New value for password
* @param boolean $generic true=Create generic password (32 chars/numbers), false=Use the configured password generation module
* @param string $replaceambiguouschars Discard ambigous characters. For example array('I').
* @return string New value for password
* @see dol_hash
*/
function getRandomPassword($generic=false)
function getRandomPassword($generic=false, $replaceambiguouschars=array())
{
global $db,$conf,$langs,$user;

Expand Down Expand Up @@ -508,5 +509,13 @@ function getRandomPassword($generic=false)
unset($genhandler);
}

// Do we have to discard some alphabetic characters ?
if (is_array($replaceambiguouschars) && count($replaceambiguouschars) > 0)
{
$numbers = "ABCDEF";
$max = strlen($numbers) - 1;
$generated_password=str_replace($replaceambiguouschars, $numbers{random_int(0, $max)}, $generated_password);
}

return $generated_password;
}
10 changes: 7 additions & 3 deletions test/phpunit/SecurityTest.php
Expand Up @@ -260,17 +260,21 @@ public function testGetRandomPassword()
{
global $conf;

$genpass1=getRandomPassword(true); // Should be a string return by dol_hash (if no option set, will be md5)
$genpass1=getRandomPassword(true); // Should be a string return by dol_hash (if no option set, will be md5)
print __METHOD__." genpass1=".$genpass1."\n";
$this->assertEquals(strlen($genpass1), 32);

$genpass1=getRandomPassword(true, array('I')); // Should be a string return by dol_hash (if no option set, will be md5)
print __METHOD__." genpass1=".$genpass1."\n";
$this->assertEquals(strlen($genpass1), 32);

$conf->global->USER_PASSWORD_GENERATED='None';
$genpass2=getRandomPassword(false); // Should be an empty string
$genpass2=getRandomPassword(false); // Should return an empty string
print __METHOD__." genpass2=".$genpass2."\n";
$this->assertEquals($genpass2, '');

$conf->global->USER_PASSWORD_GENERATED='Standard';
$genpass3=getRandomPassword(false);
$genpass3=getRandomPassword(false); // Should return a password of 8 chars
print __METHOD__." genpass3=".$genpass3."\n";
$this->assertEquals(strlen($genpass3), 8);

Expand Down

0 comments on commit 8a70cf9

Please sign in to comment.