7462de41c73851346403e408eb9d53cbf861fe01
[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                 if (! defined $user ||
42                     ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
43                         return sub { IkiWiki::needsignin($cgi, $session) };
44                 }
45                 else {
46                         return sprintf(gettext("%s is locked and cannot be edited"),
47                                 htmllink("", "", $page, noimageinline => 1));
48                         
49                 }
50         }
51
52         # XXX deprecated, should be removed eventually
53         foreach my $admin (@{$config{adminuser}}) {
54                 if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"))) {
55                         if (! defined $user ||
56                             ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
57                                 return sub { IkiWiki::needsignin($cgi, $session) };
58                         }
59                         else {
60                                 return sprintf(gettext("%s is locked and cannot be edited"),
61                                         htmllink("", "", $page, noimageinline => 1));
62                         }
63                 }
64         }
65
66         return undef;
67 } #}}}
68
69 sub formbuilder_setup (@) { #{{{
70         my %params=@_;
71
72         # XXX deprecated, should be removed eventually  
73         my $form=$params{form};
74         if ($form->title eq "preferences") {
75                 my $session=$params{session};
76                 my $cgi=$params{cgi};
77                 my $user_name=$session->param("name");
78
79                 $form->field(name => "locked_pages", size => 50,
80                         fieldset => "admin",
81                         comment => "deprecated; please move to locked_pages in setup file"
82                 );
83                 if (! IkiWiki::is_admin($user_name)) {
84                         $form->field(name => "locked_pages", type => "hidden");
85                 }
86                 if (! $form->submitted) {
87                         my $value=IkiWiki::userinfo_get($user_name, "locked_pages");
88                         if (length $value) {
89                                 $form->field(name => "locked_pages", force => 1, value => $value);
90                         }
91                         else {
92                                 $form->field(name => "locked_pages", type => "hidden");
93                         }
94                 }
95                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
96                         if (defined $form->field("locked_pages")) {
97                                 IkiWiki::userinfo_set($user_name, "locked_pages",
98                                         $form->field("locked_pages")) ||
99                                                 error("failed to set locked_pages");
100                                 if (! length $form->field("locked_pages")) {
101                                         $form->field(name => "locked_pages", type => "hidden");
102                                 }
103                         }
104                 }
105         }
106 } #}}}
107
108 1