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, size => 30);
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", comment => "use FirstnameLastName");
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, value =>
112 $session->param("name"), force => 1);
113 $form->field(name => "password", type => "password");
114 $form->field(name => "confirm_password", type => "password",
116 shift eq $form->field("password");
122 sub formbuilder (@) { #{{{
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);
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.");
147 error("Error creating account.");
150 elsif ($form->submitted eq 'Mail Password') {
151 my $user_name=$form->field("name");
152 my $template=template("passwordmail.tmpl");
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},
161 eval q{use Mail::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");
170 $form->text("Your password has been emailed to you.");
171 $form->field(name => "name", required => 0);
172 push @$buttons, "Mail Password";
174 elsif ($form->submitted eq "Register") {
175 @$buttons="Create Account";
178 elsif ($form->submitted eq "Create Account") {
179 @$buttons="Create Account";
182 push @$buttons, "Register", "Mail Password";
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");
196 IkiWiki::printheader($session);
197 print IkiWiki::misctemplate($form->title, $form->render(submit => $buttons));