lib/MojoMojo/Controller/Jsrpc.pm

filter: Statementstmt 45.5% 61 / 134 Branchbran 15.7% 6 / 38 Conditioncond 25.0% 3 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 68.0% 17 / 25 Podpod 100% 10 / 10 totaltotal 43.1% 98 / 227 CC 25 SCAR 22.9
CC
25
Cov
38%
diff
30.0
set_permissions
20.0
validate_perm_edit
20.0
CRAP 9.9
« lib/MojoMojo/Controller/Gallery.pm ↑ Summary lib/MojoMojo.pm »
1 package MojoMojo::Controller::Jsrpc;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use parent 'Catalyst::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 MojoMojo::Controller::Jsrpc - Various JSRPC functions.
10  
11 =head1 SYNOPSIS
12  
13 This is the Mojo powering our AJAX features.
14  
15 =head1 DESCRIPTION
16  
17 This controller dispatches various data to ajax methods in mojomojo
18 These methods will be called indirectly through javascript functions.
19  
20 =head1 ACTIONS
21  
22 =head2 render ( /.jsrpc/render )
23  
24 Edit uses this to get live preview. It gets some content in
25 params->{content} and runs it through the formatter chain.
26  
27 =cut
28  
29 68 ▶sub render : Local {
Subroutine render: called CC=4 SCAR=14.5
30 68 my ( $self, $c ) = @_;
31 68 my $output = $c->loc("Please type something");
32 68 my $input = $c->req->params->{content};
33 68 ▶ if ( $input && $input =~ /(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input and $input =~ /(\S+)/) 66 2 68
Condition66%2/3
$input and $input =~ /(\S+)/
!ll&&!rl&&r
2066
MC/DC50%1/2
$input and $input =~ /(\S+)/
$input $input =~ /(\S+)/
Truth table66%2/3
A and B
A= $inputB= $input =~ /(\S+)/
ABresult
0X0
100
111
34 66 $output = $c->model("DBIC::Content")->format_content( $c, $input );
35 }
36  
37 68 ▶ unless ($output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output) 0 68 68
38 0 $output = $c->loc('Your input is invalid, please reformat it and try again.');
39 0 $c->res->status(500);
40 }
41  
42 68 $c->res->output($output);
43 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 =head2 child_menu ( /.jsrpc/child_menu?page_id=$page_id )
46  
47 Returns a list of children for the page given by the page_id parameter,
48 formatted for inclusion in a vertical tree navigation menu.
49  
50 =cut
51  
52 1 ▶sub child_menu : Local {
Subroutine child_menu: called CC=2 SCAR=7.6
Pod documented
53 1 my ( $self, $c, $page_id ) = @_;
54  
55 # DBIC complains if find argument is not numeric
56 1 ▶ if ( $c->req->params->{page_id} =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->params->{"page_id"} =~ /^\d+$/) 0 1 1
57 0 $c->stash->{parent_page} = $c->model("DBIC::Page")->find( $c->req->params->{page_id} );
58 }
59  
60 1 $c->stash->{template} = 'child_menu.tt';
61 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 =head2 diff ( /.jsrpc/diff )
64  
65 Loads diff on demand. Takes an absolute revision number as arg,
66 and diffs it against the previous version.
67  
68 =cut
69  
70 0 ▶sub diff : Local {
Subroutine diff: not called CC=5 SCAR=34.0
Pod documented
71 0 my ( $self, $c, $page, $revision, $against, $sparse ) = @_;
72 0 ▶ unless ($revision) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($revision) 0 0 0
73 0 ▶ my $page = $c->model("DBIC::Page")->find($page) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $page = $c->model("DBIC::Page")->find($page) or do { $c->res->output($c->loc("Can't diff a nonexistent page")); return 0 } 0 0 0
74 0 $c->res->output($c->loc("Can't diff a nonexistent page"));
75 0 return 0;
76 };
77 0 $revision = $page->content->id;
78 }
79 0 $revision = $c->model("DBIC::Content")->search(
80 {
81 page => $page,
82 version => $revision
83 }
84 )->next;
85 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (my $previous = $against ? $c->model('DBIC::Content')->search({'page', $page, 'version', $against})->next : $revision->previous) { } 0 0 0
$against ? : 0 0 0
86 my $previous = $against
87 ? $c->model("DBIC::Content")->search(
88 {
89 page => $page,
90 version => $against
91 }
92 )->next
93 : $revision->previous
94 )
95 {
96 0 $c->res->output( $revision->formatted_diff( $c, $previous, $sparse ) );
97 }
98 else {
99 0 $c->res->output($c->loc("This is the first revision! Nothing to diff against."));
100 }
101 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 =head2 submittag ( /.jsrpc/submittag )
104  
105 Add a tag through form submit
106  
107 =cut
108  
109 0 ▶sub submittag : Local {
Subroutine submittag: not called CC=1 SCAR=6.9
Pod documented
110 0 my ( $self, $c, $page ) = @_;
111 0 $c->forward( '/jsrpc/tag', [ $c->req->params->{tag} ] );
112 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 =head2 tag ( /.jsrpc/tag )
115  
116 Add a tag to a page. Returns a list of yours and popular tags.
117  
118 =cut
119  
120 6 ▶sub tag : Local Args(1) {
Subroutine tag: called CC=5 SCAR=17.0
Pod documented
121 6 my ( $self, $c, $tagname ) = @_;
122 6 ($tagname) = $tagname =~ m/([\w\s]+)/;
123 6 my $page = $c->stash->{page};
124 6 foreach my $tag ( split m/\s/, $tagname ) {
125 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($tag and not $c->model("DBIC::Tag")->search({"page", $page->id, "person", $c->req->{"user_id"}, "tag", $tagname})->next) 6 0 6
Condition33%1/3
$tag and not $c->model("DBIC::Tag")->search({"page", $page->id, "person", $c->req->{"user_id"}, "tag", $tagname})->next
!ll&&!rl&&r
006
MC/DC0%0/2
$tag and not $c->model("DBIC::Tag")->search({"page", $page->id, "person", $c->req->{"user_id"}, "tag", $tagname})->next
$tag not $c->model("DBIC::Tag")->search({"page", $page->id, "person", $c->req->{"user_id"}, "tag", $tagname})->next
Truth table33%1/3
A and not(B)
A= $tagB= not $c->model("DBIC::Tag")->search({"page", $page->id, "person", $c->req->{"user_id"}, "tag", $tagname})->next
ABresult
0X0
100
111
126 $tag
127 && !$c->model("DBIC::Tag")->search(
128 {
129 page => $page->id,
130 person => $c->req->{user_id},
131 tag => $tagname,
132 }
133 )->next()
134 )
135 {
136 $page->add_to_tags(
137 {
138 tag => $tag,
139 person => $c->stash->{user}->id
140 }
141 6 ▶ ) if $page;
Branch: 1 branch
BranchTrueFalseTotal
if $page 6 0 6
142 }
143 }
144 6 $c->req->args( [$tagname] );
145 6 $c->forward('/page/inline_tags');
146 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 =head2 untag ( /.jsrpc/untag )
149  
150 Remove a tag from a page. Returns a list of yours and popular tags.
151  
152 =cut
153  
154 0 ▶sub untag : Local Args(1) {
Subroutine untag: not called CC=3 SCAR=24.8
Pod documented
155 0 my ( $self, $c, $tagname ) = @_;
156 0 my $page = $c->stash->{page};
157 0 ▶ die "Page " . $page . " not found" unless ref $page;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page 0 0 0
158 0 my $tag = $c->model("DBIC::Tag")->search(
159 {
160 page => $page->id,
161 person => $c->user->obj->id,
162 tag => $tagname
163 }
164 )->next();
165 0 ▶ $tag->delete() if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 0 0
166 0 $c->req->args( [$tagname] );
167 0 $c->forward('/page/inline_tags');
168 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 =head2 imginfo ( .jsrpc/imginfo )
171  
172 Inline info on hover for gallery photos.
173  
174 =cut
175  
176 0 ▶sub imginfo : Local {
Subroutine imginfo: not called CC=1 SCAR=6.9
Pod documented
177 0 my ( $self, $c, $photo ) = @_;
178 0 $c->stash->{photo} = $c->model("DBIC::Photo")->find($photo);
179 0 $c->stash->{template} = 'gallery/imginfo.tt';
180 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 =head2 usersearch ( .jsrpc/usersearch )
183  
184 Backend that handles jQuery autocomplete requests for users.
185  
186 =cut
187  
188 0 ▶sub usersearch : Local {
Subroutine usersearch: not called CC=3 SCAR=24.8
Pod documented
189 0 my ($self, $c) = @_;
190 0 my $query = $c->req->param('q');
191  
192 0 $c->stash->{template} = "user/user_search.tt";
193  
194 0 ▶ if (defined($query) && length($query)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and length $query) 0 0 0
Condition0%0/3
defined $query and length $query
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table0%0/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
195 0 my $rs = $c->model('DBIC::Person')->search({
196 login => { -like => '%'.$query.'%'}
197 });
198 0 $c->stash->{users} = [ $rs->all ];
199 }
200 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 =head2 set_permissions ( .jsrpc/set_permissions )
203  
204 Sets page permissions.
205  
206 =cut
207  
208 0 ▶sub set_permissions : Local {
Subroutine set_permissions: not called CC=4 SCAR=30.0
Pod documented
209 0 my ($self, $c) = @_;
210  
211 0 $c->forward('validate_perm_edit');
212  
213 0 my @path_elements = $c->_expand_path_elements($c->stash->{path});
214 0 my $current_path = pop @path_elements;
215  
216 my ( $create, $read, $write, $delete, $attachment, $subpages) =
217 0 ▶ map { $c->req->param($_) ? 'yes' : 'no' }
Branch: 1 branch
BranchTrueFalseTotal
$c->req->param($_) ? : 0 0 0
218 qw/create read write delete attachment subpages/;
219  
220 0 my $role = $c->stash->{role};
221  
222 0 my $params = {
223 path => $current_path,
224 role => $role->id,
225 apply_to_subpages => $subpages,
226 create_allowed => $create,
227 delete_allowed => $delete,
228 edit_allowed => $write,
229 view_allowed => $read,
230 attachment_allowed => $attachment
231 };
232  
233 0 my $model = $c->model('DBIC::PathPermissions');
234  
235 # when subpages should inherit permissions we actually need to update two
236 # entries: one for the subpages and one for the current page
237 0 ▶ if ($subpages eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subpages eq 'yes') { } 0 0 0
238 # update permissions for subpages
239 0 $model->update_or_create( $params );
240  
241 # update permissions for the current page
242 0 $params->{apply_to_subpages} = 'no';
243 0 $model->update_or_create( $params );
244 }
245 # otherwise, we must remove the subpages permissions entry and update the
246 # entry for the current page
247 else {
248 # delete permissions for subpages
249 0 $model->search( {
250 path => $current_path,
251 role => $role->id,
252 apply_to_subpages => 'yes'
253 } )->delete;
254  
255 # update permissions for the current page
256 0 $model->update_or_create($params);
257 }
258  
259 # clear cache
260 0 ▶ if ( $c->pref('cache_permission_data') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->pref("cache_permission_data")) 0 0 0
261 0 $c->cache->remove( 'page_permission_data' );
262 }
263  
264 0 $c->res->body("OK");
265 0 $c->res->status(200);
266 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 =head2 clear_permissions ( .jsrpc/clear_permissions )
269  
270 Clears this page permissions for a given role (making permissions inherited).
271  
272 =cut
273  
274 0 ▶sub clear_permissions : Local {
Subroutine clear_permissions: not called CC=3 SCAR=24.8
Pod documented
275 0 my ($self, $c) = @_;
276  
277 0 $c->forward('validate_perm_edit');
278  
279 0 my @path_elements = $c->_expand_path_elements($c->stash->{path});
280 0 my $current_path = pop @path_elements;
281  
282 0 my $role = $c->stash->{role};
283  
284 0 ▶ if ($role) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role) 0 0 0
285  
286 # delete permissions for subpages
287 0 $c->model('DBIC::PathPermissions')->search( {
288 path => $current_path,
289 role => $role->id
290 } )->delete;
291  
292 # clear cache
293 0 ▶ if ( $c->pref('cache_permission_data') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->pref("cache_permission_data")) 0 0 0
294 0 $c->cache->remove( 'page_permission_data' );
295 }
296  
297 }
298  
299 0 $c->res->body("OK");
300 0 $c->res->status(200);
301  
302 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
303  
304 =head2 validate_perm_edit
305  
306 Validates if the user is able to edit permissions and if a role was supplied.
307  
308 =cut
309  
310 0 ▶sub validate_perm_edit : Private {
Subroutine validate_perm_edit: not called CC=4 SCAR=30.0
Pod documented
311 0 my ($self, $c) = @_;
312  
313 0 my $user = $c->user;
314  
315 # only admins can change permissions for now
316 0 ▶ unless ($user && $user->is_admin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user and $user->is_admin) 0 0 0
Condition0%0/3
$user and $user->is_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $user->is_admin
$user $user->is_admin
Truth table0%0/3
A and B
A= $userB= $user->is_admin
ABresult
0X0
100
111
317 0 $c->res->body("Forbidden");
318 0 $c->res->status(403);
319 0 $c->detach;
320 }
321  
322 0 my $role = $c->model('DBIC::Role')->find(
323 { name => $c->req->param('role_name') }
324 );
325  
326 0 ▶ unless ($role) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($role) 0 0 0
327 0 $c->res->body('Bad Request');
328 0 $c->res->status(400);
329 0 $c->detach;
330 }
331  
332 0 $c->stash->{role} = $role;
333 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
334  
335 =head1 AUTHOR
336  
337 Marcus Ramberg <mramberg@cpan.org>, David Naughton <naughton@cpan.org>
338  
339 =head1 LICENSE
340  
341 This library is free software. You can redistribute it and/or modify it under
342 the same terms as Perl itself.
343  
344 =cut
345  
346 35 1;
« lib/MojoMojo/Controller/Gallery.pm ↑ Summary lib/MojoMojo.pm »