summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/lockedit.pm
blob: 93a5256770981c20d2ac27127c128ad9440d5b04 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::lockedit;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "getsetup", id => "lockedit", call => \&getsetup);
  8. hook(type => "canedit", id => "lockedit", call => \&canedit);
  9. hook(type => "formbuilder_setup", id => "lockedit",
  10. call => \&formbuilder_setup);
  11. } # }}}
  12. sub getsetup () { #{{{
  13. return
  14. locked_pages => {
  15. type => "pagespec",
  16. example => "!*/Discussion",
  17. description => "PageSpec controlling which pages are locked",
  18. description_html => htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).
  19. " controlling which pages are locked",
  20. safe => 1,
  21. rebuild => 0,
  22. },
  23. } #}}}
  24. sub canedit ($$) { #{{{
  25. my $page=shift;
  26. my $cgi=shift;
  27. my $session=shift;
  28. my $user=$session->param("name");
  29. return undef if defined $user && IkiWiki::is_admin($user);
  30. if (defined $config{locked_pages} && length $config{locked_pages} &&
  31. pagespec_match($page, $config{locked_pages})) {
  32. if (! defined $user ||
  33. ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
  34. return sub { IkiWiki::needsignin($cgi, $session) };
  35. }
  36. else {
  37. return sprintf(gettext("%s is locked and cannot be edited"),
  38. htmllink("", "", $page, noimageinline => 1));
  39. }
  40. }
  41. # XXX deprecated, should be removed eventually
  42. foreach my $admin (@{$config{adminuser}}) {
  43. if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"))) {
  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. return undef;
  55. } #}}}
  56. sub formbuilder_setup (@) { #{{{
  57. my %params=@_;
  58. # XXX deprecated, should be removed eventually
  59. my $form=$params{form};
  60. if ($form->title eq "preferences") {
  61. my $session=$params{session};
  62. my $cgi=$params{cgi};
  63. my $user_name=$session->param("name");
  64. $form->field(name => "locked_pages", size => 50,
  65. fieldset => "admin",
  66. comment => "deprecated; please move to locked_pages in setup file"
  67. );
  68. if (! IkiWiki::is_admin($user_name)) {
  69. $form->field(name => "locked_pages", type => "hidden");
  70. }
  71. if (! $form->submitted) {
  72. my $value=IkiWiki::userinfo_get($user_name, "locked_pages");
  73. if (length $value) {
  74. $form->field(name => "locked_pages", force => 1, value => $value);
  75. }
  76. else {
  77. $form->field(name => "locked_pages", type => "hidden");
  78. }
  79. }
  80. if ($form->submitted && $form->submitted eq 'Save Preferences') {
  81. if (defined $form->field("locked_pages")) {
  82. IkiWiki::userinfo_set($user_name, "locked_pages",
  83. $form->field("locked_pages")) ||
  84. error("failed to set locked_pages");
  85. if (! length $form->field("locked_pages")) {
  86. $form->field(name => "locked_pages", type => "hidden");
  87. }
  88. }
  89. }
  90. }
  91. } #}}}
  92. 1