line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Kwiki::PagePrivacy; |
2
|
1
|
|
|
1
|
|
40845
|
use Kwiki::Plugin -Base; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use mixin 'Kwiki::Installer'; |
4
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
const class_id => 'page_privacy'; |
7
|
|
|
|
|
|
|
const cgi_class => 'Kwiki::PagePrivacy::CGI'; |
8
|
|
|
|
|
|
|
const config_file => 'page_privacy.yaml'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
|
|
|
|
|
|
my $registry = shift; |
12
|
|
|
|
|
|
|
$registry->add(action => 'page_privacy_set'); |
13
|
|
|
|
|
|
|
$registry->add(widget => 'page_privacy_select', |
14
|
|
|
|
|
|
|
template => 'page_privacy_select.html', |
15
|
|
|
|
|
|
|
show_for => 'display', |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
$registry->add(hook => 'page:is_readable', |
18
|
|
|
|
|
|
|
post => 'is_readable', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
$registry->add(hook => 'page:is_writable', |
21
|
|
|
|
|
|
|
post => 'is_writable', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
$registry->add(hook => 'page:to_html', |
24
|
|
|
|
|
|
|
pre => 'linked_page_formatter', |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub linked_page_formatter { |
29
|
|
|
|
|
|
|
my $page = $self; |
30
|
|
|
|
|
|
|
return unless io($page->file_path)->is_link; |
31
|
|
|
|
|
|
|
my $hook = pop; |
32
|
|
|
|
|
|
|
$hook->cancel; |
33
|
|
|
|
|
|
|
return '' . $self->html_escape($page->content) . ' '; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub is_readable { |
37
|
|
|
|
|
|
|
my $page = $self; |
38
|
|
|
|
|
|
|
my $hook = pop; |
39
|
|
|
|
|
|
|
return unless $hook->returned_true; |
40
|
|
|
|
|
|
|
$self = $self->hub->page_privacy; |
41
|
|
|
|
|
|
|
my $privacy = $self->page_privacy($page); |
42
|
|
|
|
|
|
|
return 1 unless $privacy eq 'private'; |
43
|
|
|
|
|
|
|
$self->privacy_group eq $self->page_group($page); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub is_writable { |
47
|
|
|
|
|
|
|
my $page = $self; |
48
|
|
|
|
|
|
|
my $hook = pop; |
49
|
|
|
|
|
|
|
return unless $hook->returned_true; |
50
|
|
|
|
|
|
|
$self = $self->hub->page_privacy; |
51
|
|
|
|
|
|
|
my $privacy = $self->page_privacy($page); |
52
|
|
|
|
|
|
|
return 1 if $privacy eq 'public'; |
53
|
|
|
|
|
|
|
$self->privacy_group eq $self->page_group($page); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub page_privacy_set { |
57
|
|
|
|
|
|
|
return unless $self->page_privacy_selectable; |
58
|
|
|
|
|
|
|
my $privacy = $self->cgi->privacy; |
59
|
|
|
|
|
|
|
my $dir = $self->plugin_directory; |
60
|
|
|
|
|
|
|
my $id = $self->pages->current->id; |
61
|
|
|
|
|
|
|
my $group = $self->privacy_group; |
62
|
|
|
|
|
|
|
if ($privacy eq 'public') { |
63
|
|
|
|
|
|
|
io->dir("$dir/$id")->rmtree; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
elsif ($privacy eq 'protected') { |
66
|
|
|
|
|
|
|
io->file("$dir/$id/group")->assert->print("$group\n"); |
67
|
|
|
|
|
|
|
io->file("$dir/$id/protected")->assert->touch; |
68
|
|
|
|
|
|
|
io->file("$dir/$id/private")->unlink; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
elsif ($privacy eq 'private') { |
71
|
|
|
|
|
|
|
io->file("$dir/$id/group")->assert->print("$group\n"); |
72
|
|
|
|
|
|
|
io->file("$dir/$id/private")->assert->touch; |
73
|
|
|
|
|
|
|
io->file("$dir/$id/protected")->unlink; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub page_privacy_selectable { |
78
|
|
|
|
|
|
|
my $group = $self->privacy_group |
79
|
|
|
|
|
|
|
or return; |
80
|
|
|
|
|
|
|
my $page_group = $self->page_group |
81
|
|
|
|
|
|
|
or return 1; |
82
|
|
|
|
|
|
|
$group eq $page_group; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub privacy_group { |
86
|
|
|
|
|
|
|
$self->config->privacy_group; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub page_group { |
90
|
|
|
|
|
|
|
my $id = (shift || $self->pages->current)->id; |
91
|
|
|
|
|
|
|
my $dir = $self->plugin_directory; |
92
|
|
|
|
|
|
|
my $group_file = "$dir/$id/group"; |
93
|
|
|
|
|
|
|
-f $group_file ? io($group_file)->chomp->getline : ''; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub page_privacy { |
97
|
|
|
|
|
|
|
my $id = (shift || $self->pages->current)->id; |
98
|
|
|
|
|
|
|
my $dir = $self->plugin_directory; |
99
|
|
|
|
|
|
|
-e "$dir/$id/private" ? 'private' : |
100
|
|
|
|
|
|
|
-e "$dir/$id/protected" ? 'protected' : |
101
|
|
|
|
|
|
|
'public'; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
package Kwiki::PagePrivacy::CGI; |
105
|
|
|
|
|
|
|
use Kwiki::CGI -base; |
106
|
|
|
|
|
|
|
cgi 'privacy'; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
package Kwiki::PagePrivacy; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__DATA__ |