From 5f162cfd344f6b75fa39a57be4b3d488cadd1535 Mon Sep 17 00:00:00 2001 From: joey Date: Fri, 2 Feb 2007 02:33:03 +0000 Subject: * Add canedit hook, allowing arbitrary controls over when a page can be edited. * Move code forcing signing before edit to a new "signinedit" plugin, and code checking for locked pages into a new "lockedit" plugin. Both are enabled by default. * Remove the anonok config setting. This is now implemented by a new "anonok" plugin. Anyone with a wiki allowing anonymous edits should change their configs to enable this new plugin. * Add an opendiscussion plugin that allows anonymous users to edit discussion pages, on a wiki that is otherwise wouldn't allow it. * Lots of CGI code reorg and cleanup. --- IkiWiki/CGI.pm | 131 ++++++++++++++++++++------------------- IkiWiki/Plugin/anonok.pm | 16 +++++ IkiWiki/Plugin/lockedit.pm | 61 ++++++++++++++++++ IkiWiki/Plugin/opendiscussion.pm | 22 +++++++ IkiWiki/Plugin/openid.pm | 2 +- IkiWiki/Plugin/passwordauth.pm | 2 +- IkiWiki/Plugin/signinedit.pm | 25 ++++++++ IkiWiki/Plugin/skeleton.pm | 9 +++ 8 files changed, 201 insertions(+), 67 deletions(-) create mode 100644 IkiWiki/Plugin/anonok.pm create mode 100644 IkiWiki/Plugin/lockedit.pm create mode 100644 IkiWiki/Plugin/opendiscussion.pm create mode 100644 IkiWiki/Plugin/signinedit.pm (limited to 'IkiWiki') diff --git a/IkiWiki/CGI.pm b/IkiWiki/CGI.pm index 0c6670546..a8e610e2d 100644 --- a/IkiWiki/CGI.pm +++ b/IkiWiki/CGI.pm @@ -33,28 +33,25 @@ sub redirect ($$) { #{{{ } } #}}} -sub page_locked ($$;$) { #{{{ +sub check_canedit ($$$;$) { #{{{ my $page=shift; + my $q=shift; my $session=shift; my $nonfatal=shift; - my $user=$session->param("name"); - return if defined $user && is_admin($user); - - foreach my $admin (@{$config{adminuser}}) { - my $locked_pages=userinfo_get($admin, "locked_pages"); - if (pagespec_match($page, userinfo_get($admin, "locked_pages"))) { - return 1 if $nonfatal; - - #translators: The first parameter is a page name, - #translators: second is the user who locked it. - error(sprintf(gettext("%s is locked by %s and cannot be edited"), - htmllink("", "", $page, 1), - userlink($admin))); + my $canedit; + run_hooks(canedit => sub { + return if defined $canedit; + my $ret=shift->($page, $q, $session); + if (defined $ret && $ret eq "") { + $canedit=1; } - } - - return 0; + elsif (defined $ret) { + $canedit=0; + error($ret) unless $nonfatal; + } + }); + return $canedit; } #}}} sub decode_form_utf8 ($) { #{{{ @@ -113,6 +110,23 @@ sub cgi_recentchanges ($) { #{{{ print $q->header(-charset => 'utf-8'), $template->output; } #}}} +# Check if the user is signed in. If not, redirect to the signin form and +# save their place to return to later. +sub needsignin ($$) { #{{{ + my $q=shift; + my $session=shift; + + if (! defined $session->param("name") || + ! userinfo_get($session->param("name"), "regdate")) { + if (! defined $session->param("postsignin")) { + $session->param(postsignin => $ENV{QUERY_STRING}); + } + cgi_signin($q, $session); + cgi_savesession($session); + exit; + } +} #}}} + sub cgi_signin ($$) { #{{{ my $q=shift; my $session=shift; @@ -134,11 +148,11 @@ sub cgi_signin ($$) { #{{{ ); my $buttons=["Login"]; - $form->field(name => "do", type => "hidden"); - if ($q->param("do") ne "signin" && !$form->submitted) { $form->text(gettext("You need to log in first.")); } + $form->field(name => "do", type => "hidden", value => "signin", + force => 1); run_hooks(formbuilder_setup => sub { shift->(form => $form, cgi => $q, session => $session); @@ -166,23 +180,19 @@ sub cgi_postsignin ($$) { #{{{ my $session=shift; # Continue with whatever was being done before the signin process. - if (defined $q->param("do") && $q->param("do") ne "signin" && - defined $session->param("postsignin")) { - my $postsignin=CGI->new($session->param("postsignin")); - $session->clear("postsignin"); - cgi($postsignin, $session); - cgi_savesession($session); - exit; - } - else { - redirect($q, $config{url}); - } + my $postsignin=CGI->new($session->param("postsignin")); + $session->clear("postsignin"); + cgi($postsignin, $session); + cgi_savesession($session); + exit; } #}}} sub cgi_prefs ($$) { #{{{ my $q=shift; my $session=shift; + needsignin($q, $session); + eval q{use CGI::FormBuilder}; error($@) if $@; my $form = CGI::FormBuilder->new( @@ -210,13 +220,10 @@ sub cgi_prefs ($$) { #{{{ $form->field(name => "email", size => 50); $form->field(name => "subscriptions", size => 50, comment => "(".htmllink("", "", "PageSpec", 1).")"); - $form->field(name => "locked_pages", size => 50, - comment => "(".htmllink("", "", "PageSpec", 1).")"); $form->field(name => "banned_users", size => 50); my $user_name=$session->param("name"); if (! is_admin($user_name)) { - $form->field(name => "locked_pages", type => "hidden"); $form->field(name => "banned_users", type => "hidden"); } @@ -225,8 +232,6 @@ sub cgi_prefs ($$) { #{{{ value => userinfo_get($user_name, "email")); $form->field(name => "subscriptions", force => 1, value => userinfo_get($user_name, "subscriptions")); - $form->field(name => "locked_pages", force => 1, - value => userinfo_get($user_name, "locked_pages")); if (is_admin($user_name)) { $form->field(name => "banned_users", force => 1, value => join(" ", get_banned_users())); @@ -245,7 +250,7 @@ sub cgi_prefs ($$) { #{{{ return; } elsif ($form->submitted eq 'Save Preferences' && $form->validate) { - foreach my $field (qw(email subscriptions locked_pages)) { + foreach my $field (qw(email subscriptions)) { if (defined $form->field($field) && length $form->field($field)) { userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field"); } @@ -422,16 +427,22 @@ sub cgi_editpage ($$) { #{{{ if length $config{userdir}; @page_locs = grep { - ! exists $pagecase{lc $_} && - ! page_locked($_, $session, 1) + ! exists $pagecase{lc $_} } @page_locs; - if (! @page_locs) { # hmm, someone else made the page in the # meantime? redirect($q, "$config{url}/".htmlpage($page)); return; } + + my @editable_locs = grep { + check_canedit($_, $q, $session, 1) + } @page_locs; + if (! @editable_locs) { + # let it throw an error this time + map { check_canedit($_, $q, $session) } @page_locs; + } my @page_types; if (exists $hooks{htmlize}) { @@ -440,13 +451,13 @@ sub cgi_editpage ($$) { #{{{ $form->tmpl_param("page_select", 1); $form->field(name => "page", type => 'select', - options => \@page_locs, value => $best_loc); + options => \@editable_locs, value => $best_loc); $form->field(name => "type", type => 'select', options => \@page_types); $form->title(sprintf(gettext("creating %s"), pagetitle($page))); } elsif ($form->field("do") eq "edit") { - page_locked($page, $session); + check_canedit($page, $q, $session); if (! defined $form->field('editcontent') || ! length $form->field('editcontent')) { my $content=""; @@ -467,7 +478,7 @@ sub cgi_editpage ($$) { #{{{ } else { # save page - page_locked($page, $session); + check_canedit($page, $q, $session); my $content=$form->field('editcontent'); @@ -547,7 +558,7 @@ sub cgi_savesession ($) { #{{{ my $oldmask=umask(077); $session->flush; umask($oldmask); -} +} #}}} sub cgi (;$$) { #{{{ my $q=shift; @@ -606,37 +617,27 @@ sub cgi (;$$) { #{{{ } } } - - # Everything below this point needs the user to be signed in. - if (((! $config{anonok} || $do eq 'prefs') && - (! defined $session->param("name") || - ! userinfo_get($session->param("name"), "regdate"))) - || $do eq 'signin') { - if ($do ne 'signin' && ! defined $session->param("postsignin")) { - $session->param(postsignin => $ENV{QUERY_STRING}); - } - cgi_signin($q, $session); - cgi_savesession($session); - return; - } - elsif (defined $session->param("postsignin")) { - cgi_postsignin($q, $session); - } - - if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) { + + if (defined $session->param("name") && + userinfo_get($session->param("name"), "banned")) { print $q->header(-status => "403 Forbidden"); $session->delete(); print gettext("You are banned."); cgi_savesession($session); - exit; } - - if ($do eq 'create' || $do eq 'edit') { - cgi_editpage($q, $session); + elsif ($do eq 'signin') { + cgi_signin($q, $session); + cgi_savesession($session); + } + elsif (defined $session->param("postsignin")) { + cgi_postsignin($q, $session); } elsif ($do eq 'prefs') { cgi_prefs($q, $session); } + elsif ($do eq 'create' || $do eq 'edit') { + cgi_editpage($q, $session); + } elsif ($do eq 'blog') { my $page=titlepage(decode_utf8($q->param('title'))); $page=~s/(\/)/"__".ord($1)."__"/eg; # escape slashes too diff --git a/IkiWiki/Plugin/anonok.pm b/IkiWiki/Plugin/anonok.pm new file mode 100644 index 000000000..3e2a746e6 --- /dev/null +++ b/IkiWiki/Plugin/anonok.pm @@ -0,0 +1,16 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::anonok; + +use warnings; +use strict; +use IkiWiki; + +sub import { #{{{ + hook(type => "canedit", id => "anonok", call => \&canedit,); +} # }}} + +sub canedit ($$$) { #{{{ + return ""; +} #}}} + +1 diff --git a/IkiWiki/Plugin/lockedit.pm b/IkiWiki/Plugin/lockedit.pm new file mode 100644 index 000000000..587f7ee54 --- /dev/null +++ b/IkiWiki/Plugin/lockedit.pm @@ -0,0 +1,61 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::lockedit; + +use warnings; +use strict; +use IkiWiki; + +sub import { #{{{ + hook(type => "canedit", id => "lockedit", call => \&canedit); + hook(type => "formbuilder_setup", id => "lockedit", + call => \&formbuilder_setup); +} # }}} + +sub canedit ($$) { #{{{ + my $page=shift; + my $cgi=shift; + my $session=shift; + + my $user=$session->param("name"); + return undef if defined $user && IkiWiki::is_admin($user); + + foreach my $admin (@{$config{adminuser}}) { + if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"))) { + return sprintf(gettext("%s is locked by %s and cannot be edited"), + htmllink("", "", $page, 1), + IkiWiki::userlink($admin)); + } + } + + return undef; +} #}}} + +sub formbuilder_setup (@) { #{{{ + my %params=@_; + + my $form=$params{form}; + my $session=$params{session}; + my $cgi=$params{cgi}; + my $user_name=$session->param("name"); + + if ($form->title eq "preferences") { + $form->field(name => "locked_pages", size => 50, + comment => "(".htmllink("", "", "PageSpec", 1).")"); + if (! IkiWiki::is_admin($user_name)) { + $form->field(name => "locked_pages", type => "hidden"); + } + if (! $form->submitted) { + $form->field(name => "locked_pages", force => 1, + value => IkiWiki::userinfo_get($user_name, "locked_pages")); + } + if ($form->submitted && $form->submitted eq 'Save Preferences') { + if (defined $form->field("locked_pages")) { + IkiWiki::userinfo_set($user_name, "locked_pages", + $form->field("locked_pages")) || + error("failed to set locked_pages"); + } + } + } +} #}}} + +1 diff --git a/IkiWiki/Plugin/opendiscussion.pm b/IkiWiki/Plugin/opendiscussion.pm new file mode 100644 index 000000000..4b1a432b2 --- /dev/null +++ b/IkiWiki/Plugin/opendiscussion.pm @@ -0,0 +1,22 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::opendiscussion; + +use warnings; +use strict; +use IkiWiki; + +sub import { #{{{ + hook(type => "canedit", id => "opendiscussion", call => \&canedit); +} # }}} + +sub canedit ($$) { #{{{ + my $page=shift; + my $cgi=shift; + my $session=shift; + + my $discussion=gettext("discussion"); + return "" if $page=~/\/\Q$discussion\E$/; + return undef; +} #}}} + +1 diff --git a/IkiWiki/Plugin/openid.pm b/IkiWiki/Plugin/openid.pm index 4a7255069..5d387fbc6 100644 --- a/IkiWiki/Plugin/openid.pm +++ b/IkiWiki/Plugin/openid.pm @@ -59,7 +59,7 @@ sub formbuilder_setup (@) { #{{{ elsif ($form->title eq "preferences") { if (! defined $form->field(name => "name")) { $form->field(name => "OpenID", disabled => 1, value => - $session->param("name"), size => 30, force => 1); + $session->param("name"), size => 50, force => 1); } } } diff --git a/IkiWiki/Plugin/passwordauth.pm b/IkiWiki/Plugin/passwordauth.pm index 7ffc12080..3007dd4ff 100644 --- a/IkiWiki/Plugin/passwordauth.pm +++ b/IkiWiki/Plugin/passwordauth.pm @@ -21,7 +21,7 @@ sub formbuilder_setup (@) { #{{{ my $cgi=$params{cgi}; if ($form->title eq "signin" || $form->title eq "register") { - $form->field(name => "name", required => 0, size => 30); + $form->field(name => "name", required => 0, size => 50); $form->field(name => "password", type => "password", required => 0); if ($form->submitted eq "Register" || $form->submitted eq "Create Account") { diff --git a/IkiWiki/Plugin/signinedit.pm b/IkiWiki/Plugin/signinedit.pm new file mode 100644 index 000000000..04532f4dc --- /dev/null +++ b/IkiWiki/Plugin/signinedit.pm @@ -0,0 +1,25 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::signinedit; + +use warnings; +use strict; +use IkiWiki; + +sub import { #{{{ + hook(type => "canedit", id => "signinedit", call => \&canedit, + last => 1); +} # }}} + +sub canedit ($$$) { #{{{ + my $page=shift; + my $cgi=shift; + my $session=shift; + + # Have the user sign in, if they are not already. This is why the + # hook runs last, so that any hooks that don't need the user to + # signin can override this. + IkiWiki::needsignin($cgi, $session); + return ""; +} #}}} + +1 diff --git a/IkiWiki/Plugin/skeleton.pm b/IkiWiki/Plugin/skeleton.pm index feb0f7419..06b184b0a 100644 --- a/IkiWiki/Plugin/skeleton.pm +++ b/IkiWiki/Plugin/skeleton.pm @@ -21,6 +21,7 @@ sub import { #{{{ hook(type => "change", id => "skeleton", call => \&change); hook(type => "cgi", id => "skeleton", call => \&cgi); hook(type => "auth", id => "skeleton", call => \&auth); + hook(type => "canedit", id => "skeleton", call => \&canedit); hook(type => "formbuilder_setup", id => "skeleton", call => \&formbuilder_setup); hook(type => "formbuilder", id => "skeleton", call => \&formbuilder); hook(type => "savestate", id => "savestate", call => \&savestate); @@ -105,6 +106,14 @@ sub auth ($$) { #{{{ debug("skeleton plugin running in auth"); } #}}} +sub canedit ($$$) { #{{{ + my $page=shift; + my $cgi=shift; + my $session=shift; + + debug("skeleton plugin running in canedit"); +} #}}} + sub formbuilder_setup (@) { #{{{ my %params=@_; -- cgit v1.2.3