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

(-)a/Koha/Exceptions.pm (+17 lines)
Lines 9-14 use Exception::Class ( Link Here
9
        description => 'Something went wrong!',
9
        description => 'Something went wrong!',
10
    },
10
    },
11
11
12
    'Koha::Exceptions::BadSystemPreference' => {
13
        isa => 'Koha::Exceptions::Exception',
14
        description => 'System preference value is incomprehensible',
15
        fields => ['preference'],
16
    },
12
    'Koha::Exceptions::DuplicateObject' => {
17
    'Koha::Exceptions::DuplicateObject' => {
13
        isa => 'Koha::Exceptions::Exception',
18
        isa => 'Koha::Exceptions::Exception',
14
        description => 'Same object already exists',
19
        description => 'Same object already exists',
Lines 21-26 use Exception::Class ( Link Here
21
        isa => 'Koha::Exceptions::Exception',
26
        isa => 'Koha::Exceptions::Exception',
22
        description => 'A required parameter is missing'
27
        description => 'A required parameter is missing'
23
    },
28
    },
29
    'Koha::Exceptions::Password::Invalid' => {
30
        isa => 'Koha::Exceptions::Exception',
31
        description => 'Invalid password',
32
    },
33
    'Koha::Exceptions::Password::TooShort' => {
34
        isa => 'Koha::Exceptions::Exception',
35
        description => 'Password is too short',
36
    },
37
    'Koha::Exceptions::Password::TrailingWhitespaces' => {
38
        isa => 'Koha::Exceptions::Exception',
39
        description => 'Password contains trailing whitespace(s)',
40
    },
24
    # Virtualshelves exceptions
41
    # Virtualshelves exceptions
25
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
42
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
26
        isa => 'Koha::Exceptions::DuplicateObject',
43
        isa => 'Koha::Exceptions::DuplicateObject',
(-)a/Koha/Patron.pm (-3 / +8 lines)
Lines 27-32 use C4::Log; Link Here
27
use Koha::AuthUtils;
27
use Koha::AuthUtils;
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::DateUtils;
29
use Koha::DateUtils;
30
use Koha::Exceptions;
30
use Koha::Issues;
31
use Koha::Issues;
31
use Koha::OldIssues;
32
use Koha::OldIssues;
32
use Koha::Patron::Categories;
33
use Koha::Patron::Categories;
Lines 283-292 sub change_password_to { Link Here
283
284
284
    my $min_length = C4::Context->preference("minPasswordLength");
285
    my $min_length = C4::Context->preference("minPasswordLength");
285
    if ($min_length > length($cleartext_password)) {
286
    if ($min_length > length($cleartext_password)) {
286
        return (undef, "Password is too short. Minimum length: $min_length.");
287
        Koha::Exceptions::Password::TooShort->throw(
288
            "Password is too short. Minimum length: $min_length"
289
        );
287
    }
290
    }
288
    if ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
291
    elsif ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
289
        return (undef, "Password cannot contain trailing whitespaces.");
292
        Koha::Exceptions::Password::TrailingWhitespaces->throw(
293
            "Password cannot contain trailing whitespaces."
294
        );
290
    }
295
    }
291
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
296
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
292
    $self->set({ password => $hashed_password })->store;
297
    $self->set({ password => $hashed_password })->store;
(-)a/Koha/REST/V1/Patron.pm (-22 / +50 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;
24
use Koha::Patrons;
25
use Koha::Patrons;
25
26
27
use Scalar::Util qw(blessed);
28
use Try::Tiny;
29
26
sub list {
30
sub list {
27
    my ($c, $args, $cb) = @_;
31
    my ($c, $args, $cb) = @_;
28
32
Lines 49-77 sub get { Link Here
49
sub changepassword {
53
sub changepassword {
50
    my ($c, $args, $cb) = @_;
54
    my ($c, $args, $cb) = @_;
51
55
52
    my $user = $c->stash('koha.user');
56
    my $patron;
53
    my $patron = Koha::Patrons->find($args->{borrowernumber});
57
    my $user;
54
58
    try {
55
    my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
59
        $patron = Koha::Patrons->find($args->{borrowernumber});
56
    my $haspermission = haspermission($user->userid, {borrowers => 1});
60
        $user = $c->stash('koha.user');
57
    unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}
61
58
            || $haspermission) {
62
        my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
59
        return $c->$cb({ error => "OPAC password change is disabled" }, 403);
63
        my $haspermission = haspermission($user->userid, {borrowers => 1});
64
        unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}) {
65
            Koha::Exceptions::BadSystemPreference->throw(
66
                preference => 'OpacPasswordChange'
67
            ) unless $haspermission;
68
        }
69
70
        my $pw = $args->{'body'};
71
        my $dbh = C4::Context->dbh;
72
        unless ($haspermission || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
73
            Koha::Exceptions::Password::Invalid->throw;
74
        }
75
        $patron->change_password_to($pw->{'new_password'});
76
        return $c->$cb({}, 200);
60
    }
77
    }
61
    return $c->$cb({ error => "Patron not found." }, 404) unless $patron;
78
    catch {
62
79
        if (not defined $patron) {
63
    my $pw = $args->{'body'};
80
            return $c->$cb({ error => "Patron not found." }, 404);
64
    my $dbh = C4::Context->dbh;
81
        }
65
    unless ($haspermission
82
        elsif (not defined $user) {
66
            || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
83
            return $c->$cb({ error => "User must be defined." }, 500);
67
        return $c->$cb({ error => "Wrong current password." }, 400);
84
        }
68
    }
85
69
86
        die $_ unless blessed $_ && $_->can('rethrow');
70
    my ($success, $errmsg) = $patron->change_password_to($pw->{'new_password'});
87
        if ($_->isa('Koha::Exceptions::Password::Invalid')) {
71
    if ($errmsg) {
88
            return $c->$cb({ error => "Wrong current password." }, 400);
72
        return $c->$cb({ error => $errmsg }, 400);
89
        }
90
        elsif ($_->isa('Koha::Exceptions::Password::TooShort')) {
91
            return $c->$cb({ error => $_->error }, 400);
92
        }
93
        elsif ($_->isa('Koha::Exceptions::Password::TrailingWhitespaces')) {
94
            return $c->$cb({ error => $_->error }, 400);
95
        }
96
        elsif ($_->isa('Koha::Exceptions::BadSystemPreference')
97
               && $_->preference eq 'OpacPasswordChange') {
98
            return $c->$cb({ error => "OPAC password change is disabled" }, 403);
99
        }
100
        else {
101
            return $c->$cb({ error => "Something went wrong. $_" }, 500);
102
        }
73
    }
103
    }
74
    return $c->$cb({}, 200);
75
}
104
}
76
105
77
1;
106
1;
78
- 

Return to bug 17006