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

(-)a/Koha/Exceptions.pm (+5 lines)
Lines 13-18 use Exception::Class ( Link Here
13
        description => 'Bad parameter was given',
13
        description => 'Bad parameter was given',
14
        fields => ["parameter"],
14
        fields => ["parameter"],
15
    },
15
    },
16
    'Koha::Exceptions::BadSystemPreference' => {
17
        isa => 'Koha::Exceptions::Exception',
18
        description => 'System preference value is incomprehensible',
19
        fields => ['preference'],
20
    },
16
    'Koha::Exceptions::DuplicateObject' => {
21
    'Koha::Exceptions::DuplicateObject' => {
17
        isa => 'Koha::Exceptions::Exception',
22
        isa => 'Koha::Exceptions::Exception',
18
        description => 'Same object already exists',
23
        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 725-734 sub change_password_to { Link Here
725
726
726
    my $min_length = C4::Context->preference("minPasswordLength");
727
    my $min_length = C4::Context->preference("minPasswordLength");
727
    if ($min_length > length($cleartext_password)) {
728
    if ($min_length > length($cleartext_password)) {
728
        return (undef, "Password is too short. Minimum length: $min_length.");
729
        Koha::Exceptions::Password::TooShort->throw(
730
            "Password is too short. Minimum length: $min_length"
731
        );
729
    }
732
    }
730
    if ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
733
    elsif ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
731
        return (undef, "Password cannot contain trailing whitespaces.");
734
        Koha::Exceptions::Password::TrailingWhitespaces->throw(
735
            "Password cannot contain trailing whitespaces."
736
        );
732
    }
737
    }
733
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
738
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
734
    $self->set({ password => $hashed_password })->store;
739
    $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 = shift->openapi->valid_input or return;
32
    my $c = shift->openapi->valid_input or return;
28
33
Lines 46-74 sub get { Link Here
46
sub changepassword {
51
sub changepassword {
47
    my ($c, $args, $cb) = @_;
52
    my ($c, $args, $cb) = @_;
48
53
49
    my $user = $c->stash('koha.user');
54
    my $patron;
50
    my $patron = Koha::Patrons->find($args->{borrowernumber});
55
    my $user;
51
56
    try {
52
    my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
57
53
    my $haspermission = haspermission($user->userid, {borrowers => 1});
58
        $patron = Koha::Patrons->find($args->{borrowernumber});
54
    unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}
59
        $user = $c->stash('koha.user');
55
            || $haspermission) {
60
56
        return $c->$cb({ error => "OPAC password change is disabled" }, 403);
61
        my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
62
        my $haspermission = haspermission($user->userid, {borrowers => 1});
63
        unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}) {
64
            Koha::Exceptions::BadSystemPreference->throw(
65
                preference => 'OpacPasswordChange'
66
            ) unless $haspermission;
67
        }
68
69
        my $pw = $args->{'body'};
70
        my $dbh = C4::Context->dbh;
71
        unless ($haspermission || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
72
            Koha::Exceptions::Password::Invalid->throw;
73
        }
74
        $patron->change_password_to($pw->{'new_password'});
75
        return $c->$cb({}, 200);
57
    }
76
    }
58
    return $c->$cb({ error => "Patron not found." }, 404) unless $patron;
77
    catch {
59
78
        if (not defined $patron) {
60
    my $pw = $args->{'body'};
79
            return $c->$cb({ error => "Patron not found." }, 404);
61
    my $dbh = C4::Context->dbh;
80
        }
62
    unless ($haspermission
81
        elsif (not defined $user) {
63
            || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
82
            return $c->$cb({ error => "User must be defined." }, 500);
64
        return $c->$cb({ error => "Wrong current password." }, 400);
83
        }
65
    }
84
66
85
        die $_ unless blessed $_ && $_->can('rethrow');
67
    my ($success, $errmsg) = $patron->change_password_to($pw->{'new_password'});
86
        if ($_->isa('Koha::Exceptions::Password::Invalid')) {
68
    if ($errmsg) {
87
            return $c->$cb({ error => "Wrong current password." }, 400);
69
        return $c->$cb({ error => $errmsg }, 400);
88
        }
89
        elsif ($_->isa('Koha::Exceptions::Password::TooShort')) {
90
            return $c->$cb({ error => $_->error }, 400);
91
        }
92
        elsif ($_->isa('Koha::Exceptions::Password::TrailingWhitespaces')) {
93
            return $c->$cb({ error => $_->error }, 400);
94
        }
95
        elsif ($_->isa('Koha::Exceptions::BadSystemPreference')
96
               && $_->preference eq 'OpacPasswordChange') {
97
            return $c->$cb({ error => "OPAC password change is disabled" }, 403);
98
        }
99
        else {
100
            return $c->$cb({ error => "Something went wrong. $_" }, 500);
101
        }
70
    }
102
    }
71
    return $c->$cb({}, 200);
72
}
103
}
73
104
74
1;
105
1;
75
- 

Return to bug 17006