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