summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/passwordauth.pm
blob: d1a965dc889c144f62a01bcba826ae192ec37b10 (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", size => 30);
  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,
  102. value => $session->param("name"), force => 1,
  103. fieldset => "login");
  104. $form->field(name => "password", type => "password",
  105. fieldset => "login");
  106. $form->field(name => "confirm_password", type => "password",
  107. fieldset => "login",
  108. validate => sub {
  109. shift eq $form->field("password");
  110. });
  111. }
  112. }
  113. sub formbuilder (@) { #{{{
  114. my %params=@_;
  115. my $form=$params{form};
  116. my $session=$params{session};
  117. my $cgi=$params{cgi};
  118. my $buttons=$params{buttons};
  119. if ($form->title eq "signin" || $form->title eq "register") {
  120. if ($form->submitted && $form->validate) {
  121. if ($form->submitted eq 'Login') {
  122. $session->param("name", $form->field("name"));
  123. IkiWiki::cgi_postsignin($cgi, $session);
  124. }
  125. elsif ($form->submitted eq 'Create Account') {
  126. my $user_name=$form->field('name');
  127. if (IkiWiki::userinfo_setall($user_name, {
  128. 'email' => $form->field('email'),
  129. 'password' => $form->field('password'),
  130. 'regdate' => time})) {
  131. $form->field(name => "confirm_password", type => "hidden");
  132. $form->field(name => "email", type => "hidden");
  133. $form->text(gettext("Account creation successful. Now you can Login."));
  134. }
  135. else {
  136. error(gettext("Error creating account."));
  137. }
  138. }
  139. elsif ($form->submitted eq 'Mail Password') {
  140. my $user_name=$form->field("name");
  141. my $template=template("passwordmail.tmpl");
  142. $template->param(
  143. user_name => $user_name,
  144. user_password => IkiWiki::userinfo_get($user_name, "password"),
  145. wikiurl => $config{url},
  146. wikiname => $config{wikiname},
  147. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  148. );
  149. eval q{use Mail::Sendmail};
  150. error($@) if $@;
  151. sendmail(
  152. To => IkiWiki::userinfo_get($user_name, "email"),
  153. From => "$config{wikiname} admin <$config{adminemail}>",
  154. Subject => "$config{wikiname} information",
  155. Message => $template->output,
  156. ) or error(gettext("Failed to send mail"));
  157. $form->text(gettext("Your password has been emailed to you."));
  158. $form->field(name => "name", required => 0);
  159. push @$buttons, "Mail Password";
  160. }
  161. elsif ($form->submitted eq "Register") {
  162. @$buttons="Create Account";
  163. }
  164. }
  165. elsif ($form->submitted eq "Create Account") {
  166. @$buttons="Create Account";
  167. }
  168. else {
  169. push @$buttons, "Register", "Mail Password";
  170. }
  171. }
  172. elsif ($form->title eq "preferences") {
  173. if ($form->submitted eq "Save Preferences" && $form->validate) {
  174. my $user_name=$form->field('name');
  175. foreach my $field (qw(password)) {
  176. if (defined $form->field($field) && length $form->field($field)) {
  177. IkiWiki::userinfo_set($user_name, $field, $form->field($field)) ||
  178. error("failed to set $field");
  179. }
  180. }
  181. }
  182. }
  183. IkiWiki::printheader($session);
  184. print IkiWiki::misctemplate($form->title, $form->render(submit => $buttons));
  185. } #}}}
  186. 1