]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/passwordauth.pm
03000c8004bd0ae0ad5187d4cba399f5d80cc8e0
[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;
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, size => 30);
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", comment => "use FirstnameLastName");
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, value =>
112                         $session->param("name"), force => 1);
113                 $form->field(name => "password", type => "password");
114                 $form->field(name => "confirm_password", type => "password",
115                         validate => sub {
116                                 shift eq $form->field("password");
117                         });
118                 
119         }
122 sub formbuilder (@) { #{{{
123         my %params=@_;
125         my $form=$params{form};
126         my $session=$params{session};
127         my $cgi=$params{cgi};
128         my $buttons=$params{buttons};
130         if ($form->title eq "signin" || $form->title eq "register") {
131                 if ($form->submitted && $form->validate) {
132                         if ($form->submitted eq 'Login') {
133                                 $session->param("name", $form->field("name"));
134                                 IkiWiki::cgi_postsignin($cgi, $session);
135                         }
136                         elsif ($form->submitted eq 'Create Account') {
137                                 my $user_name=$form->field('name');
138                                 if (IkiWiki::userinfo_setall($user_name, {
139                                         'email' => $form->field('email'),
140                                         'password' => $form->field('password'),
141                                         'regdate' => time})) {
142                                         $form->field(name => "confirm_password", type => "hidden");
143                                         $form->field(name => "email", type => "hidden");
144                                         $form->text("Account creation successful. Now you can Login.");
145                                 }
146                                 else {
147                                         error("Error creating account.");
148                                 }
149                         }
150                         elsif ($form->submitted eq 'Mail Password') {
151                                 my $user_name=$form->field("name");
152                                 my $template=template("passwordmail.tmpl");
153                                 $template->param(
154                                         user_name => $user_name,
155                                         user_password => IkiWiki::userinfo_get($user_name, "password"),
156                                         wikiurl => $config{url},
157                                         wikiname => $config{wikiname},
158                                         REMOTE_ADDR => $ENV{REMOTE_ADDR},
159                                 );
160                         
161                                 eval q{use Mail::Sendmail};
162                                 error($@) if $@;
163                                 sendmail(
164                                         To => IkiWiki::userinfo_get($user_name, "email"),
165                                         From => "$config{wikiname} admin <$config{adminemail}>",
166                                         Subject => "$config{wikiname} information",
167                                         Message => $template->output,
168                                 ) or error("Failed to send mail");
169                         
170                                 $form->text("Your password has been emailed to you.");
171                                 $form->field(name => "name", required => 0);
172                                 push @$buttons, "Mail Password";
173                         }
174                         elsif ($form->submitted eq "Register") {
175                                 @$buttons="Create Account";
176                         }
177                 }
178                 elsif ($form->submitted eq "Create Account") {
179                         @$buttons="Create Account";
180                 }
181                 else {
182                         push @$buttons, "Register", "Mail Password";
183                 }
184         }
185         elsif ($form->title eq "preferences") {
186                 if ($form->submitted eq "Save Preferences" && $form->validate) {
187                         my $user_name=$form->field('name');
188                         foreach my $field (qw(password)) {
189                                 if (defined $form->field($field) && length $form->field($field)) {
190                                         userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
191                                 }
192                         }
193                 }
194         }
195         
196         IkiWiki::printheader($session);
197         print IkiWiki::misctemplate($form->title, $form->render(submit => $buttons));
198 } #}}}