lib/MojoMojo.pm

filter: Statementstmt 89.2% 224 / 251 Branchbran 72.9% 89 / 122 Conditioncond 57.1% 24 / 42 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 93.9% 31 / 33 Podpod 100% 16 / 16 totaltotal 80.0% 392 / 490 CC 66 SCAR 22.4
CC
66
Cov
81%
pref_cached
20.8
check_permissions
18.5
get_permissions_data
9.0
CRAP 9.4
« lib/MojoMojo/Controller/Jsrpc.pm ↑ Summary lib/Text/Wikispaces2Markdown.pm »
1 package MojoMojo;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use Path::Class 'file';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 ▶use Catalyst qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
7 ConfigLoader
8 Authentication
9 Cache
10 Session
11 Session::Store::Cache
12 Session::State::Cookie
13 Static::Simple
14 SubRequest
15 I18N
16 Setenv
17 35 /;
18  
19 35 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 35 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 35 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 35 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 35 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use DBIx::Class::ResultClass::HashRefInflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 35 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 35 ▶use MojoMojo::Formatter::Wiki;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 use Module::Pluggable::Ordered
29 35 ▶ search_path => 'MojoMojo::Formatter',
Subroutine BEGIN: called CC=1 SCAR=0.0
30 except => qr/^MojoMojo::Plugin::/,
31 35 require => 1;
32  
33 35 our $VERSION = '1.12';
34 35 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 MojoMojo->config->{authentication}{dbic} = {
37 35 user_class => 'DBIC::Person',
38 user_field => 'login',
39 password_field => 'pass'
40 };
41 35 MojoMojo->config->{default_view} = 'TT';
42 MojoMojo->config->{'Plugin::Cache'}{backend} = {
43 class => "Cache::FastMmap",
44 unlink_on_exit => 1,
45 share_file => ''
46 . Path::Class::file(
47 File::Spec->tmpdir,
48 'mojomojo-sharefile-' . Digest::MD5::md5_hex(MojoMojo->config->{home})
49 35 ),
50 };
51  
52 35 __PACKAGE__->config(
53 authentication => {
54 default_realm => 'members',
55 use_session => 1,
56 realms => {
57 members => {
58 credential => {
59 class => 'Password',
60 password_field => 'pass',
61 password_type => 'hashed',
62 password_hash_type => 'SHA-1',
63 },
64 store => {class => 'DBIx::Class', user_class => 'DBIC::Person',},
65 },
66 }
67 }
68 );
69  
70 35 __PACKAGE__->config('Controller::HTML::FormFu' =>
71 {languages_from_context => 1, localize_from_context => 1,});
72  
73 35 __PACKAGE__->config(setup_components => {search_extra => ['::Extensions'],});
74  
75 35 MojoMojo->setup();
76  
77 # Check for deployed database
78 35 my $has_DB = 1;
79 35 my $NO_DB_MESSAGE = <<"EOF";
80  
81 ***********************************************
82 ERROR. Looks like you need to deploy a database.
83 Run script/mojomojo_spawn_db.pl
84 ***********************************************
85  
86 EOF
87 35 eval {
88 35 MojoMojo->model('DBIC')
89 ->schema->resultset('MojoMojo::Schema::Result::Person')->next;
90 };
91 35 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 32 35
92 3 $has_DB = 0;
93 3 warn $NO_DB_MESSAGE;
94 3 warn "(Error: $@)";
95 }
96  
97 MojoMojo->model('DBIC')
98 ->schema->attachment_dir(MojoMojo->config->{attachment_dir}
99 35 ▶ || MojoMojo->path_to('uploads') . '');
Condition33%1/3
'MojoMojo'->config->{'attachment_dir'} || 'MojoMojo'->path_to('uploads') . ''
l!l&&r!l&&!r
3500
MC/DC0%0/2
'MojoMojo'->config->{'attachment_dir'} || 'MojoMojo'->path_to('uploads') . ''
'MojoMojo'->config->{'attachment_dir'} 'MojoMojo'->path_to('uploads') . ''
Truth table33%1/3
A || B
A= 'MojoMojo'->config->{'attachment_dir'}B= 'MojoMojo'->path_to('uploads') . ''
ABresult
1X1
011
000
100  
101 =head1 NAME
102  
103 MojoMojo - A Wiki with a tree
104  
105 =head1 SYNOPSIS
106  
107 # Set up database (see mojomojo.conf first)
108  
109 ./script/mojomojo_spawn_db.pl
110  
111 # Standalone mode
112  
113 ./script/mojomo_server.pl
114  
115 # In apache conf
116 <Location /mojomojo>
117 SetHandler perl-script
118 PerlHandler MojoMojo
119 </Location>
120  
121 =head1 DESCRIPTION
122  
123 Mojomojo is a content management system, borrowing many concepts from
124 wikis and blogs. It allows you to maintain a full tree-structure of pages,
125 and to interlink them in various ways. It has full version support, so you can
126 always go back to a previous version and see what's changed with an easy diff
127 system. There are also a some of useful features like live AJAX preview while
128 editing, tagging, built-in fulltext search, image galleries, and RSS feeds
129 for every wiki page.
130  
131 To find out more about how you can use MojoMojo, please visit
132 L<http://mojomojo.org/> or read the installation instructions in
133 L<MojoMojo::Installation> to try it out yourself.
134  
135 =head1 METHODS
136  
137 =head2 prepare
138  
139 Accommodate a forcing of SSL if needed in a reverse proxy setup.
140  
141 =cut
142  
143 197 ▶sub prepare {
Subroutine prepare: called CC=2 SCAR=9.2
Pod documented
144 197 my $self = shift->next::method(@_);
145 197 ▶ if ($self->config->{force_ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"force_ssl"}) 0 197 197
146 0 my $request = $self->request;
147 0 $request->base->scheme('https');
148 0 $request->uri->scheme('https');
149 }
150 197 return $self;
151 }
152  
153  
154 =head2 ajax
155  
156 Return whether the request is an AJAX one (used by the live preview,
157 for example), as opposed to a rgular request (such as one used to view
158 a page).
159  
160 =cut
161  
162 0 ▶sub ajax {
Subroutine ajax: not called CC=2 SCAR=17.9
Pod documented
163 0 my ($c) = @_;
164 0 ▶ return $c->req->header('x-requested-with')
Condition0%0/3
$c->req->header('x-requested-with') && $c->req->header('x-requested-with') eq 'XMLHttpRequest'
!ll&&!rl&&r
000
MC/DC0%0/2
$c->req->header('x-requested-with') && $c->req->header('x-requested-with') eq 'XMLHttpRequest'
$c->req->header('x-requested-with') $c->req->header('x-requested-with') eq 'XMLHttpRequest'
Truth table0%0/3
A && B
A= $c->req->header('x-requested-with')B= $c->req->header('x-requested-with') eq 'XMLHttpRequest'
ABresult
0X0
100
111
165 && $c->req->header('x-requested-with') eq 'XMLHttpRequest';
166 }
167  
168 =head2 expand_wikilink
169  
170 Proxy method for the L<MojoMojo::Formatter::Wiki> expand_wikilink method.
171  
172 =cut
173  
174 105 ▶sub expand_wikilink {
Subroutine expand_wikilink: called CC=1 SCAR=0.0
Pod documented
175 105 my $c = shift;
176 105 return MojoMojo::Formatter::Wiki->expand_wikilink(@_);
177 }
178  
179 =head2 wikiword
180  
181 Format a wikiword as a link or as a wanted page, as appropriate.
182  
183 =cut
184  
185 62 ▶sub wikiword {
Subroutine wikiword: called CC=1 SCAR=0.0
Pod documented
186 62 return MojoMojo::Formatter::Wiki->format_link(@_);
187 }
188  
189 =head2 pref
190  
191 Find or create a preference key. Update it if a value is passed, then
192 return the current setting.
193  
194 =cut
195  
196 3145 ▶sub pref {
Subroutine pref: called CC=5 SCAR=17.3
Pod documented
197 3145 my ($c, $setting, $value) = @_;
198  
199 3145 ▶ return unless $setting;
Branch: 1 branch
BranchTrueFalseTotal
unless $setting 0 3145 3145
200  
201 # Unfortunately there are MojoMojo->pref() calls in
202 # MojoMojo::Schema::Result::Person which makes it hard
203 # to get cache working for those calls - so we'll just
204 # not use caching for those calls.
205 3145 ▶ return $c->pref_cached($setting, $value) if ref($c) eq 'MojoMojo';
Branch: 1 branch
BranchTrueFalseTotal
if ref $c eq "MojoMojo" 2979 166 3145
206  
207 166 $setting
208 = $c->model('DBIC::Preference')->find_or_create({prefkey => $setting});
209 165 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 165 165
210 0 $setting->prefvalue($value);
211 0 $setting->update();
212 0 return $value;
213 }
214 165 ▶ return (defined $setting->prefvalue() ? $setting->prefvalue : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $setting->prefvalue ? : 150 15 165
215 }
216  
217 =head2 pref_cached
218  
219 Get preference key/value from cache if possible.
220  
221 =cut
222  
223 2979 ▶sub pref_cached {
Subroutine pref_cached: called CC=17 SCAR=30.3
Pod documented
224 2979 my ($c, $setting, $value) = @_;
225  
226 # Already in cache and no new value to set?
227 2979 ▶ if (defined $c->cache->get($setting) and not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->cache->get($setting) and not defined $value) 2358 621 2979
Condition100%3/3
defined $c->cache->get($setting) and not defined $value
!ll&&!rl&&r
61382358
MC/DC100%2/2
defined $c->cache->get($setting) and not defined $value
defined $c->cache->get($setting) not defined $value
Truth table100%3/3
A and not(B)
A= defined $c->cache->get($setting)B= not defined $value
ABresult
0X0
100
111
228 2358 return $c->cache->get($setting);
229 }
230  
231 # Check that we have a database, i.e. script/mojomojo_spawn_db.pl was run.
232 621 my $row;
233 621 $row = $c->model('DBIC::Preference')->find_or_create({prefkey => $setting});
234  
235 # Update database
236 621 ▶ $row->update({prefvalue => $value}) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 9 612 621
237  
238 621 my $prefvalue = $row->prefvalue();
239  
240 # if no entry in preferences, try get one from config or get default value
241 621 ▶ unless (defined $prefvalue) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefvalue) 502 119 621
242  
243 502 ▶ if ($setting eq 'main_formatter') {
Branch: 6 branches
BranchTrueFalseTotal
if ($setting eq 'main_formatter') { } 14 488 502
elsif ($setting eq 'default_lang') { } 30 458 488
elsif ($setting eq 'name') { } 0 458 458
elsif ($setting eq 'theme') { } 0 458 458
elsif ($setting =~ /^(enforce_login|check_permission_on_view)$/) { } 50 408 458
elsif ($setting =~ /^(cache_permission_data|create_allowed|delete_allowed|edit_allowed|view_allowed|attachment_allowed)$/) { } 132 276 408
244 $prefvalue
245 = defined $c->config->{'main_formatter'}
246 14 ▶ ? $c->config->{'main_formatter'}
Branch: 1 branch
BranchTrueFalseTotal
defined $c->config->{'main_formatter'} ? : 0 14 14
247 : 'MojoMojo::Formatter::Markdown';
248 }
249 elsif ($setting eq 'default_lang') {
250 $prefvalue
251 30 ▶ = defined $c->config->{$setting} ? $c->config->{$setting} : 'en';
Branch: 1 branch
BranchTrueFalseTotal
defined $c->config->{$setting} ? : 0 30 30
252 }
253 elsif ($setting eq 'name') {
254 $prefvalue
255 0 ▶ = defined $c->config->{$setting} ? $c->config->{$setting} : 'MojoMojo';
Branch: 1 branch
BranchTrueFalseTotal
defined $c->config->{$setting} ? : 0 0 0
256 }
257 elsif ($setting eq 'theme') {
258 $prefvalue
259 0 ▶ = defined $c->config->{$setting} ? $c->config->{$setting} : 'default';
Branch: 1 branch
BranchTrueFalseTotal
defined $c->config->{$setting} ? : 0 0 0
260 }
261 elsif ($setting =~ /^(enforce_login|check_permission_on_view)$/) {
262 $prefvalue
263 = defined $c->config->{'permissions'}{$setting}
264 50 ▶ ? $c->config->{'permissions'}{$setting}
Branch: 1 branch
BranchTrueFalseTotal
defined $c->config->{'permissions'}{$setting} ? : 50 0 50
265 : 0;
266 }
267 elsif ($setting
268 =~ /^(cache_permission_data|create_allowed|delete_allowed|edit_allowed|view_allowed|attachment_allowed)$/
269 )
270 {
271 $prefvalue
272 = defined $c->config->{'permissions'}{$setting}
273 132 ▶ ? $c->config->{'permissions'}{$setting}
Branch: 1 branch
BranchTrueFalseTotal
defined $c->config->{'permissions'}{$setting} ? : 132 0 132
274 : 1;
275 }
276 else {
277 276 $prefvalue = $c->config->{$setting};
278 }
279  
280 }
281  
282 # Update cache
283 621 $c->cache->set($setting => $prefvalue);
284  
285 621 return $c->cache->get($setting);
286 }
287  
288 =head2 fixw
289  
290 Clean up wiki words: replace spaces with underscores and remove non-\w, / and .
291 characters.
292  
293 =cut
294  
295 0 ▶sub fixw {
Subroutine fixw: not called CC=1 SCAR=6.9
Pod documented
296 0 my ($c, $w) = @_;
297 0 $w =~ s/\s/\_/g;
298 0 $w =~ s/[^\w\/\.]//g;
299 0 return $w;
300 }
301  
302 =head2 tz
303  
304 Convert timezone
305  
306 =cut
307  
308 107 ▶sub tz {
Subroutine tz: called CC=3 SCAR=12.7
Pod documented
309 107 my ($c, $dt) = @_;
310 107 ▶ if ($c->user && $c->user->timezone) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->user and $c->user->timezone) 0 107 107
Condition66%2/3
$c->user and $c->user->timezone
!ll&&!rl&&r
86210
MC/DC0%0/2
$c->user and $c->user->timezone
$c->user $c->user->timezone
Truth table66%2/3
A and B
A= $c->userB= $c->user->timezone
ABresult
0X0
100
111
311 0 eval { $dt->set_time_zone($c->user->timezone) };
312 }
313 107 return $dt;
314 }
315  
316 =head2 prepare_action
317  
318 Provide "No DB" message when one needs to spawn the db (script/mojomojo_spawn.pl).
319  
320 =cut
321  
322 197 ▶sub prepare_action {
Subroutine prepare_action: called CC=2 SCAR=6.9
323 197 my $c = shift;
324  
325 197 ▶ if ($has_DB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_DB) { } 196 1 197
326 196 $c->next::method(@_);
327 }
328 else {
329 1 $c->res->status(404);
330 1 $c->response->body($NO_DB_MESSAGE);
331 1 return;
332 }
333 }
334  
335 =head2 prepare_path
336  
337 We override this method to work around some of Catalyst's assumptions about
338 dispatching. Since MojoMojo supports page namespaces
339 (e.g. C</parent_page/child_page>), with page paths that always start with C</>,
340 we strip the trailing slash from C<< $c->req->base >>. Also, since MojoMojo
341 indicates actions by appending a C<.$action> to the path
342 (e.g. C</parent_page/child_page.edit>), we remove the page path and save it in
343 C<< $c->stash->{path} >> and reset C<< $c->req->path >> to C<< $action >>.
344 We save the original URI in C<< $c->stash->{pre_hacked_uri} >>.
345  
346 =cut
347  
348 197 ▶sub prepare_path {
Subroutine prepare_path: called CC=4 SCAR=13.9
Pod documented
349 197 my $c = shift;
350 197 $c->next::method(@_);
351 197 $c->stash->{pre_hacked_uri} = $c->req->uri->clone;
352 197 my $base = $c->req->base;
353 197 $base =~ s|/+$||;
354 197 $c->req->base(URI->new($base));
355 197 my ($path, $action);
356 197 $path = $c->req->path;
357  
358 197 ▶ if ($path =~ /^special(?:\/|$)(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^special(?:/|$)(.*)]) { } 12 185 197
359 12 $c->stash->{path} = $path;
360 12 $c->req->path($1);
361 }
362 else {
363 # find the *last* period, so that pages can have periods in their name.
364 185 my $index = index($path, '.');
365  
366 185 ▶ if ($index == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == -1) { } 24 161 185
367  
368 # no action found, default to view
369 24 $c->stash->{path} = $path;
370 24 $c->req->path('view');
371 }
372 else {
373  
374 # set path in stash, and set req.path to action
375 161 $c->stash->{path} = substr($path, 0, $index);
376 161 $c->req->path(substr($path, $index + 1));
377 }
378 }
379 197 ▶ $c->stash->{path} = '/' . $c->stash->{path} unless ($path =~ m!^/!);
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 197 0 197
380 }
381  
382 =head2 base_uri
383  
384 Return C<< $c->req->base >> as an URI object.
385  
386 =cut
387  
388 5 ▶sub base_uri {
Subroutine base_uri: called CC=1 SCAR=0.0
Pod documented
389 5 my $c = shift;
390 5 return URI->new($c->req->base);
391 }
392  
393 =head2 uri_for
394  
395 Override C<< $c->uri_for >> to append path, if a relative path is used.
396  
397 =cut
398  
399 4227 ▶sub uri_for {
Subroutine uri_for: called CC=5 SCAR=16.2
Pod documented
400 4227 my $c = shift;
401 4227 ▶ unless ($_[0] =~ m/^\//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0] =~ m[^/]) 2020 2207 4227
402 2020 my $val = shift @_;
403 2020 ▶ my $prefix = $c->stash->{path} =~ m|^/| ? '' : '/';
Branch: 1 branch
BranchTrueFalseTotal
$c->stash->{'path'} =~ m[^/] ? : 2020 0 2020
404 2020 unshift(@_, $prefix . $c->stash->{path} . '.' . $val);
405 }
406  
407 # do I see unicode here?
408 4227 ▶ if (Encode::is_utf8($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($_[0])) 36 4191 4227
409 $_[0]
410 36 = join('/', map { URI::Escape::uri_escape_utf8($_) } split(/\//, $_[0]));
411 }
412  
413 4227 my $res = $c->next::method(@_);
414 4227 ▶ $res->scheme('https') if $c->config->{'force_ssl'};
Branch: 1 branch
BranchTrueFalseTotal
if $c->config->{"force_ssl"} 0 4227 4227
415 4227 return $res;
416 }
417  
418 =head2 uri_for_static
419  
420 C</static/> has been remapped to C</.static/>.
421  
422 =cut
423  
424 1688 ▶sub uri_for_static {
Subroutine uri_for_static: called CC=2 SCAR=7.2
Pod documented
425 1688 my ($self, $asset) = @_;
426 return (
427 defined($self->config->{static_path})
428 1688 ▶ ? $self->config->{static_path} . $asset
Branch: 1 branch
BranchTrueFalseTotal
defined $self->config->{'static_path'} ? : 0 1688 1688
429 : $self->uri_for('/.static', $asset));
430 }
431  
432 =head2 _cleanup_path
433  
434 Lowercase the path and remove any double-slashes.
435  
436 =cut
437  
438 117 ▶sub _cleanup_path {
Subroutine _cleanup_path: called CC=1 SCAR=0.0
439 117 my ($c, $path) = @_;
440 ## Make some changes to the path - we have to do this
441 ## because path is not always cleaned up before we get it:
442 ## sometimes we get caps, other times we don't. Permissions are
443 ## set using lowercase paths.
444  
445 ## lowercase the path - and ensure it has a leading /
446 117 my $searchpath = lc($path);
447  
448 # clear out any double-slashes
449 117 $searchpath =~ s|//|/|g;
450  
451 117 return $searchpath;
452 }
453  
454 =head2 _expand_path_elements
455  
456 Generate all the intermediary paths to C</path/to/a/page>, starting from C</>
457 and ending with the complete path:
458  
459 /
460 /path
461 /path/to
462 /path/to/a
463 /path/to/a/page
464  
465 =cut
466  
467 117 ▶sub _expand_path_elements {
Subroutine _expand_path_elements: called CC=4 SCAR=13.9
468 117 my ($c, $path) = @_;
469 117 my $searchpath = $c->_cleanup_path($path);
470  
471 117 my @pathelements = split '/', $searchpath;
472  
473 117 ▶ if (@pathelements && $pathelements[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (@pathelements and $pathelements[0] eq "") 43 74 117
Condition66%2/3
@pathelements and $pathelements[0] eq ""
!ll&&!rl&&r
74043
MC/DC50%1/2
@pathelements and $pathelements[0] eq ""
@pathelements $pathelements[0] eq ""
Truth table66%2/3
A and B
A= @pathelementsB= $pathelements[0] eq ""
ABresult
0X0
100
111
474 43 shift @pathelements;
475 }
476  
477 117 my @paths_to_check = ('/');
478  
479 117 my $current_path = '';
480  
481 117 foreach my $pathitem (@pathelements) {
482 54 $current_path .= "/" . $pathitem;
483 54 push @paths_to_check, $current_path;
484 }
485  
486 117 return @paths_to_check;
487 }
488  
489 =head2 get_permissions_data
490  
491 Permissions are checked prior to most actions, including C<view> if that is
492 turned on in the configuration. The permission system works as follows:
493  
494 =over
495  
496 =item 1.
497  
498 There is a base set of rules which may be defined in the application
499 config. These are:
500  
501 $c->config->{permissions}{view_allowed} = 1; # or 0
502
503 Similar entries exist for C<delete>, C<edit>, C<create> and C<attachment>.
504 If these config variables are not defined, the default is to allow anyone
505 to do anything.
506  
507 =item 2.
508  
509 Global rules that apply to everyone may be specified by creating a
510 record with a role id of 0.
511  
512 =item 3.
513  
514 Rules are defined using a combination of path(s)?, and role and may be
515 applied to subpages or not.
516  
517 TODO: clarify.
518  
519 =item 4.
520  
521 All rules matching a given user's roles and the current path are used to
522 determine the final yes/no on each permission. Rules are evaluated from
523 least-specific path to most specific. This means that when checking
524 permissions on C</foo/bar/baz>, permission rules set for C</foo> will be
525 overridden by rules set on C</foo/bar> when editing C</foo/bar/baz>. When two
526 rules (from different roles) are found for the same path prefix, explicit
527 C<allow>s override C<deny>s. Null entries for a given permission are always
528 ignored and do not affect the permissions defined at earlier level. This
529 allows you to change certain permissions (such as C<create>) only while not
530 affecting previously determined permissions for the other actions. Finally -
531 C<apply_to_subpages> C<yes>/C<no> is exclusive, meaning that a rule for C</foo> with
532 C<apply_to_subpages> set to C<yes> will apply to C</foo/bar> but not to C</foo>
533 alone. The endpoint in the path is always checked for a rule explicitly for that
534 page - meaning C<apply_to_subpages = no>.
535  
536 =back
537  
538 =cut
539  
540 117 ▶sub get_permissions_data {
Subroutine get_permissions_data: called CC=9 SCAR=22.0
Pod documented
541 117 my ($c, $current_path, $paths_to_check, $role_ids) = @_;
542  
543 # default to roles for current user
544 117 ▶ $role_ids ||= $c->user_role_ids($c->user);
Condition33%1/3
$role_ids ||= $c->user_role_ids($c->user)
l!l&&r!l&&!r
11700
MC/DC0%0/2
$role_ids ||= $c->user_role_ids($c->user)
$role_ids $c->user_role_ids($c->user)
Truth table33%1/3
A ||= B
A= $role_idsB= $c->user_role_ids($c->user)
ABresult
1X1
011
000
545  
546 117 my $permdata;
547  
548 ## Now that we have our path elements to check, we have to figure out how we are accessing them.
549 ## If we have caching turned on, we load the perms from the cache and walk the tree.
550 ## Otherwise we pull what we need out of the DB. The structure is:
551 # $permdata{$pagepath} = {
552 # admin => {
553 # page => {
554 # create => 'yes',
555 # delete => 'yes',
556 # view => 'yes',
557 # edit => 'yes',
558 # attachment => 'yes',
559 # },
560 # subpages => {
561 # create => 'yes',
562 # delete => 'yes',
563 # view => 'yes',
564 # edit => 'yes',
565 # attachment => 'yes',
566 # },
567 # },
568 # users => .....
569 # }
570 117 ▶ if ($c->pref('cache_permission_data')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->pref("cache_permission_data")) 113 4 117
571 113 $permdata = $c->cache->get('page_permission_data');
572 }
573  
574 # If we don't have any permissions data, we have a problem. We need to load it.
575 # We have two options here - if we are caching, we will load everything and cache it.
576 # If we are not - then we load just the bits we need.
577 117 ▶ if (!$permdata) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($permdata) 26 91 117
578  
579 # Initialize $permdata as a reference or we end up with an error
580 # when we try to dereference it further down. The error we're avoiding is:
581 # Can't use string ("") as a HASH ref while "strict refs"
582 26 $permdata = {};
583  
584 ## Either the data hasn't been loaded, or it's expired since we used it last,
585 ## so we need to reload it.
586 26 my $rs = $c->model('DBIC::PathPermissions')
587 ->search(undef, {order_by => 'length(path),role,apply_to_subpages'});
588  
589 # If we are not caching, we don't return the whole enchilada.
590 26 ▶ if (!$c->pref('cache_permission_data')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->pref("cache_permission_data")) 4 22 26
591 ## this seems odd to me - but that's what the DBIx::Class says to do.
592 4 ▶ $rs = $rs->search({role => $role_ids}) if $role_ids;
Branch: 1 branch
BranchTrueFalseTotal
if $role_ids 4 0 4
593 4 $rs = $rs->search(
594 {
595 '-or' => [
596 {path => $paths_to_check, apply_to_subpages => 'yes'},
597 {path => $current_path, apply_to_subpages => 'no'}
598 ]
599 }
600 );
601 }
602 26 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
603  
604 26 my $recordtype;
605 26 ▶ while (my $record = $rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $rs->next) 228 26 254
606 228 ▶ if ($record->{'apply_to_subpages'} eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{'apply_to_subpages'} eq 'yes') { } 116 112 228
607 116 $recordtype = 'subpages';
608 }
609 else {
610 112 $recordtype = 'page';
611 }
612 228 %{$permdata->{$record->{'path'}}{$record->{'role'}}{$recordtype}}
613 228 = map { $_ => $record->{$_ . "_allowed"} }
614 qw/create edit view delete attachment/;
615 }
616 }
617  
618 ## now we re-cache it - if we need to. # !$c->cache('memory')->exists('page_permission_data')
619 117 ▶ if ($c->pref('cache_permission_data')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->pref("cache_permission_data")) 113 4 117
620 113 $c->cache->set('page_permission_data', $permdata);
621 }
622  
623 117 return $permdata;
624 }
625  
626 =head2 user_role_ids
627  
628 Get the list of role ids for a user.
629  
630 =cut
631  
632 117 ▶sub user_role_ids {
Subroutine user_role_ids: called CC=2 SCAR=7.0
Pod documented
633 117 my ($c, $user) = @_;
634  
635 ## always use role_id 0 - which is default role and includes everyone.
636 117 my @role_ids = (0);
637  
638 117 ▶ if (ref($user)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user) 117 0 117
639 117 push @role_ids, map { $_->role->id } $user->role_members->all;
640 }
641  
642 117 return @role_ids;
643 }
644  
645 =head2 check_permissions
646  
647 Check user permissions for a path.
648  
649 =cut
650  
651 191 ▶sub check_permissions {
Subroutine check_permissions: called CC=17 SCAR=29.2
Pod documented
652 191 my ($c, $path, $user) = @_;
653  
654 191 ▶ return {attachment => 1, create => 1, delete => 1, edit => 1, view => 1,}
Branch: 1 branch
BranchTrueFalseTotal
if $user and $user->is_admin 74 117 191
Condition100%3/3
$user and $user->is_admin
!ll&&!rl&&r
1061174
MC/DC100%2/2
$user and $user->is_admin
$user $user->is_admin
Truth table100%3/3
A and B
A= $userB= $user->is_admin
ABresult
0X0
100
111
655 if ($user && $user->is_admin);
656  
657 # if no user is logged in
658 117 ▶ if (not $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 106 11 117
659  
660 # if anonymous user is allowed
661 106 my $anonymous = $c->pref('anonymous_user');
662 106 ▶ if ($anonymous) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anonymous) 106 0 106
663  
664 # get anonymous user for no logged-in users
665 106 $user = $c->model('DBIC::Person')->search({login => $anonymous})->first;
666 }
667 }
668  
669 117 my @paths_to_check = $c->_expand_path_elements($path);
670 117 my $current_path = $paths_to_check[-1];
671  
672 117 my @role_ids = $c->user_role_ids($user);
673  
674 117 my $permdata
675 = $c->get_permissions_data($current_path, \@paths_to_check, \@role_ids);
676  
677 # rules comparison hash
678 # allow everything by default
679 117 my %rulescomparison = (
680 'create' => {
681 'allowed' => $c->pref('create_allowed'),
682 'role' => '__default',
683 'len' => 0,
684 },
685 'delete' => {
686 'allowed' => $c->pref('delete_allowed'),
687 'role' => '__default',
688 'len' => 0,
689 },
690 'edit' => {
691 'allowed' => $c->pref('edit_allowed'),
692 'role' => '__default',
693 'len' => 0,
694 },
695 'view' => {
696 'allowed' => $c->pref('view_allowed'),
697 'role' => '__default',
698 'len' => 0,
699 },
700 'attachment' => {
701 'allowed' => $c->pref('attachment_allowed'),
702 'role' => '__default',
703 'len' => 0,
704 },
705 );
706  
707 ## The outcome of this loop is a combined permission set.
708 ## The rule orders are essentially based on how specific the path
709 ## match is. More specific paths override less specific paths.
710 ## When conflicting rules at the same level of path hierarchy
711 ## (with different roles) are discovered, the grant is given precedence
712 ## over the deny. Note that more-specific denies will still
713 ## override.
714 117 my $permtype = 'subpages';
715 117 foreach my $i (0 .. $#paths_to_check) {
716 171 my $path = $paths_to_check[$i];
717 171 ▶ if ($i == $#paths_to_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#paths_to_check) 117 54 171
718 117 $permtype = 'page';
719 }
720 171 foreach my $role (@role_ids) {
721 342 ▶ if ( exists($permdata->{$path})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $permdata->{$path} and exists $permdata->{$path}{$role} and exists $permdata->{$path}{$role}{$permtype}) 119 223 342
Condition83%5/6
exists $permdata->{$path} and exists $permdata->{$path}{$role} and exists $permdata->{$path}{$role}{$permtype}
!ll&&!rl&&r
2230119
exists $permdata->{$path} and exists $permdata->{$path}{$role}
!ll&&!rl&&r
82141119
MC/DC66%2/3
exists $permdata->{$path} and exists $permdata->{$path}{$role} and exists $permdata->{$path}{$role}{$permtype}
exists $permdata->{$path} exists $permdata->{$path}{$role} exists $permdata->{$path}{$role}{$permtype}
Truth table75%3/4
A and B and C
A= exists $permdata->{$path}B= exists $permdata->{$path}{$role}C= exists $permdata->{$path}{$role}{$permtype}
ABCresult
0XX0
10X0
1100
1111
722 && exists($permdata->{$path}{$role})
723 && exists($permdata->{$path}{$role}{$permtype}))
724 {
725  
726 119 my $len = length($path);
727  
728 119 foreach my $perm (keys %{$permdata->{$path}{$role}{$permtype}}) {
729  
730 ## if the xxxx_allowed column is null, this permission is ignored.
731 595 ▶ if (defined($permdata->{$path}{$role}{$permtype}{$perm})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $permdata->{$path}{$role}{$permtype}{$perm}) 595 0 595
732 595 ▶ if ($len == $rulescomparison{$perm}{'len'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len == $rulescomparison{$perm}{'len'}) { } 0 595 595
elsif ($len > $rulescomparison{$perm}{'len'}) { } 595 0 595
733 0 ▶ if ($permdata->{$path}{$role}{$permtype}{$perm} eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($permdata->{$path}{$role}{$permtype}{$perm} eq "yes") 0 0 0
734 0 $rulescomparison{$perm}{'allowed'} = 1;
735 0 $rulescomparison{$perm}{'len'} = $len;
736 0 $rulescomparison{$perm}{'role'} = $role;
737 }
738 }
739 elsif ($len > $rulescomparison{$perm}{'len'}) {
740 595 ▶ if ($permdata->{$path}{$role}{$permtype}{$perm} eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($permdata->{$path}{$role}{$permtype}{$perm} eq 'yes') { } 365 230 595
741 365 $rulescomparison{$perm}{'allowed'} = 1;
742 }
743 else {
744 230 $rulescomparison{$perm}{'allowed'} = 0;
745 }
746 595 $rulescomparison{$perm}{'len'} = $len;
747 595 $rulescomparison{$perm}{'role'} = $role;
748 }
749 }
750 }
751 }
752 }
753 }
754  
755 my %perms
756 117 = map { $_ => $rulescomparison{$_}{'allowed'} } keys %rulescomparison;
757  
758 117 return \%perms;
759 }
760  
761 =head2 check_view_permission
762  
763 Check if a user can view a path.
764  
765 =cut
766  
767 36 ▶sub check_view_permission {
Subroutine check_view_permission: called CC=5 SCAR=17.4
Pod documented
768 36 my $c = shift;
769  
770 36 ▶ return 1 unless $c->pref('check_permission_on_view');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->pref("check_permission_on_view") 0 36 36
771  
772 36 my $user;
773 36 ▶ if ($c->user_exists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->user_exists) 14 22 36
774 14 $user = $c->user->obj;
775 }
776  
777 36 ▶ $c->log->info('Checking permissions') if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 36 36
778  
779 36 my $perms = $c->check_permissions($c->stash->{path}, $user);
780 36 ▶ if (!$perms->{view}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($perms->{"view"}) 0 36 36
781 $c->stash->{message}
782 0 = $c->loc('Permission Denied to view x', $c->stash->{page}->name);
783 0 $c->stash->{template} = 'message.tt';
784 0 return;
785 }
786  
787 36 return 1;
788 }
789  
790 35 my $search_setup_failed = 0;
791  
792 35 ▶MojoMojo->config->{index_dir} ||= MojoMojo->path_to('index');
Condition33%1/3
"MojoMojo"->config->{"index_dir"} ||= "MojoMojo"->path_to("index")
l!l&&r!l&&!r
0350
MC/DC0%0/2
"MojoMojo"->config->{"index_dir"} ||= "MojoMojo"->path_to("index")
"MojoMojo"->config->{"index_dir"} "MojoMojo"->path_to("index")
Truth table33%1/3
A ||= B
A= "MojoMojo"->config->{"index_dir"}B= "MojoMojo"->path_to("index")
ABresult
1X1
011
000
793 35 ▶MojoMojo->config->{attachment_dir} ||= MojoMojo->path_to('uploads');
Condition33%1/3
"MojoMojo"->config->{"attachment_dir"} ||= "MojoMojo"->path_to("uploads")
l!l&&r!l&&!r
3500
MC/DC0%0/2
"MojoMojo"->config->{"attachment_dir"} ||= "MojoMojo"->path_to("uploads")
"MojoMojo"->config->{"attachment_dir"} "MojoMojo"->path_to("uploads")
Truth table33%1/3
A ||= B
A= "MojoMojo"->config->{"attachment_dir"}B= "MojoMojo"->path_to("uploads")
ABresult
1X1
011
000
794 35 ▶MojoMojo->config->{root} ||= MojoMojo->path_to('root');
Condition33%1/3
"MojoMojo"->config->{"root"} ||= "MojoMojo"->path_to("root")
l!l&&r!l&&!r
3500
MC/DC0%0/2
"MojoMojo"->config->{"root"} ||= "MojoMojo"->path_to("root")
"MojoMojo"->config->{"root"} "MojoMojo"->path_to("root")
Truth table33%1/3
A ||= B
A= "MojoMojo"->config->{"root"}B= "MojoMojo"->path_to("root")
ABresult
1X1
011
000
795 35 ▶unless (-e MojoMojo->config->{index_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "MojoMojo"->config->{"index_dir"}) 1 34 35
796 1 ▶ if (not mkdir MojoMojo->config->{index_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir "MojoMojo"->config->{"index_dir"}) 0 1 1
797 warn 'Could not make index directory <'
798 . MojoMojo->config->{index_dir}
799 0 . '> - FIX IT OR SEARCH WILL NOT WORK!';
800 0 $search_setup_failed = 1;
801 }
802 }
803 35 ▶unless (-w MojoMojo->config->{index_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w "MojoMojo"->config->{"index_dir"}) 0 35 35
804 warn 'Require write access to index <'
805 . MojoMojo->config->{index_dir}
806 0 . '> - FIX IT OR SEARCH WILL NOT WORK!';
807 0 $search_setup_failed = 1;
808 }
809  
810 MojoMojo->model('Search')->prepare_search_index()
811 35 ▶ if not -f MojoMojo->config->{index_dir} . '/segments'
Branch: 1 branch
BranchTrueFalseTotal
unless -f "MojoMojo"->config->{"index_dir"} . '/segments' or $search_setup_failed or "MojoMojo"->pref("disable_search") 1 34 35
Condition66%4/6
-f "MojoMojo"->config->{"index_dir"} . '/segments' or $search_setup_failed or "MojoMojo"->pref("disable_search")
l!l&&r!l&&!r
3401
-f "MojoMojo"->config->{"index_dir"} . '/segments' or $search_setup_failed
l!l&&r!l&&!r
3401
MC/DC33%1/3
-f "MojoMojo"->config->{"index_dir"} . '/segments' or $search_setup_failed or "MojoMojo"->pref("disable_search")
-f "MojoMojo"->config->{"index_dir"} . '/segments' $search_setup_failed "MojoMojo"->pref("disable_search")
Truth table50%2/4
A or B or C
A= -f "MojoMojo"->config->{"index_dir"} . '/segments'B= $search_setup_failedC= "MojoMojo"->pref("disable_search")
ABCresult
1XX1
01X1
0011
0000
812 and not $search_setup_failed
813 and not MojoMojo->pref('disable_search');
814  
815 35 ▶unless (-e MojoMojo->config->{attachment_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "MojoMojo"->config->{"attachment_dir"}) 2 33 35
816 mkdir MojoMojo->config->{attachment_dir}
817 or die 'Could not make attachment directory <'
818 2 ▶ . MojoMojo->config->{attachment_dir} . '>';
Branch: 1 branch
BranchTrueFalseTotal
mkdir "MojoMojo"->config->{"attachment_dir"} or die 'Could not make attachment directory <' . "MojoMojo"->config->{"attachment_dir"} . '>' 2 0 2
819 }
820 die 'Require write access to attachment_dir: <'
821 . MojoMojo->config->{attachment_dir} . '>'
822 35 ▶ unless -w MojoMojo->config->{attachment_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -w "MojoMojo"->config->{"attachment_dir"} 0 35 35
823  
824 35 1;
825  
826 =head1 SUPPORT
827  
828 =over
829  
830 =item *
831  
832 L<http://mojomojo.org>
833  
834 =item *
835  
836 IRC: L<irc://irc.perl.org/mojomojo>.
837  
838 =item *
839  
840 Mailing list: L<http://mojomojo.2358427.n2.nabble.com/>
841  
842 =item *
843  
844 Commercial support and customization for MojoMojo is also provided by Nordaaker
845 Ltd. Contact C<arneandmarcus@nordaaker.com> for details.
846  
847 =back
848  
849 =head1 AUTHORS
850  
851 Marcus Ramberg C<marcus@nordaaker.com>
852  
853 David Naughton C<naughton@umn.edu>
854  
855 Andy Grundman C<andy@hybridized.org>
856  
857 Jonathan Rockway C<jrockway@jrockway.us>
858  
859 A number of other contributors over the years:
860 https://www.ohloh.net/p/mojomojo/contributors
861  
862 =head1 COPYRIGHT
863  
864 Unless explicitly stated otherwise, all modules and scripts in this distribution are:
865 Copyright 2005-2010, Marcus Ramberg
866  
867 =head1 LICENSE
868  
869 You may distribute this code under the same terms as Perl itself.
870  
871 =cut
« lib/MojoMojo/Controller/Jsrpc.pm ↑ Summary lib/Text/Wikispaces2Markdown.pm »