]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/ChangePW.pm
add version numbers to all modules
[bse.git] / site / cgi-bin / modules / BSE / ChangePW.pm
1 package BSE::ChangePW;
2 use strict;
3 use BSE::Util::Tags qw(tag_error_img);
4 use BSE::Util::HTML;
5 use base 'BSE::UI::AdminDispatch';
6
7 our $VERSION = "1.000";
8
9 my %actions =
10   (
11    form=>1,
12    change=>1
13   );
14
15 sub actions {
16   \%actions;
17 }
18
19 sub rights {
20   +{}
21 }
22
23 sub default_action {
24   'form';
25 }
26
27 sub req_form {
28   my ($class, $req, $msg, $errors) = @_;
29
30   $msg ||= $req->cgi->param('m');
31   $errors ||= +{};
32
33   if ($msg) {
34     $msg = escape_html($msg);
35   }
36   else {
37     $msg = join "<br />", map escape_html($_), values %$errors;
38   }
39
40   my %acts;
41   %acts =
42     (
43      BSE::Util::Tags->basic(undef, $req->cgi, $req->cfg),
44      BSE::Util::Tags->admin(undef, $req->cfg),
45      BSE::Util::Tags->secure($req),
46      message => $msg,
47      error_img => [ \&tag_error_img, $req->cfg, $errors ],
48     );
49
50   return $req->dyn_response('admin/changepw', \%acts);
51 }
52
53 sub req_change {
54   my ($class, $req) = @_;
55
56   my $cgi = $req->cgi;
57   my $oldpw = $cgi->param('oldpassword');
58   my $newpw = $cgi->param('newpassword');
59   my $confirm = $cgi->param('confirm');
60
61   my $user = $req->user;
62
63   my %errors;
64   if (!defined $oldpw || $oldpw eq '') {
65     $errors{oldpassword} = "Enter your current password";
66   }
67   else {
68     $user->check_password($oldpw)
69       or $errors{oldpassword} = "Your old password is incorrect";
70   }
71   if (!defined $newpw || $newpw eq '') {
72     $errors{newpassword} = "Enter a new password";
73   }
74   if (!defined $confirm || $confirm eq '') {
75     $errors{confirm} = "Enter the confirmation password";
76   }
77   if (!$errors{newpassword} && !$errors{confirm}
78       && $newpw ne $confirm) {
79     $errors{confirm} = "Confirmation password does not match new password";
80   }
81   keys %errors
82     and return $class->req_form($req, undef, \%errors);
83
84   $user->changepw($newpw);
85   $user->save;
86
87   my $r = $cgi->param('r');
88   unless ($r) {
89     $r = $req->url('menu', { m => "New password saved" });
90   }
91
92   return BSE::Template->get_refresh($r, $req->cfg);
93 }
94
95 1;