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