2 # Ikiwiki password authentication.
3 package IkiWiki::Plugin::passwordauth;
10 hook(type => "formbuilder_setup", id => "passwordauth",
11 call => \&formbuilder_setup);
12 hook(type => "formbuilder", id => "passwordauth",
13 call => \&formbuilder);
16 sub formbuilder_setup (@) { #{{{
19 my $form=$params{form};
20 my $session=$params{session};
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);
27 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
28 $form->field(name => "confirm_password", type => "password");
29 $form->field(name => "account_creation_password", type => "password") if (length $config{account_creation_password});
30 $form->field(name => "email", size => 50);
31 $form->title("register");
34 $form->field(name => "confirm_password",
36 shift eq $form->field("password");
39 $form->field(name => "password",
41 shift eq $form->field("confirm_password");
46 if ($form->submitted) {
47 my $submittype=$form->submitted;
48 # Set required fields based on how form was submitted.
50 "Login" => [qw(name password)],
52 "Create Account" => [qw(name password confirm_password email)],
53 "Mail Password" => [qw(name)],
55 foreach my $opt (@{$required{$submittype}}) {
56 $form->field(name => $opt, required => 1);
59 if ($submittype eq "Create Account") {
61 name => "account_creation_password",
63 shift eq $config{account_creation_password};
66 ) if (length $config{account_creation_password});
73 # Validate password against name for Login.
74 if ($submittype eq "Login") {
78 length $form->field("name") &&
79 shift eq IkiWiki::userinfo_get($form->field("name"), 'password');
83 elsif ($submittype eq "Register" ||
84 $submittype eq "Create Account" ||
85 $submittype eq "Mail Password") {
86 $form->field(name => "password", validate => 'VALUE');
89 # And make sure the entered name exists when logging
90 # in or sending email, and does not when registering.
91 if ($submittype eq 'Create Account' ||
92 $submittype eq 'Register') {
98 $name=~/$config{wiki_file_regexp}/ &&
99 ! IkiWiki::userinfo_get($name, "regdate");
103 elsif ($submittype eq "Login" ||
104 $submittype eq "Mail Password") {
110 IkiWiki::userinfo_get($name, "regdate");
116 # First time settings.
117 $form->field(name => "name");
118 if ($session->param("name")) {
119 $form->field(name => "name", value => $session->param("name"));
123 elsif ($form->title eq "preferences") {
124 $form->field(name => "name", disabled => 1,
125 value => $session->param("name"), force => 1,
126 fieldset => "login");
127 $form->field(name => "password", type => "password",
130 shift eq $form->field("confirm_password");
132 $form->field(name => "confirm_password", type => "password",
135 shift eq $form->field("password");
140 sub formbuilder (@) { #{{{
143 my $form=$params{form};
144 my $session=$params{session};
145 my $cgi=$params{cgi};
146 my $buttons=$params{buttons};
148 if ($form->title eq "signin" || $form->title eq "register") {
149 if ($form->submitted && $form->validate) {
150 if ($form->submitted eq 'Login') {
151 $session->param("name", $form->field("name"));
152 IkiWiki::cgi_postsignin($cgi, $session);
154 elsif ($form->submitted eq 'Create Account') {
155 my $user_name=$form->field('name');
156 if (IkiWiki::userinfo_setall($user_name, {
157 'email' => $form->field('email'),
158 'password' => $form->field('password'),
159 'regdate' => time})) {
160 $form->field(name => "confirm_password", type => "hidden");
161 $form->field(name => "email", type => "hidden");
162 $form->text(gettext("Account creation successful. Now you can Login."));
165 error(gettext("Error creating account."));
168 elsif ($form->submitted eq 'Mail Password') {
169 my $user_name=$form->field("name");
170 my $template=template("passwordmail.tmpl");
172 user_name => $user_name,
173 user_password => IkiWiki::userinfo_get($user_name, "password"),
174 wikiurl => $config{url},
175 wikiname => $config{wikiname},
176 REMOTE_ADDR => $ENV{REMOTE_ADDR},
179 eval q{use Mail::Sendmail};
182 To => IkiWiki::userinfo_get($user_name, "email"),
183 From => "$config{wikiname} admin <$config{adminemail}>",
184 Subject => "$config{wikiname} information",
185 Message => $template->output,
186 ) or error(gettext("Failed to send mail"));
188 $form->text(gettext("Your password has been emailed to you."));
189 $form->field(name => "name", required => 0);
190 push @$buttons, "Mail Password";
192 elsif ($form->submitted eq "Register") {
193 @$buttons="Create Account";
196 elsif ($form->submitted eq "Create Account") {
197 @$buttons="Create Account";
200 push @$buttons, "Register", "Mail Password";
203 elsif ($form->title eq "preferences") {
204 if ($form->submitted eq "Save Preferences" && $form->validate) {
205 my $user_name=$form->field('name');
206 foreach my $field (qw(password)) {
207 if (defined $form->field($field) && length $form->field($field)) {
208 IkiWiki::userinfo_set($user_name, $field, $form->field($field)) ||
209 error("failed to set $field");
215 IkiWiki::printheader($session);
216 print IkiWiki::misctemplate($form->title, $form->render(submit => $buttons));