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

(-)a/Koha/Exceptions/Password.pm (+72 lines)
Line 0 Link Here
1
package Koha::Exceptions::Password;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Exception::Class (
21
22
    'Koha::Exceptions::Password' => {
23
        description => 'Something went wrong!',
24
    },
25
    'Koha::Exceptions::Password::Invalid' => {
26
        isa => 'Koha::Exceptions::Password',
27
        description => 'Invalid password'
28
    },
29
    'Koha::Exceptions::Password::TooShort' => {
30
        isa => 'Koha::Exceptions::Password',
31
        description => 'Password is too short',
32
        fields => ['length','min_length']
33
    },
34
    'Koha::Exceptions::Password::TooWeak' => {
35
        isa => 'Koha::Exceptions::Password',
36
        description => 'Password is too weak'
37
    },
38
    'Koha::Exceptions::Password::TrailingWhitespaces' => {
39
        isa => 'Koha::Exceptions::Password',
40
        description => 'Password contains trailing whitespace(s)'
41
    }
42
);
43
44
=head1 NAME
45
46
Koha::Exceptions::Password - Base class for password exceptions
47
48
=head1 Exceptions
49
50
=head2 Koha::Exceptions::Password
51
52
Generic password exception
53
54
=head2 Koha::Exceptions::Password::Invalid
55
56
The supplied password is invalid.
57
58
=head2 Koha::Exceptions::Password::TooShort
59
60
Password is too short.
61
62
=head2 Koha::Exceptions::Password::TooWeak
63
64
Password is too weak.
65
66
=head2 Koha::Exceptions::Password::TrailingWhitespaces
67
68
Password contains trailing spaces, which is forbidden.
69
70
=cut
71
72
1;
(-)a/Koha/Patron.pm (-1 / +58 lines)
Lines 33-38 use Koha::AuthUtils; Link Here
33
use Koha::Checkouts;
33
use Koha::Checkouts;
34
use Koha::Database;
34
use Koha::Database;
35
use Koha::DateUtils;
35
use Koha::DateUtils;
36
use Koha::Exceptions::Password;
36
use Koha::Holds;
37
use Koha::Holds;
37
use Koha::Old::Checkouts;
38
use Koha::Old::Checkouts;
38
use Koha::Patron::Categories;
39
use Koha::Patron::Categories;
Lines 702-707 sub update_password { Link Here
702
    return $digest;
703
    return $digest;
703
}
704
}
704
705
706
=head3 set_password
707
708
    $patron->set_password( $plain_text_password );
709
710
Set the patron's password.
711
712
=head4 Exceptions
713
714
The passed string is validated against the current password enforcement policy.
715
Exceptions are thrown if the password is not good enough.
716
717
=over 4
718
719
=item Koha::Exceptions::Password::TooShort
720
721
=item Koha::Exceptions::Password::TrailingWhitespaces
722
723
=item Koha::Exceptions::Password::TooWeak
724
725
=back
726
727
=cut
728
729
sub set_password {
730
    my ( $self, $password ) = @_;
731
732
    my $min_length = C4::Context->preference('minPasswordLength');
733
    $min_length = 3 if not $min_length or $min_length < 3;
734
735
    my $password_length = length($password);
736
    Koha::Exceptions::Password::TooShort->throw(
737
        { length => $password_length, min_length => $min_length } )
738
        if $password_length < $min_length;
739
740
    Koha::Exceptions::Password::TrailingWhitespaces->throw(
741
        "Password contains trailing spaces, which is forbidden.")
742
        if $password =~ m[^\s|\s$];
743
744
    if ( C4::Context->preference('RequireStrongPassword') ) {
745
        Koha::Exceptions::Password::TooWeak->throw()
746
            if $password !~ m|(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{$min_length,}|;
747
    }
748
749
    my $digest = Koha::AuthUtils::hash_password($password);
750
    $self->update(
751
        {   password       => $digest,
752
            login_attempts => 0,
753
        }
754
    );
755
756
    logaction( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" )
757
        if C4::Context->preference("BorrowersLog");
758
759
    return $self;
760
}
761
762
705
=head3 renew_account
763
=head3 renew_account
706
764
707
my $new_expiry_date = $patron->renew_account
765
my $new_expiry_date = $patron->renew_account
708
- 

Return to bug 21178