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