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