* Improved the canedit hook interface, allowing a callback function to be
[ikiwiki.git] / IkiWiki / Plugin / lockedit.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::lockedit;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "canedit", id => "lockedit", call => \&canedit);
10         hook(type => "formbuilder_setup", id => "lockedit",
11              call => \&formbuilder_setup);
12 } # }}}
13
14 sub canedit ($$) { #{{{
15         my $page=shift;
16         my $cgi=shift;
17         my $session=shift;
18
19         my $user=$session->param("name");
20         return undef if defined $user && IkiWiki::is_admin($user);
21
22         foreach my $admin (@{$config{adminuser}}) {
23                 if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"))) {
24                         if (! defined $user ||
25                             ! userinfo_get($session->param("name"), "regdate")) {
26                                 return sub { IkiWiki::needsignin($cgi, $session) };
27                         }
28                         else {
29                                 return sprintf(gettext("%s is locked by %s and cannot be edited"),
30                                         htmllink("", "", $page, noimageinline => 1),
31                                         IkiWiki::userlink($admin));
32                         }
33                 }
34         }
35
36         return undef;
37 } #}}}
38
39 sub formbuilder_setup (@) { #{{{
40         my %params=@_;
41         
42         my $form=$params{form};
43         my $session=$params{session};
44         my $cgi=$params{cgi};
45         my $user_name=$session->param("name");
46
47         if ($form->title eq "preferences") {
48                 $form->field(name => "locked_pages", size => 50,
49                         fieldset => "admin",
50                         comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
51                 if (! IkiWiki::is_admin($user_name)) {
52                         $form->field(name => "locked_pages", type => "hidden");
53                 }
54                 if (! $form->submitted) {
55                         $form->field(name => "locked_pages", force => 1,
56                                 value => IkiWiki::userinfo_get($user_name, "locked_pages"));
57                 }
58                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
59                         if (defined $form->field("locked_pages")) {
60                                 IkiWiki::userinfo_set($user_name, "locked_pages",
61                                         $form->field("locked_pages")) ||
62                                                 error("failed to set locked_pages");
63                         }
64                 }
65         }
66 } #}}}
67
68 1