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