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