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 |
- |
|
|