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