]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/lockedit.pm
93a5256770981c20d2ac27127c128ad9440d5b04
[git.ikiwiki.info.git] / IkiWiki / Plugin / lockedit.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::lockedit;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
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 } # }}}
15 sub getsetup () { #{{{
16         return
17                 locked_pages => {
18                         type => "pagespec",
19                         example => "!*/Discussion",
20                         description => "PageSpec controlling which pages are locked",
21                         description_html => htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).
22                                 " controlling which pages are locked",
23                         safe => 1,
24                         rebuild => 0,
25                 },
26 } #}}}
28 sub canedit ($$) { #{{{
29         my $page=shift;
30         my $cgi=shift;
31         my $session=shift;
33         my $user=$session->param("name");
34         return undef if defined $user && IkiWiki::is_admin($user);
36         if (defined $config{locked_pages} && length $config{locked_pages} &&
37             pagespec_match($page, $config{locked_pages})) {
38                 if (! defined $user ||
39                     ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
40                         return sub { IkiWiki::needsignin($cgi, $session) };
41                 }
42                 else {
43                         return sprintf(gettext("%s is locked and cannot be edited"),
44                                 htmllink("", "", $page, noimageinline => 1));
45                         
46                 }
47         }
49         # XXX deprecated, should be removed eventually
50         foreach my $admin (@{$config{adminuser}}) {
51                 if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"))) {
52                         if (! defined $user ||
53                             ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
54                                 return sub { IkiWiki::needsignin($cgi, $session) };
55                         }
56                         else {
57                                 return sprintf(gettext("%s is locked and cannot be edited"),
58                                         htmllink("", "", $page, noimageinline => 1));
59                         }
60                 }
61         }
63         return undef;
64 } #}}}
66 sub formbuilder_setup (@) { #{{{
67         my %params=@_;
69         # XXX deprecated, should be removed eventually  
70         my $form=$params{form};
71         if ($form->title eq "preferences") {
72                 my $session=$params{session};
73                 my $cgi=$params{cgi};
74                 my $user_name=$session->param("name");
76                 $form->field(name => "locked_pages", size => 50,
77                         fieldset => "admin",
78                         comment => "deprecated; please move to locked_pages in setup file"
79                 );
80                 if (! IkiWiki::is_admin($user_name)) {
81                         $form->field(name => "locked_pages", type => "hidden");
82                 }
83                 if (! $form->submitted) {
84                         my $value=IkiWiki::userinfo_get($user_name, "locked_pages");
85                         if (length $value) {
86                                 $form->field(name => "locked_pages", force => 1, value => $value);
87                         }
88                         else {
89                                 $form->field(name => "locked_pages", type => "hidden");
90                         }
91                 }
92                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
93                         if (defined $form->field("locked_pages")) {
94                                 IkiWiki::userinfo_set($user_name, "locked_pages",
95                                         $form->field("locked_pages")) ||
96                                                 error("failed to set locked_pages");
97                                 if (! length $form->field("locked_pages")) {
98                                         $form->field(name => "locked_pages", type => "hidden");
99                                 }
100                         }
101                 }
102         }
103 } #}}}