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

(-)a/Koha/Exceptions.pm (+5 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',
(-)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::Database;
28
use Koha::Database;
29
use Koha::DateUtils;
29
use Koha::DateUtils;
30
use Koha::Holds;
30
use Koha::Holds;
31
use Koha::Exceptions::Password;
31
use Koha::Issues;
32
use Koha::Issues;
32
use Koha::OldIssues;
33
use Koha::OldIssues;
33
use Koha::Patron::Categories;
34
use Koha::Patron::Categories;
Lines 465-474 sub change_password_to { Link Here
465
466
466
    my $min_length = C4::Context->preference("minPasswordLength");
467
    my $min_length = C4::Context->preference("minPasswordLength");
467
    if ($min_length > length($cleartext_password)) {
468
    if ($min_length > length($cleartext_password)) {
468
        return (undef, "Password is too short. Minimum length: $min_length.");
469
        Koha::Exceptions::Password::TooShort->throw(
470
            "Password is too short. Minimum length: $min_length"
471
        );
469
    }
472
    }
470
    if ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
473
    elsif ($cleartext_password =~ m|^\s+| or $cleartext_password =~ m|\s+$|) {
471
        return (undef, "Password cannot contain trailing whitespaces.");
474
        Koha::Exceptions::Password::TrailingWhitespaces->throw(
475
            "Password cannot contain trailing whitespaces."
476
        );
472
    }
477
    }
473
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
478
    my $hashed_password = Koha::AuthUtils::hash_password($cleartext_password);
474
    $self->set({ password => $hashed_password })->store;
479
    $self->set({ password => $hashed_password })->store;
(-)a/Koha/REST/V1/Patron.pm (-22 / +51 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
        $patron = Koha::Patrons->find($args->{borrowernumber});
56
    my $haspermission = haspermission($user->userid, {borrowers => 1});
61
        $user = $c->stash('koha.user');
57
    unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}
62
58
            || $haspermission) {
63
        my $OpacPasswordChange = C4::Context->preference("OpacPasswordChange");
59
        return $c->$cb({ error => "OPAC password change is disabled" }, 403);
64
        my $haspermission = haspermission($user->userid, {borrowers => 1});
65
        unless ($OpacPasswordChange && $user->borrowernumber == $args->{borrowernumber}) {
66
            Koha::Exceptions::BadSystemPreference->throw(
67
                preference => 'OpacPasswordChange'
68
            ) unless $haspermission;
69
        }
70
71
        my $pw = $args->{'body'};
72
        my $dbh = C4::Context->dbh;
73
        unless ($haspermission || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
74
            Koha::Exceptions::Password::Invalid->throw;
75
        }
76
        $patron->change_password_to($pw->{'new_password'});
77
        return $c->$cb({}, 200);
60
    }
78
    }
61
    return $c->$cb({ error => "Patron not found." }, 404) unless $patron;
79
    catch {
62
80
        if (not defined $patron) {
63
    my $pw = $args->{'body'};
81
            return $c->$cb({ error => "Patron not found." }, 404);
64
    my $dbh = C4::Context->dbh;
82
        }
65
    unless ($haspermission
83
        elsif (not defined $user) {
66
            || checkpw_internal($dbh, $patron->userid, $pw->{'current_password'})) {
84
            return $c->$cb({ error => "User must be defined." }, 500);
67
        return $c->$cb({ error => "Wrong current password." }, 400);
85
        }
68
    }
86
69
87
        die $_ unless blessed $_ && $_->can('rethrow');
70
    my ($success, $errmsg) = $patron->change_password_to($pw->{'new_password'});
88
        if ($_->isa('Koha::Exceptions::Password::Invalid')) {
71
    if ($errmsg) {
89
            return $c->$cb({ error => "Wrong current password." }, 400);
72
        return $c->$cb({ error => $errmsg }, 400);
90
        }
91
        elsif ($_->isa('Koha::Exceptions::Password::TooShort')) {
92
            return $c->$cb({ error => $_->error }, 400);
93
        }
94
        elsif ($_->isa('Koha::Exceptions::Password::TrailingWhitespaces')) {
95
            return $c->$cb({ error => $_->error }, 400);
96
        }
97
        elsif ($_->isa('Koha::Exceptions::BadSystemPreference')
98
               && $_->preference eq 'OpacPasswordChange') {
99
            return $c->$cb({ error => "OPAC password change is disabled" }, 403);
100
        }
101
        else {
102
            return $c->$cb({ error => "Something went wrong. $_" }, 500);
103
        }
73
    }
104
    }
74
    return $c->$cb({}, 200);
75
}
105
}
76
106
77
1;
107
1;
78
- 

Return to bug 17006