View | Details | Raw Unified | Return to bug 12617
Collapse All | Expand All

(-)a/Koha/AuthUtils.pm (-10 / +23 lines)
Lines 23-28 use Encode qw( encode is_utf8 ); Link Here
23
use Fcntl qw/O_RDONLY/; # O_RDONLY is used in generate_salt
23
use Fcntl qw/O_RDONLY/; # O_RDONLY is used in generate_salt
24
use List::MoreUtils qw/ any /;
24
use List::MoreUtils qw/ any /;
25
use String::Random qw( random_string random_regex );
25
use String::Random qw( random_string random_regex );
26
use YAML qw/Load/;
26
27
27
use C4::Context;
28
use C4::Context;
28
use Koha::Patron::Categories;
29
use Koha::Patron::Categories;
Lines 235-251 Returns correct minPasswordLength Link Here
235
sub min_password_length {
236
sub min_password_length {
236
    my $categorycode = shift;
237
    my $categorycode = shift;
237
238
238
    my $categoryinfo = Koha::Patron::Categories->find($categorycode);
239
    my $password_lengths = C4::Context->preference('PasswordLengthsForCategories') || [] ;
239
    my $passwordpolicy = $categoryinfo ? $categoryinfo->passwordpolicy : undef;
240
    if($password_lengths){
240
    my $minpasslen;
241
        $password_lengths = eval { YAML::Load(
241
    if ($passwordpolicy eq "complex") {
242
            Encode::encode(
242
        $minpasslen = C4::Context->preference("minComplexPasswordLength");
243
            'UTF-8',
243
    }elsif ($passwordpolicy eq "alphanumeric") {
244
            $password_lengths,
244
        $minpasslen = C4::Context->preference("minAlnumPasswordLength");
245
            Encode::FB_CROAK
245
    }else {
246
        ))};
246
        $minpasslen = C4::Context->preference("minPasswordLength");
247
248
        return (0, "YAMLERROR", "There's a problem in your 'PasswordLengthsForCategories': $@.") if $@;
247
    }
249
    }
248
    return $minpasslen;
250
251
    my $minPasswordLength;
252
253
    foreach my $password_length (@{$password_lengths}){
254
        if(grep( $_ eq $categorycode, @{$password_length->{'categories'}})){
255
            $minPasswordLength = $password_length->{'minlength'};
256
        }
257
    }
258
259
    $minPasswordLength = C4::Context->preference("minPasswordLength") unless $minPasswordLength;
260
261
    return $minPasswordLength;
249
}
262
}
250
263
251
264
(-)a/installer/data/mysql/atomicupdate/Bug-12617-Add-syspref-PasswordLengthsForCategories.perl (+19 lines)
Line 0 Link Here
1
$DBversion = 'XXX';  # will be replaced by the RM
2
if( CheckVersion( $DBversion ) ) {
3
    $dbh->do(
4
        "INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES ('PasswordLengthsForCategories', '', NULL, 'Used to determine password lengths for different patron categories in a YAML format.', 'textarea')"
5
    );
6
    $dbh->do(
7
        "DELETE FROM systempreferences WHERE variable = 'minAlnumPasswordLengt'"
8
    );
9
    $dbh->do(
10
        "DELETE FROM systempreferences WHERE variable = 'minComplexPasswordLength'"
11
    );
12
    $dbh->do(
13
        "UPDATE systempreferences SET explanation = 'Specify the minimum length of a patron/staff password' where variable = 'minPasswordLength'"
14
    );
15
16
    # Always end with this (adjust the bug info)
17
    SetVersion( $DBversion );
18
    print "Upgrade to $DBversion done (Bug 12617 - Add syspref PasswordLengthsForCategories)\n";
19
}
(-)a/installer/data/mysql/sysprefs.sql (+1 lines)
Lines 482-487 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
482
('OverdueNoticeCalendar',0,NULL,'Take the calendar into consideration when generating overdue notices','YesNo'),
482
('OverdueNoticeCalendar',0,NULL,'Take the calendar into consideration when generating overdue notices','YesNo'),
483
('OverduesBlockCirc','noblock','noblock|confirmation|block','When checking out an item should overdues block checkout, generate a confirmation dialogue, or allow checkout','Choice'),
483
('OverduesBlockCirc','noblock','noblock|confirmation|block','When checking out an item should overdues block checkout, generate a confirmation dialogue, or allow checkout','Choice'),
484
('OverduesBlockRenewing','allow','allow|blockitem|block','If any of patron checked out documents is late, should renewal be allowed, blocked only on overdue items or blocked on whatever checked out document','Choice'),
484
('OverduesBlockRenewing','allow','allow|blockitem|block','If any of patron checked out documents is late, should renewal be allowed, blocked only on overdue items or blocked on whatever checked out document','Choice'),
485
('PasswordLengthsForCategories', '', NULL, 'Used to determine password lengths for different patron categories in a YAML format.', 'textarea')
485
('PatronAnonymizeDelay','',NULL,'Delay for anonymizing patrons', 'Integer'),
486
('PatronAnonymizeDelay','',NULL,'Delay for anonymizing patrons', 'Integer'),
486
('PatronAutoComplete','1','Try|Don\'t try','to guess the patron being entered while typing a patron search for circulation or patron search. Only returns the first 10 results at a time.','YesNo'),
487
('PatronAutoComplete','1','Try|Don\'t try','to guess the patron being entered while typing a patron search for circulation or patron search. Only returns the first 10 results at a time.','YesNo'),
487
('patronimages','0',NULL,'Enable patron images for the Staff Client','YesNo'),
488
('patronimages','0',NULL,'Enable patron images for the Staff Client','YesNo'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref (+12 lines)
Lines 367-369 Patrons: Link Here
367
             location: "Location"
367
             location: "Location"
368
             itemcallnumber: "Item's callnumber"
368
             itemcallnumber: "Item's callnumber"
369
             ccode: "Collection code"
369
             ccode: "Collection code"
370
     - 
371
         - pref: PasswordLengthsForCategories
372
           type: textarea
373
           class: code
374
         - Define password lengths for different patron categories.
375
         - <br/>E.g
376
         - "<br/>-"
377
         - "<br/>&nbsp;categories: [PT, K]"
378
         - "<br/>&nbsp;minlength: 7"
379
         - "<br/>-"
380
         - "<br/>&nbsp;categories: [S]"
381
         - "<br/>&nbsp;minlength: 4"
(-)a/t/AuthUtils.t (-8 / +18 lines)
Lines 70-78 my $category_complex = $builder->build({ Link Here
70
    },
70
    },
71
});
71
});
72
72
73
t::lib::Mocks::mock_preference('minAlnumPasswordLength', 5);
74
t::lib::Mocks::mock_preference('minComplexPasswordLength', 6);
75
76
subtest 'is_password_valid' => sub {
73
subtest 'is_password_valid' => sub {
77
    plan tests => 13;
74
    plan tests => 13;
78
75
Lines 107-113 subtest 'is_password_valid' => sub { Link Here
107
    subtest 'password policies' => sub {
104
    subtest 'password policies' => sub {
108
105
109
        t::lib::Mocks::mock_preference('RequireStrongPassword', 0);
106
        t::lib::Mocks::mock_preference('RequireStrongPassword', 0);
110
        t::lib::Mocks::mock_preference('minPasswordLength', 4);
107
        t::lib::Mocks::mock_preference('minPasswordLength', 3);
108
109
        my $YAML ='
110
        -
111
         categories: ['.$category_simple->{categorycode}.']
112
         minlength: 4
113
        -
114
         categories: ['.$category_alpha->{categorycode}.']
115
         minlength: 5
116
        -
117
         categories: ['.$category_complex->{categorycode}.']
118
         minlength: 6
119
        ';
120
121
        t::lib::Mocks::mock_preference('PasswordLengthsForCategories', $YAML);
111
122
112
        #test simplenumeric password policy
123
        #test simplenumeric password policy
113
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '1234', $category_simple->{categorycode} );
124
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '1234', $category_simple->{categorycode} );
Lines 118-124 subtest 'is_password_valid' => sub { Link Here
118
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '!234', $category_simple->{categorycode} );
129
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '!234', $category_simple->{categorycode} );
119
        is ( $is_valid, 0, 'simplenumeric password should not contain non-special characters' );
130
        is ( $is_valid, 0, 'simplenumeric password should not contain non-special characters' );
120
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123', $category_simple->{categorycode} );
131
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123', $category_simple->{categorycode} );
121
        is ( $is_valid, 0, 'simplenumeric password follows minPasswordLength syspref' );
132
        is ( $is_valid, 0, 'simplenumeric password follows PasswordLengthsForCategories syspref' );
122
133
123
        #test alphanumeric
134
        #test alphanumeric
124
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A1234', $category_alpha->{categorycode} );
135
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A1234', $category_alpha->{categorycode} );
Lines 127-133 subtest 'is_password_valid' => sub { Link Here
127
        is ( $is_valid, 0, 'alphanumeric password must contain at least one uppercase character' );
138
        is ( $is_valid, 0, 'alphanumeric password must contain at least one uppercase character' );
128
        is($error, 'alpha_policy_mismatch', 'error "alpha_policy_mismatch" raised');
139
        is($error, 'alpha_policy_mismatch', 'error "alpha_policy_mismatch" raised');
129
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A123', $category_alpha->{categorycode} );
140
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A123', $category_alpha->{categorycode} );
130
        is ( $is_valid, 0, 'alphanumeric password follows minAlnumPasswordLength syspref');
141
        is ( $is_valid, 0, 'alphanumeric password follows PasswordLengthsForCategories syspref');
131
142
132
        #test complex
143
        #test complex
133
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'As!123', $category_complex->{categorycode} );
144
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'As!123', $category_complex->{categorycode} );
Lines 136-142 subtest 'is_password_valid' => sub { Link Here
136
        is ( $is_valid, 0, 'complex password must contain numbers, lower and uppercase characters and special characters' );
147
        is ( $is_valid, 0, 'complex password must contain numbers, lower and uppercase characters and special characters' );
137
        is($error, 'complex_policy_mismatch', 'error "complex_policy_mismatch" raised');
148
        is($error, 'complex_policy_mismatch', 'error "complex_policy_mismatch" raised');
138
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'As!12', $category_complex->{categorycode});
149
        ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'As!12', $category_complex->{categorycode});
139
        is ( $is_valid, 0, 'complex password follows minComplexPasswordLength syspref' );
150
        is ( $is_valid, 0, 'complex password follows PasswordLengthsForCategories syspref' );
140
    }
151
    }
141
};
152
};
142
153
143
- 

Return to bug 12617