new client side Ajax admin user interface
[bse.git] / site / cgi-bin / modules / BSE / ChangePW.pm
CommitLineData
daee3409
TC
1package BSE::ChangePW;
2use strict;
3use BSE::Util::Tags qw(tag_error_img);
3f9c8a96 4use BSE::Util::HTML;
4d764c34 5use base 'BSE::UI::AdminDispatch';
daee3409 6
0c3d2fc5 7our $VERSION = "1.001";
cb7fd78d 8
daee3409
TC
9my %actions =
10 (
11 form=>1,
12 change=>1
13 );
14
4d764c34
TC
15sub actions {
16 \%actions;
17}
daee3409 18
4d764c34
TC
19sub rights {
20 +{}
21}
22
23sub default_action {
24 'form';
daee3409
TC
25}
26
27sub 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 (
daee3409 43 BSE::Util::Tags->basic(undef, $req->cgi, $req->cfg),
62533efa 44 BSE::Util::Tags->admin(undef, $req->cfg),
daee3409
TC
45 BSE::Util::Tags->secure($req),
46 message => $msg,
47 error_img => [ \&tag_error_img, $req->cfg, $errors ],
48 );
49
4d764c34 50 return $req->dyn_response('admin/changepw', \%acts);
daee3409
TC
51}
52
53sub 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 {
b190a4c1 68 $user->check_password($oldpw)
daee3409
TC
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 }
0c3d2fc5
TC
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 }
daee3409 86
b190a4c1 87 $user->changepw($newpw);
daee3409
TC
88 $user->save;
89
0c3d2fc5
TC
90 $req->is_ajax
91 and return $req->json_content(success => 1);
92
d09682dd
TC
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);
daee3409
TC
99}
100
1011;