]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/passwordauth.pm
improve a string
[git.ikiwiki.info.git] / IkiWiki / Plugin / passwordauth.pm
1 #!/usr/bin/perl
2 # Ikiwiki password authentication.
3 package IkiWiki::Plugin::passwordauth;
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
9 sub import { #{{{
10         hook(type => "formbuilder_setup", id => "passwordauth",
11                 call => \&formbuilder_setup);
12         hook(type => "formbuilder", id => "passwordauth",
13                 call => \&formbuilder);
14 } # }}}
16 sub formbuilder_setup (@) { #{{{
17         my %params=@_;
19         my $form=$params{form};
20         my $session=$params{session};
21         my $cgi=$params{cgi};
23         if ($form->title eq "signin" || $form->title eq "register") {
24                 $form->field(name => "name", required => 0);
25                 $form->field(name => "password", type => "password", required => 0);
26                 
27                 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
28                         $form->field(name => "confirm_password", type => "password");
29                         $form->field(name => "email", size => 50);
30                         $form->title("register");
31                         $form->text("");
32                 }
34                 if ($form->submitted) {
35                         my $submittype=$form->submitted;
36                         # Set required fields based on how form was submitted.
37                         my %required=(
38                                 "Login" => [qw(name password)],
39                                 "Register" => [],
40                                 "Create Account" => [qw(name password confirm_password email)],
41                                 "Mail Password" => [qw(name)],
42                         );
43                         foreach my $opt (@{$required{$submittype}}) {
44                                 $form->field(name => $opt, required => 1);
45                         }
46         
47                         if ($submittype eq "Create Account") {
48                                 $form->field(
49                                         name => "confirm_password",
50                                         validate => sub {
51                                                 shift eq $form->field("password");
52                                         },
53                                 );
54                                 $form->field(
55                                         name => "email",
56                                         validate => "EMAIL",
57                                 );
58                         }
60                         # Validate password against name for Login.
61                         if ($submittype eq "Login") {
62                                 $form->field(
63                                         name => "password",
64                                         validate => sub {
65                                                 length $form->field("name") &&
66                                                 shift eq IkiWiki::userinfo_get($form->field("name"), 'password');
67                                         },
68                                 );
69                         }
70                         elsif ($submittype eq "Register" ||
71                                $submittype eq "Create Account" ||
72                                $submittype eq "Mail Password") {
73                                 $form->field(name => "password", validate => 'VALUE');
74                         }
75                         
76                         # And make sure the entered name exists when logging
77                         # in or sending email, and does not when registering.
78                         if ($submittype eq 'Create Account' ||
79                             $submittype eq 'Register') {
80                                 $form->field(
81                                         name => "name",
82                                         validate => sub {
83                                                 my $name=shift;
84                                                 length $name &&
85                                                 $name=~/$config{wiki_file_regexp}/ &&
86                                                 ! IkiWiki::userinfo_get($name, "regdate");
87                                         },
88                                 );
89                         }
90                         elsif ($submittype eq "Login" ||
91                                $submittype eq "Mail Password") {
92                                 $form->field( 
93                                         name => "name",
94                                         validate => sub {
95                                                 my $name=shift;
96                                                 length $name &&
97                                                 IkiWiki::userinfo_get($name, "regdate");
98                                         },
99                                 );
100                         }
101                 }
102                 else {
103                         # First time settings.
104                         $form->field(name => "name");
105                         if ($session->param("name")) {
106                                 $form->field(name => "name", value => $session->param("name"));
107                         }
108                 }
109         }
110         elsif ($form->title eq "preferences") {
111                 $form->field(name => "name", disabled => 1, 
112                         value => $session->param("name"), force => 1,
113                         fieldset => "login");
114                 $form->field(name => "password", type => "password",
115                         fieldset => "login");
116                 $form->field(name => "confirm_password", type => "password",
117                         fieldset => "login",
118                         validate => sub {
119                                 shift eq $form->field("password");
120                         });
121                 
122         }
125 sub formbuilder (@) { #{{{
126         my %params=@_;
128         my $form=$params{form};
129         my $session=$params{session};
130         my $cgi=$params{cgi};
131         my $buttons=$params{buttons};
133         if ($form->title eq "signin" || $form->title eq "register") {
134                 if ($form->submitted && $form->validate) {
135                         if ($form->submitted eq 'Login') {
136                                 $session->param("name", $form->field("name"));
137                                 IkiWiki::cgi_postsignin($cgi, $session);
138                         }
139                         elsif ($form->submitted eq 'Create Account') {
140                                 my $user_name=$form->field('name');
141                                 if (IkiWiki::userinfo_setall($user_name, {
142                                         'email' => $form->field('email'),
143                                         'password' => $form->field('password'),
144                                         'regdate' => time})) {
145                                         $form->field(name => "confirm_password", type => "hidden");
146                                         $form->field(name => "email", type => "hidden");
147                                         $form->text(gettext("Account creation successful. Now you can Login."));
148                                 }
149                                 else {
150                                         error(gettext("Error creating account."));
151                                 }
152                         }
153                         elsif ($form->submitted eq 'Mail Password') {
154                                 my $user_name=$form->field("name");
155                                 my $template=template("passwordmail.tmpl");
156                                 $template->param(
157                                         user_name => $user_name,
158                                         user_password => IkiWiki::userinfo_get($user_name, "password"),
159                                         wikiurl => $config{url},
160                                         wikiname => $config{wikiname},
161                                         REMOTE_ADDR => $ENV{REMOTE_ADDR},
162                                 );
163                         
164                                 eval q{use Mail::Sendmail};
165                                 error($@) if $@;
166                                 sendmail(
167                                         To => IkiWiki::userinfo_get($user_name, "email"),
168                                         From => "$config{wikiname} admin <$config{adminemail}>",
169                                         Subject => "$config{wikiname} information",
170                                         Message => $template->output,
171                                 ) or error(gettext("Failed to send mail"));
172                         
173                                 $form->text(gettext("Your password has been emailed to you."));
174                                 $form->field(name => "name", required => 0);
175                                 push @$buttons, "Mail Password";
176                         }
177                         elsif ($form->submitted eq "Register") {
178                                 @$buttons="Create Account";
179                         }
180                 }
181                 elsif ($form->submitted eq "Create Account") {
182                         @$buttons="Create Account";
183                 }
184                 else {
185                         push @$buttons, "Register", "Mail Password";
186                 }
187         }
188         elsif ($form->title eq "preferences") {
189                 if ($form->submitted eq "Save Preferences" && $form->validate) {
190                         my $user_name=$form->field('name');
191                         foreach my $field (qw(password)) {
192                                 if (defined $form->field($field)) {
193                                         IkiWiki::userinfo_set($user_name, $field, $form->field($field)) ||
194                                                 error("failed to set $field");
195                                 }
196                         }
197                 }
198         }
199         
200         IkiWiki::printheader($session);
201         print IkiWiki::misctemplate($form->title, $form->render(submit => $buttons));
202 } #}}}