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

(-)a/Koha/Exceptions.pm (+5 lines)
Lines 8-13 use Exception::Class ( Link Here
8
    'Koha::Exceptions::Exception' => {
8
    'Koha::Exceptions::Exception' => {
9
        description => 'Something went wrong!',
9
        description => 'Something went wrong!',
10
    },
10
    },
11
    'Koha::Exceptions::BadSystemPreference' => {
12
        isa => 'Koha::Exceptions::Exception',
13
        description => 'System preference value is incomprehensible',
14
        fields => ['preference'],
15
    },
11
    'Koha::Exceptions::DuplicateObject' => {
16
    'Koha::Exceptions::DuplicateObject' => {
12
        isa => 'Koha::Exceptions::Exception',
17
        isa => 'Koha::Exceptions::Exception',
13
        description => 'Same object already exists',
18
        description => 'Same object already exists',
(-)a/Koha/Exceptions/Password.pm (+24 lines)
Line 0 Link Here
1
package Koha::Exceptions::Password;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Password' => {
8
        description => 'Something went wrong!',
9
    },
10
    'Koha::Exceptions::Password::Invalid' => {
11
        isa => 'Koha::Exceptions::Password',
12
        description => 'Invalid password',
13
    },
14
    'Koha::Exceptions::Password::TooShort' => {
15
        isa => 'Koha::Exceptions::Password',
16
        description => 'Password is too short',
17
    },
18
    'Koha::Exceptions::Password::TrailingWhitespaces' => {
19
        isa => 'Koha::Exceptions::Password',
20
        description => 'Password contains trailing whitespace(s)',
21
    }
22
);
23
24
1;
(-)a/Koha/Patron.pm (-3 / +8 lines)
Lines 28-33 use Koha::AuthUtils; Link Here
28
use Koha::Checkouts;
28
use Koha::Checkouts;
29
use Koha::Database;
29
use Koha::Database;
30
use Koha::DateUtils;
30
use Koha::DateUtils;
31
use Koha::Exceptions::Password;
31
use Koha::Holds;
32
use Koha::Holds;
32
use Koha::Old::Checkouts;
33
use Koha::Old::Checkouts;
33
use Koha::Patron::Categories;
34
use Koha::Patron::Categories;
Lines 670-679 sub change_password_to { Link Here
670
671
671
    my $min_length = C4::Context->preference("minPasswordLength");
672
    my $min_length = C4::Context->preference("minPasswordLength");
672
    if ($min_length > length($cleartext_password)) {
673
    if ($min_length > length($cleartext_password)) {
673
        return (undef, "Password is too short. Minimum length: $min_length.");
674
        Koha::Exceptions::Password::TooShort->throw(
675
            "Password is too short. Minimum length: $min_length"
676
        );
674
    }
677
    }
675
    if ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
678
    elsif ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
676
        return (undef, "Password cannot contain trailing whitespaces.");
679
        Koha::Exceptions::Password::TrailingWhitespaces->throw(
680
            "Password cannot contain trailing whitespaces."
681
        );
677
    }
682
    }
678
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
683
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
679
    $self->set({ password => $hashed_password })->store;
684
    $self->set({ password => $hashed_password })->store;
(-)a/Koha/REST/V1/Patron.pm (-22 / +52 lines)
Lines 21-28 use Mojo::Base 'Mojolicious::Controller'; Link Here
21
21
22
use C4::Auth qw( haspermission checkpw_internal );
22
use C4::Auth qw( haspermission checkpw_internal );
23
use C4::Context;
23
use C4::Context;
24
use Koha::Exceptions;
25
use Koha::Exceptions::Password;
24
use Koha::Patrons;
26
use Koha::Patrons;
25
27
28
use Scalar::Util qw(blessed);
29
use Try::Tiny;
30
26
sub list {
31
sub list {
27
    my ($c, $args, $cb) = @_;
32
    my ($c, $args, $cb) = @_;
28
33
Lines 49-77 sub get { Link Here
49
sub changepassword {
54
sub changepassword {
50
    my ($c, $args, $cb) = @_;
55
    my ($c, $args, $cb) = @_;
51
56
52
    my $user = $c->stash('koha.user');
57
    my $patron;
53
    my $patron = Koha::Patrons->find($args->{borrowernumber});
58
    my $user;
54
59
    try {
55
    my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
60
56
    my $haspermission = haspermission($user->userid, {borrowers => 1});
61
        $patron = Koha::Patrons->find($args->{borrowernumber});
57
    unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}
62
        $user = $c->stash('koha.user');
58
            || $haspermission) {
63
59
        return $c->$cb({ error => "OPAC password change is disabled" }, 403);
64
        my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
65
        my $haspermission = haspermission($user->userid, {borrowers => 1});
66
        unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}) {
67
            Koha::Exceptions::BadSystemPreference->throw(
68
                preference => 'OpacPasswordChange'
69
            ) unless $haspermission;
70
        }
71
72
        my $pw = $args->{'body'};
73
        my $dbh = C4::Context->dbh;
74
        unless ($haspermission || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
75
            Koha::Exceptions::Password::Invalid->throw;
76
        }
77
        $patron->change_password_to($pw->{'new_password'});
78
        return $c->$cb({}, 200);
60
    }
79
    }
61
    return $c->$cb({ error => "Patron not found." }, 404) unless $patron;
80
    catch {
62
81
        if (not defined $patron) {
63
    my $pw = $args->{'body'};
82
            return $c->$cb({ error => "Patron not found." }, 404);
64
    my $dbh = C4::Context->dbh;
83
        }
65
    unless ($haspermission
84
        elsif (not defined $user) {
66
            || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
85
            return $c->$cb({ error => "User must be defined." }, 500);
67
        return $c->$cb({ error => "Wrong current password." }, 400);
86
        }
68
    }
87
69
88
        die $_ unless blessed $_ && $_->can('rethrow');
70
    my ($success, $errmsg) = $patron->change_password_to($pw->{'new_password'});
89
        if ($_->isa('Koha::Exceptions::Password::Invalid')) {
71
    if ($errmsg) {
90
            return $c->$cb({ error => "Wrong current password." }, 400);
72
        return $c->$cb({ error => $errmsg }, 400);
91
        }
92
        elsif ($_->isa('Koha::Exceptions::Password::TooShort')) {
93
            return $c->$cb({ error => $_->error }, 400);
94
        }
95
        elsif ($_->isa('Koha::Exceptions::Password::TrailingWhitespaces')) {
96
            return $c->$cb({ error => $_->error }, 400);
97
        }
98
        elsif ($_->isa('Koha::Exceptions::BadSystemPreference')
99
               && $_->preference eq 'OpacPasswordChange') {
100
            return $c->$cb({ error => "OPAC password change is disabled" }, 403);
101
        }
102
        else {
103
            return $c->$cb({ error => "Something went wrong. $_" }, 500);
104
        }
73
    }
105
    }
74
    return $c->$cb({}, 200);
75
}
106
}
76
107
77
1;
108
1;
78
- 

Return to bug 17006