new client side Ajax admin user interface
[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.001";
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   if (keys %errors) {
82     $req->is_ajax
83       and return $class->_field_error($req, \%errors);
84     return $class->req_form($req, undef, \%errors);
85   }
86
87   $user->changepw($newpw);
88   $user->save;
89
90   $req->is_ajax
91     and return $req->json_content(success => 1);
92
93   my $r = $cgi->param('r');
94   unless ($r) {
95     $r = $req->url('menu', { m => "New password saved" });
96   }
97
98   return BSE::Template->get_refresh($r, $req->cfg);
99 }
100
101 1;