Commit e628d22a authored by sylvieg's avatar sylvieg

[NEW]password: more options to check the pattern of a password

parent 1ed31e94
......@@ -2772,6 +2772,7 @@ lib/test/attributes/AttributeTest.php -text
lib/test/attributes/RelationTest.php -text
lib/test/auth/AuthTokensTest.php -text
lib/test/auth/MembershipTest.php -text
lib/test/auth/PasswordTest.php -text
lib/test/bootstrap.php -text
lib/test/comments/CommentsTest.php -text
lib/test/comments/index.php -text
......@@ -4375,6 +4376,7 @@ templates/modules/mod-youtube.tpl -text
templates/modules/user_module.tpl -text
templates/object_perms_summary.tpl -text
templates/pagehistory.tpl -text
templates/password_help.tpl -text
templates/plugins/plugin-topfriends.tpl -text
templates/poll.tpl -text
templates/prefs/flag.tpl -text
......
......@@ -20,5 +20,20 @@ function prefs_pass_list() {
'shorthint' => tra('days'),
'hint' => tra('Use "-1" for never'),
),
'pass_chr_case' => array(
'name' => tra('Require alphabetical characters in lower and upper case'),
'type' => 'flag',
'description' => tra('Password must contain at least one alphabetical character in lower case like a and one in upper case like A.'),
),
'pass_chr_special' => array(
'name' => tra('Require special characters'),
'type' => 'flag',
'description' => tra('Password must contain at least one special character in lower case like " / $ % ? & * ( ) _ + ...'),
),
'pass_repetition' => array(
'name' => tra('Require no consecutive repetition of the same character'),
'type' => 'flag',
'description' => tra('Password must contain no consecutive repetition of the same character as 111 or aab.'),
),
);
}
......@@ -1101,6 +1101,9 @@ function get_default_prefs() {
'min_user_length' => 1,
'min_pass_length' => 5,
'pass_chr_num' => 'n',
'pass_chr_case' => 'n',
'pass_chr_special' => 'n',
'pass_repetition' => 'n',
'pass_due' => -1,
'email_due' => -1,
'unsuccessful_logins' => 20,
......
<?php
class PasswordTest extends TikiTestCase
{
function test_pass() {
global $prefs;
global $userlib;
$prefs['pass_chr_num'] = $prefs['pass_chr_case'] = $prefs['pass_chr_special'] = $prefs['pass_repetition'] = 'y';
$passwords = array('1234', 'abcd', '123abc', '123ABc', '123AAbc*');
foreach ($passwords as $pass) {
$res = $userlib->check_password_policy($pass);
$this->assertEquals("$pass=n", "$pass=".($res==''?'y':'n'));
}
$pass='123ABcd*';
$res = $userlib->check_password_policy($pass);
$this->assertEquals("$pass=y", "$pass=".($res==''?'y':'n'));
}
}
\ No newline at end of file
......@@ -2738,20 +2738,48 @@ class UsersLib extends TikiLib
*/
function check_password_policy($pass) {
global $prefs, $user;
$errors = array();
// Validate password here
if ( ( $prefs['auth_method'] != 'cas' || $user == 'admin' ) && strlen($pass) < $prefs['min_pass_length'] ) {
return tra("Password should be at least").' '.$prefs['min_pass_length'].' '.tra("characters long");
$errors[] = tra("Password should be at least").' '.$prefs['min_pass_length'].' '.tra("characters long");
}
// Check this code
if ($prefs['pass_chr_num'] == 'y') {
if (!preg_match_all("/[0-9]+/", $pass, $foo) || !preg_match_all("/[A-Za-z]+/", $pass, $foo)) {
return tra("Password must contain both letters and numbers");
$errors[] = tra("Password must contain both letters and numbers");
}
}
if ($prefs['pass_chr_case'] == 'y') {
if (!preg_match_all("/[a-z]+/", $pass, $foo) || !preg_match_all("/[A-Z]+/", $pass, $foo)) {
$errors[] = tra('Password must contain at least one alphabetical character in lower case like a and one in upper case like A.');
}
}
if ($prefs['pass_chr_special'] == 'y') {
$chars = str_split($pass);
$ok = false;
foreach ($chars as $char) {
if (!preg_match("/[0-9A-Za-z]+/", $char, $foo)) {
$ok = true;
break;
}
}
if (!$ok) $errors[] = tra('Password must contain at least one special character in lower case like " / $ % ? & * ( ) _ + ...');
}
if ($prefs['pass_repetition'] == 'y') {
$chars = str_split($pass);
$previous = '';
foreach ($chars as $char) {
if ($char == $previous) {
$errors[] = tra('Password must contain no consecutive repetition of the same character as 111 or aab');
break;
}
$previous = $char;
}
}
return "";
return empty($errors)?'': implode(' ', $errors);
}
function change_user_password($user, $pass, $pass_first_login=false) {
......
{if $prefs.min_pass_length > 1}<div class="highlight"><em>{tr}Minimum {$prefs.min_pass_length} characters long{/tr}</em></div>{/if}
{if $prefs.pass_chr_num eq 'y'}<div class="highlight"><em>{tr}Password must contain both letters and numbers{/tr}</em></div>{/if}
{if $prefs.pass_chr_case eq 'y'}<div class="highlight"><em>{tr}Password must contain at least one alphabetical character in lower case like a and one in upper case like A.{/tr}</em></div>{/if}
{if $prefs.pass_chr_special eq 'y'}<div class="highlight"><em>{tr}Password must contain at least one special character in lower case like " / $ % ? & * ( ) _ + ...{/tr}</em></div>{/if}
{if $prefs.pass_chr_repetition eq 'y'}<div class="highlight"><em>{tr}Password must contain no consecutive repetition of the same character as 111 or aab{/tr}</em></div>{/if}
\ No newline at end of file
......@@ -11,8 +11,7 @@
<div id="mypassword_bar" style="font-size: 5px; height: 2px; width: 0px;"></div>
</div>
{if $prefs.ajax_xajax ne 'y'}
{if $prefs.min_pass_length > 1}<div class="highlight"><em>{tr}Minimum {$prefs.min_pass_length} characters long{/tr}</em></div>{/if}
{if $prefs.pass_chr_num eq 'y'}<div class="highlight"><em>{tr}Password must contain both letters and numbers{/tr}</em></div>{/if}
{include file='password_help.tpl'}
{/if}
</td>
</tr>
......
......@@ -127,6 +127,9 @@
{preference name=feature_crypt_passwords}
{preference name=change_password}
{preference name=pass_chr_num}
{preference name=pass_chr_case}
{preference name=pass_chr_special}
{preference name=pass_repetition}
{preference name=min_pass_length}
{preference name=pass_due}
</fieldset>
......
......@@ -385,12 +385,7 @@
<div id="mypassword_bar" style="font-size: 5px; height: 2px; width: 0px;"></div>
</div>
<br />
{if $prefs.min_pass_length > 1}
<em>{tr}Minimum {$prefs.min_pass_length} characters long{/tr}</em>.
{/if}
{if $prefs.pass_chr_num eq 'y'}
<em>{tr}Password must contain both letters and numbers{/tr}</em>.
{/if}
{include file='password_help.tpl'}
</td>
</tr>
<tr>
......
......@@ -73,10 +73,7 @@
</div>
<input type="password" name="pass" id="pass1" onkeypress="regCapsLock(event)" onkeyup="runPassword(this.value, 'mypassword');{if $prefs.ajax_xajax eq 'y'}check_pass();{/if}" />
{if $prefs.ajax_xajax ne 'y'}
{if $prefs.min_pass_length > 1}
<div class="highlight"><em>{tr}Minimum {$prefs.min_pass_length} characters long{/tr}</em></div>{/if}
{if $prefs.pass_chr_num eq 'y'}
<div class="highlight"><em>{tr}Password must contain both letters and numbers{/tr}</em></div>{/if}
{include file='password_help.tpl'}
{/if}
</td>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment