lockedit: Support specifying which users (and IP addresses) a page is locked for...
[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 => "getsetup", id => "lockedit", call => \&getsetup);
10         hook(type => "canedit", id => "lockedit", call => \&canedit);
11         hook(type => "formbuilder_setup", id => "lockedit",
12              call => \&formbuilder_setup);
13 } # }}}
14
15 sub getsetup () { #{{{
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 0,
20                 },
21                 locked_pages => {
22                         type => "pagespec",
23                         example => "!*/Discussion",
24                         description => "PageSpec controlling which pages are locked",
25                         link => "ikiwiki/PageSpec",
26                         safe => 1,
27                         rebuild => 0,
28                 },
29 } #}}}
30
31 sub canedit ($$) { #{{{
32         my $page=shift;
33         my $cgi=shift;
34         my $session=shift;
35
36         my $user=$session->param("name");
37         return undef if defined $user && IkiWiki::is_admin($user);
38
39         if (defined $config{locked_pages} && length $config{locked_pages} &&
40             pagespec_match($page, $config{locked_pages},
41                     user => $session->param("name"),
42                     ip => $ENV{REMOTE_ADDR},
43             )) {
44                 if (! defined $user ||
45                     ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
46                         return sub { IkiWiki::needsignin($cgi, $session) };
47                 }
48                 else {
49                         return sprintf(gettext("%s is locked and cannot be edited"),
50                                 htmllink("", "", $page, noimageinline => 1));
51                         
52                 }
53         }
54
55         # XXX deprecated, should be removed eventually
56         foreach my $admin (@{$config{adminuser}}) {
57                 if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"),
58                     user => $session->param("name"),
59                     ip => $ENV{REMOTE_ADDR},
60                 )) {
61                         if (! defined $user ||
62                             ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
63                                 return sub { IkiWiki::needsignin($cgi, $session) };
64                         }
65                         else {
66                                 return sprintf(gettext("%s is locked and cannot be edited"),
67                                         htmllink("", "", $page, noimageinline => 1));
68                         }
69                 }
70         }
71
72         return undef;
73 } #}}}
74
75 sub formbuilder_setup (@) { #{{{
76         my %params=@_;
77
78         # XXX deprecated, should be removed eventually  
79         my $form=$params{form};
80         if ($form->title eq "preferences") {
81                 my $session=$params{session};
82                 my $cgi=$params{cgi};
83                 my $user_name=$session->param("name");
84
85                 $form->field(name => "locked_pages", size => 50,
86                         fieldset => "admin",
87                         comment => "deprecated; please move to locked_pages in setup file"
88                 );
89                 if (! IkiWiki::is_admin($user_name)) {
90                         $form->field(name => "locked_pages", type => "hidden");
91                 }
92                 if (! $form->submitted) {
93                         my $value=IkiWiki::userinfo_get($user_name, "locked_pages");
94                         if (length $value) {
95                                 $form->field(name => "locked_pages", force => 1, value => $value);
96                         }
97                         else {
98                                 $form->field(name => "locked_pages", type => "hidden");
99                         }
100                 }
101                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
102                         if (defined $form->field("locked_pages")) {
103                                 IkiWiki::userinfo_set($user_name, "locked_pages",
104                                         $form->field("locked_pages")) ||
105                                                 error("failed to set locked_pages");
106                                 if (! length $form->field("locked_pages")) {
107                                         $form->field(name => "locked_pages", type => "hidden");
108                                 }
109                         }
110                 }
111         }
112 } #}}}
113
114 1