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 => "email", size => 50);
30 $form->title("register");
34 if ($form->submitted) {
35 my $submittype=$form->submitted;
36 # Set required fields based on how form was submitted.
38 "Login" => [qw(name password)],
40 "Create Account" => [qw(name password confirm_password email)],
41 "Mail Password" => [qw(name)],
43 foreach my $opt (@{$required{$submittype}}) {
44 $form->field(name => $opt, required => 1);
47 if ($submittype eq "Create Account") {
49 name => "confirm_password",
51 shift eq $form->field("password");
60 # Validate password against name for Login.
61 if ($submittype eq "Login") {
65 length $form->field("name") &&
66 shift eq IkiWiki::userinfo_get($form->field("name"), 'password');
70 elsif ($submittype eq "Register" ||
71 $submittype eq "Create Account" ||
72 $submittype eq "Mail Password") {
73 $form->field(name => "password", validate => 'VALUE');
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') {
85 $name=~/$config{wiki_file_regexp}/ &&
86 ! IkiWiki::userinfo_get($name, "regdate");
90 elsif ($submittype eq "Login" ||
91 $submittype eq "Mail Password") {
97 IkiWiki::userinfo_get($name, "regdate");
103 # First time settings.
104 $form->field(name => "name");
105 if ($session->param("name")) {
106 $form->field(name => "name", value => $session->param("name"));
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",
119 shift eq $form->field("password");
125 sub formbuilder (@) { #{{{
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);
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."));
150 error(gettext("Error creating account."));
153 elsif ($form->submitted eq 'Mail Password') {
154 my $user_name=$form->field("name");
155 my $template=template("passwordmail.tmpl");
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},
164 eval q{use Mail::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"));
173 $form->text(gettext("Your password has been emailed to you."));
174 $form->field(name => "name", required => 0);
175 push @$buttons, "Mail Password";
177 elsif ($form->submitted eq "Register") {
178 @$buttons="Create Account";
181 elsif ($form->submitted eq "Create Account") {
182 @$buttons="Create Account";
185 push @$buttons, "Register", "Mail Password";
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");
200 IkiWiki::printheader($session);
201 print IkiWiki::misctemplate($form->title, $form->render(submit => $buttons));