Rubric/WebApp.pm

filter: Statementstmt 67.7% 260 / 384 Branchbran 34.7% 48 / 138 Conditioncond 31.9% 31 / 97 MC/DCmcdc 7.2% 4 / 55 Subroutinesub 74.6% 47 / 63 Podpod 100% 41 / 41 totaltotal 55.4% 431 / 778 CC 111 SCAR 29.5
CC
111
Cov
55%
validate_prefs
90.0
post
43.2
get_link
42.0
CRAP 19.2
« Rubric/CLI/Command/user.pm ↑ Summary Rubric/DBI/Setup.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Rubric::WebApp 0.157;
4 # ABSTRACT: the web interface to Rubric
5  
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod use strict;
9 #pod use warnings;
10 #pod use Rubric::WebApp;
11 #pod Rubric::WebApp->new->run();
12 #pod
13 #pod It's a CGI::Application!
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod Rubric::WebApp provides a CGI-based interface to Rubric data. It's built on
18 #pod top of CGI::Application, which does most of the boring work. This module's
19 #pod code sets up the dispatch tables and implements the responses to various
20 #pod queries.
21 #pod
22 #pod =head1 REQUESTS DISPATCH
23 #pod
24 #pod Requests are I<mostly> path-based, though some involve form submission. The
25 #pod basic dispatch table looks something like this:
26 #pod
27 #pod request | description | method called
28 #pod -------------+-------------------------------------------+--------------
29 #pod /login | log in to a user account | login
30 #pod /logout | log out | logout
31 #pod /preferences | view or change account settings | preferences
32 #pod /newuser | create a new user account | newuser
33 #pod /verify | verify a pending user account | verify
34 #pod /link | view the details of entries for a link | link
35 #pod /post | post or edit an entry (must be logged in) | post
36 #pod /edit | edit an entry (must be logged in) | edit
37 #pod /delete | delete an entry (must be logged in) | delete
38 #pod /entry/ENTRY | find and display the identified entry | entry
39 #pod /entries/Q | find and display results for query Q | entries
40 #pod /~USER/TAGS | see a user's entries for given tags | (in flux)
41 #pod /doc/PAGE | view the named page in the documentation | doc
42 #pod /style/PAGE | get the named style sheet | style
43 #pod
44 #pod If the system is private and no user is logged in, the default action is to
45 #pod display a login screen. If the system is public, or a user is logged in, the
46 #pod default action is to display entries.
47 #pod
48 #pod =cut
49  
50 3 ▶use CGI::Application 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use base qw(CGI::Application);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use CGI::Carp qw(fatalsToBrowser);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 3 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶use Encode 2 qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 3 ▶use HTML::TagCloud;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 3 ▶use Email::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use Email::Sender::Simple qw(sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 3 ▶use Rubric::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use Rubric::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 ▶use Rubric::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 ▶use Rubric::WebApp::URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use Rubric::WebApp::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 3 ▶use String::Truncate qw(elide);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 #pod =head1 METHODS
72 #pod
73 #pod =head2 redirect($uri, $message)
74 #pod
75 #pod This method simplifies redirection; it redirects to the given URI, printing the
76 #pod given message as the body of the HTTP response.
77 #pod
78 #pod =cut
79  
80 5 ▶sub redirect {
Subroutine redirect: called CC=1 SCAR=0.0
Pod documented
81 5 my ($self, $uri, $message) = @_;
82  
83 5 $self->header_type('redirect');
84 5 $self->header_props(-url=> $uri);
85 5 return $message;
86 }
87  
88 #pod =head2 redirect_root($message)
89 #pod
90 #pod This is shorthand to redirect to the Rubric's root URI. It calls C<redirect>.
91 #pod
92 #pod =cut
93  
94 2 ▶sub redirect_root {
Subroutine redirect_root: called CC=1 SCAR=0.0
Pod documented
95 2 my ($self, $message) = @_;
96  
97 2 return $self->redirect(Rubric::Config->uri_root, $message);
98 }
99  
100 #pod =head2 cgiapp_init
101 #pod
102 #pod This method is called during CGI::Application's initialization. It sets up the
103 #pod session configuration.
104 #pod
105 #pod =cut
106  
107 48 ▶sub cgiapp_init {
Subroutine cgiapp_init: called CC=2 SCAR=7.0
Pod documented
108 48 my ($self) = @_;
109  
110 48 my $login_class = Rubric::Config->login_class;
111  
112 ## no critic (StringyEval)
113 48 ▶ eval("require $login_class; 1") or die;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $login_class; 1" or die() 48 0 48
114 ## use critic
115 48 $login_class->check_for_login($self);
116 }
117  
118 #pod =head2 cgiapp_prerun
119 #pod
120 #pod This method is called before the selected runmode. It checks for a login,
121 #pod checks for updates to result-set paging, and starts processing the request
122 #pod path.
123 #pod
124 #pod =cut
125  
126 48 ▶sub cgiapp_prerun {
Subroutine cgiapp_prerun: called CC=1 SCAR=0.0
Pod documented
127 48 my ($self) = @_;
128  
129 48 $self->check_pager_data;
130  
131 48 my @path = split '/', $self->query->path_info;
132 48 $self->param(path => [ @path[ 2 .. $#path ] ]);
133 }
134  
135 #pod =head2 next_path_part
136 #pod
137 #pod This method shifts the next item off of the request path and returns it.
138 #pod
139 #pod =cut
140  
141 80 ▶sub next_path_part {
Subroutine next_path_part: called CC=1 SCAR=0.0
Pod documented
142 80 my ($self) = @_;
143 80 shift @{$self->param('path')};
144 }
145  
146 #pod =head2 check_pager_data
147 #pod
148 #pod This method is called by C<cgiapp_init>, and sets up parameters used for paging
149 #pod entry listings. The following parameters are used:
150 #pod
151 #pod per_page - how many items per page; default 25, maximum 100; stored in session;
152 #pod page - which page to display; default 1
153 #pod
154 #pod =cut
155  
156 48 ▶sub check_pager_data {
Subroutine check_pager_data: called CC=5 SCAR=17.0
Pod documented
157 48 my ($self) = @_;
158  
159 48 ▶ $self->session->param('per_page', int(
Condition66%4/6
$self->query->param('per_page') || $self->session->param('per_page') || 'Rubric::Config'->default_page_size
l!l&&r!l&&!r
4710
$self->query->param("per_page") or $self->session->param("per_page")
l!l&&r!l&&!r
0471
MC/DC0%0/3
$self->query->param('per_page') || $self->session->param('per_page') || 'Rubric::Config'->default_page_size
$self->query->param("per_page") $self->session->param("per_page") 'Rubric::Config'->default_page_size
Truth table50%2/4
A or B || C
A= $self->query->param("per_page")B= $self->session->param("per_page")C= 'Rubric::Config'->default_page_size
ABCresult
1XX1
01X1
0011
0000
160 $self->query->param('per_page')
161 || $self->session->param('per_page')
162 || Rubric::Config->default_page_size
163 ));
164  
165 48 ▶ $self->session->param('per_page', Rubric::Config->max_page_size)
Branch: 1 branch
BranchTrueFalseTotal
if $self->session->param("per_page") > "Rubric::Config"->max_page_size 0 48 48
166 if $self->session->param('per_page') > Rubric::Config->max_page_size;
167  
168 48 $self->param('per_page', $self->session->param('per_page'));
169 48 ▶ $self->param('page', int(($self->query->param('page') || 1)));
Condition50%1/2
$self->query->param('page') || 1
l!l
048
MC/DC0%0/1
$self->query->param('page') || 1
$self->query->param('page')
Truth table50%1/2
A
A= $self->query->param('page')
Aresult
11
00
170 }
171  
172 #pod =head2 template($template, \%variables)
173 #pod
174 #pod This method is used to render a template with both provided and default
175 #pod variables.
176 #pod
177 #pod Templates are rendered by calling the C<process> method on the template
178 #pod renderer, which is retrieved by calling the C<renderer> method on the WebApp.
179 #pod
180 #pod The following variables are passed by default:
181 #pod
182 #pod current_user - the currently logged-in user (a Rubric::User object)
183 #pod per_page - entries per page (see check_pager_data)
184 #pod page - which page (see check_pager_data)
185 #pod
186 #pod =cut
187  
188 42 ▶sub template {
Subroutine template: called CC=4 SCAR=13.9
Pod documented
189 42 my ($self, $template, $stash) = @_;
190 42 ▶ $stash ||= {};
Condition100%2/2
$stash ||= {}
l!l
411
MC/DC100%1/1
$stash ||= {}
$stash
Truth table100%2/2
A
A= $stash
Aresult
11
00
191 42 $stash->{current_user} = $self->param('current_user');
192 42 $stash->{per_page} = $self->param('per_page');
193 42 $stash->{page} = $self->param('page');
194  
195 42 my $type = $self->query->param('format');
196 42 ▶ $type = 'html' unless $type and $type =~ /^[\pL\d_]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $type =~ /^[\pL\d_]+$/ 40 2 42
Condition66%2/3
$type and $type =~ /^[\pL\d_]+$/
!ll&&!rl&&r
4002
MC/DC50%1/2
$type and $type =~ /^[\pL\d_]+$/
$type $type =~ /^[\pL\d_]+$/
Truth table66%2/3
A and B
A= $typeB= $type =~ /^[\pL\d_]+$/
ABresult
0X0
100
111
197  
198 42 my ($content_type, $output) =
199 Rubric::Renderer->process($template, $type, $stash);
200  
201 42 $self->header_add(-type => $content_type);
202 42 return $output;
203 }
204  
205 #pod =head2 setup
206 #pod
207 #pod This method, called by CGI::Application's initialization process, sets up
208 #pod the dispatch table for requests, as described above.
209 #pod
210 #pod =cut
211  
212 48 ▶sub setup {
Subroutine setup: called CC=3 SCAR=11.1
Pod documented
213 48 my ($self) = @_;
214  
215 48 $self->mode_param(path_info => 1);
216  
217 48 $self->start_mode('login');
218 48 $self->run_modes([ qw(style doc login newuser reset_password verify) ]);
219  
220 48 ▶ if ($self->param('current_user') or not Rubric::Config->private_system) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("current_user") or not "Rubric::Config"->private_system) 48 0 48
Condition66%2/3
$self->param("current_user") or not "Rubric::Config"->private_system
l!l&&r!l&&!r
17310
MC/DC0%0/2
$self->param("current_user") or not "Rubric::Config"->private_system
$self->param("current_user") not "Rubric::Config"->private_system
Truth table66%2/3
A or not(B)
A= $self->param("current_user")B= not "Rubric::Config"->private_system
ABresult
1X1
011
000
221 48 $self->start_mode('entries');
222 48 $self->run_modes([
223 qw(delete edit entries entry link logout post preferences tag_cloud calendar)
224 ]);
225 }
226  
227 48 $self->run_modes(AUTOLOAD => '_default_handler');
228 }
229  
230 5 ▶sub _default_handler {
Subroutine _default_handler: called CC=2 SCAR=6.9
231 5 my ($self, $runmode) = @_;
232 5 ▶ if (substr($runmode, 0, 1) eq '~') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($runmode, 0, 1) eq "~") 4 1 5
233 4 return $self->_entries_shortcut(substr($runmode, 1));
234 }
235 1 $self->redirect_root;
236 }
237  
238 4 ▶sub _entries_shortcut {
Subroutine _entries_shortcut: called CC=2 SCAR=6.9
239 4 my ($self, $user) = @_;
240 4 my $path = $self->param('path');
241  
242 # If there the number of elements in the path is odd, the first one is tags;
243 # otherwise, it's a normal query; this may or may not be safe, in the end. I
244 # guess we'll find out. -- rjbs, 2006-02-20
245 4 ▶ unshift @$path, 'tags' if @$path % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$path % 2 1 3 4
246 4 unshift @$path, 'user', $user;
247  
248 4 $self->entries;
249 }
250  
251 #pod =head2 entries
252 #pod
253 #pod This passes off responsibility to the class named in the C<entries_query_class>
254 #pod configuration option. This option defaults to Rubric::WebApp::Entries.
255 #pod
256 #pod =cut
257  
258 30 ▶sub entries {
Subroutine entries: called CC=2 SCAR=7.0
Pod documented
259 30 my ($self) = @_;
260  
261 30 my $entries_class = Rubric::Config->entries_query_class;
262 ## no critic (StringyEval)
263 30 ▶ die $@ unless eval "require $entries_class";
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require $entries_class" 0 30 30
264 ## use critic
265 30 $entries_class->entries($self);
266 }
267  
268 #pod =head2 entry
269 #pod
270 #pod This displays the single requested entry.
271 #pod
272 #pod =cut
273  
274 2 ▶sub entry {
Subroutine entry: called CC=5 SCAR=19.6
Pod documented
275 2 my ($self) = @_;
276  
277 2 my $entry = $self->get_entry;
278  
279 2 ▶ return $self->template('no_entry', { reason => 'missing' }) unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 2 2
280  
281 return $self->template('no_entry', { reason => 'access' })
282 2 ▶ if grep { $_ eq Rubric::Config->private_tag } $entry->tags
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "Rubric::Config"->private_tag;} $entry->tags and !$self->param("current_user") || $entry->user ne $self->param("current_user") 0 2 2
Condition16%1/6
grep {$_ eq "Rubric::Config"->private_tag;} $entry->tags and !$self->param("current_user") || $entry->user ne $self->param("current_user")
!ll&&!rl&&r
200
not $self->param("current_user") or $entry->user ne $self->param("current_user")
l!l&&r!l&&!r
000
MC/DC0%0/3
grep {$_ eq "Rubric::Config"->private_tag;} $entry->tags and !$self->param("current_user") || $entry->user ne $self->param("current_user")
grep {$_ eq "Rubric::Config"->private_tag;} $entry->tags not $self->param("current_user") $entry->user ne $self->param("current_user")
Truth table25%1/4
A and not(B) or C
A= grep {$_ eq "Rubric::Config"->private_tag;} $entry->tagsB= not $self->param("current_user")C= $entry->user ne $self->param("current_user")
ABCresult
0XX0
1000
11X1
1011
283 and (not $self->param('current_user')
284 or $entry->user ne $self->param('current_user'));
285  
286 2 $self->template('entry_long' => {
287 entry => $self->param('entry'),
288 self_url => $self->query->self_url(),
289 # FIX ME: hack to put the title of the entry in the <title> tag
290 query_description => $entry->title,
291 long_form => 1
292 });
293 }
294  
295 #pod =head2 get_entry
296 #pod
297 #pod This method gets the next part of the path, assumes it to be a Rubric::Entry
298 #pod id, and puts the corresponding entry in the "entry" parameter.
299 #pod
300 #pod =cut
301  
302 4 ▶sub get_entry {
Subroutine get_entry: called CC=1 SCAR=0.0
Pod documented
303 4 my ($self) = @_;
304  
305 4 my $entry = Rubric::Entry->retrieve($self->next_path_part);
306 4 $self->param(entry => $entry);
307 }
308  
309 #pod =head2 link
310 #pod
311 #pod This runmode displays entries that point to a given link, identified either by
312 #pod URI or MD5 sum.
313 #pod
314 #pod =cut
315  
316 0 ▶sub link {
Subroutine link: not called CC=2 SCAR=17.9
Pod documented
317 0 my ($self) = @_;
318 0 ▶ return $self->redirect_root("...no such link") unless $self->get_link;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_link 0 0 0
319 0 $self->display_entries;
320 }
321  
322 #pod =head2 get_link
323 #pod
324 #pod This method look for a C<uri> or, failing that, C<url> query parameter. If
325 #pod found, it finds a Rubric::Link for that URI and puts it in the "link"
326 #pod parameter.
327 #pod
328 #pod =cut
329  
330 0 ▶sub get_link {
Subroutine get_link: not called CC=6 SCAR=37.4
Pod documented
331 0 my ($self) = @_;
332 0 my %search;
333 0 $search{md5} = $self->query->param('md5');
334 0 ▶ $search{uri} = $self->query->param('uri') || $self->query->param('url');
Condition0%0/3
$self->query->param('uri') || $self->query->param('url')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->query->param('uri') || $self->query->param('url')
$self->query->param('uri') $self->query->param('url')
Truth table0%0/3
A || B
A= $self->query->param('uri')B= $self->query->param('url')
ABresult
1X1
011
000
335 0 for (qw(md5sum uri)) {
336 0 ▶ delete $search{$_} unless $search{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $search{$_} 0 0 0
337 }
338 0 ▶ return unless %search;
Branch: 1 branch
BranchTrueFalseTotal
unless %search 0 0 0
339 0 ▶ return unless my ($link) = Rubric::Link->search(\%search);
Branch: 1 branch
BranchTrueFalseTotal
unless my($link) = "Rubric::Link"->search(\%search) 0 0 0
340 0 $self->param('link', $link);
341 }
342  
343 #pod =head2 tag_cloud
344 #pod
345 #pod =cut
346  
347 1 ▶sub tag_cloud {
Subroutine tag_cloud: called CC=2 SCAR=6.9
Pod documented
348 1 my ($self, $options) = @_;
349  
350 1 my $tags = Rubric::DBI->db_Main->selectall_arrayref(
351 "SELECT tag, count(*)
352 FROM entrytags
353 WHERE tag not like '@%'
354 GROUP BY tag
355 ORDER BY tag");
356  
357 1 my $cloud = HTML::TagCloud->new();
358 1 foreach my $tag (@$tags) {
359 6 my $href = Rubric::WebApp::URI->entries({tags => [ $tag->[0] ]});
360 6 $cloud->add($tag->[0], $href, $tag->[1]);
361 }
362  
363 1 return $self->template('tag_cloud' => {
364 cloud => $cloud,
365 query_description => 'All Tags',
366 });
367  
368 }
369  
370 #pod =head2 calendar
371 #pod
372 #pod =cut
373  
374 1 ▶sub calendar {
Subroutine calendar: called CC=6 SCAR=18.2
Pod documented
375 1 my ($self, $options) = @_;
376 1 my $path = $self->param('path');
377  
378 1 require HTML::CalendarMonth;
379  
380 1 my $year = shift @$path;
381 1 my $month = shift @$path;
382  
383 1 ▶ if (not ($year or $month)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($year or $month) 1 0 1
Condition33%1/3
$year or $month
l!l&&r!l&&!r
001
MC/DC0%0/2
$year or $month
$year $month
Truth table33%1/3
A or B
A= $yearB= $month
ABresult
1X1
011
000
384 1 ($month, $year) = (localtime)[4,5];
385 1 $month++;
386 1 $year += 1900;
387 }
388 1 my $calendar = HTML::CalendarMonth->new(
389 month => $month,
390 year => $year,
391 full_days => 1
392 );
393 1 $calendar->item($calendar->year, $calendar->month)->attr(
394 style => 'background-color: #EEEEEE'
395 );
396 1 $calendar->attr(class => 'calendar');
397 1 $calendar->alldays->attr(class => 'day');
398 1 my $num_span = HTML::Element->new('span', class => 'day_indicator');
399 1 $calendar->alldays->attr(class => 'day');
400 1 $calendar->alldays->wrap_content($num_span);
401 1 $calendar->allheaders->attr(class => 'headers');
402  
403 1 my $start = DateTime->new(
404 year => $year,
405 month => $month,
406 day => 1,
407 hour => 0,
408 minute => 0,
409 second => 0,
410 nanosecond => 0,
411 time_zone => '-1700'
412 )->epoch;
413  
414 1 my $end = DateTime->new(
415 year => $year,
416 month => $month,
417 day => $calendar->lastday,
418 hour => 23,
419 minute => 59,
420 second => 59,
421 nanosecond => 0,
422 time_zone => '-1700'
423 )->epoch;
424  
425 1 my $entries = Rubric::Entry->retrieve_from_sql(qq{
426 WHERE id NOT IN (SELECT entry FROM entrytags WHERE tag = '\@private')
427 AND created > '$start'
428 AND created < '$end'
429 ORDER BY created}
430 );
431  
432 1 ▶ while (my $entry = $entries->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $entries->next) 3 1 4
433 3 my ($day) = $entry->created->day_of_month;
434 3 my $a = HTML::Element->new('a');
435 3 my $div = HTML::Element->new('div');
436 3 my $title = $entry->title;
437 3 $a->attr(title => $title);
438 3 $a->attr(href => Rubric::WebApp::URI->entry($entry));
439 3 $title = elide($title, 18);
440 3 $a->push_content($title);
441 3 $div->push_content($a);
442 3 $calendar->item($day)->push_content($div);
443 }
444  
445 1 my $prev_month = $month;
446 1 my $prev_year = $year;
447 1 $prev_month --;
448 1 ▶ if (not $prev_month) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev_month) 0 1 1
449 0 $prev_month = 12;
450 0 $prev_year--;
451 }
452  
453 1 my $next_month = $month;
454 1 my $next_year = $year;
455 1 $next_month++;
456 1 ▶ if ($next_month > 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_month > 12) 0 1 1
457 0 $next_month = 1;
458 0 $next_year++;
459 }
460  
461 1 return $self->template('calendar' => {
462 calendar => $calendar,
463 prev_link => {
464 month => sprintf("%02d", $prev_month),
465 year => $prev_year,
466 },
467 next_link => {
468 month => sprintf("%02d", $next_month),
469 year => $next_year,
470 },
471 query_description => 'Calendar',
472 });
473  
474 }
475  
476 #pod =head2 login
477 #pod
478 #pod If the user is logged in, this request is immediately redirected to the root of
479 #pod the Rubric site. Otherwise, a login form is provided.
480 #pod
481 #pod =cut
482  
483 5 ▶sub login {
Subroutine login: called CC=6 SCAR=22.5
Pod documented
484 5 my ($self) = @_;
485  
486 5 ▶ if ($self->param('current_user')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("current_user")) 2 3 5
487 2 ▶ my $goto = $self->query->param('then_goto') || Rubric::Config->uri_root;
Condition33%1/3
$self->query->param('then_goto') || 'Rubric::Config'->uri_root
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->query->param('then_goto') || 'Rubric::Config'->uri_root
$self->query->param('then_goto') 'Rubric::Config'->uri_root
Truth table33%1/3
A || B
A= $self->query->param('then_goto')B= 'Rubric::Config'->uri_root
ABresult
1X1
011
000
488 2 return $self->redirect($goto, "Logged in...");
489 }
490  
491 3 my $note;
492 3 ▶ if ($self->get_current_runmode ne 'login') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_current_runmode ne "login") 0 3 3
493 0 $note = "You must log in to use this feature.";
494 0 $self->query->param('then_goto', $self->query->self_url);
495 }
496  
497 3 ▶ if (Rubric::Config->secure_login) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rubric::Config"->secure_login) 0 3 3
498 0 ▶ if ($self->query->env->{'psgi.url_scheme'} ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->env->{"psgi.url_scheme"} ne "https") 0 0 0
499 0 my $goto = Rubric::WebApp::URI->login;
500 0 return $self->redirect($goto, "Logged in...");
501 }
502 }
503  
504 3 $self->template('login' => {
505 note => $note,
506 then_goto => scalar $self->query->param('then_goto'),
507 user => scalar $self->query->param('user'),
508 user_pending => scalar $self->param('user_pending')
509 });
510 }
511  
512 #pod =head2 logout
513 #pod
514 #pod This run mode unsets the "current_user" parameter in the session and the WebApp
515 #pod object, then redirects the user to the root of the Rubric site.
516 #pod
517 #pod =cut
518  
519 1 ▶sub logout {
Subroutine logout: called CC=1 SCAR=0.0
Pod documented
520 1 my ($self) = @_;
521 1 $self->session->clear('current_user');
522 1 $self->param('current_user', undef);
523  
524 1 return $self->redirect_root("Logged out...");
525 }
526  
527 #pod =head2 reset_password
528 #pod
529 #pod This run mode allows a user to request that his password be reset and emailled
530 #pod to him.
531 #pod
532 #pod =cut
533  
534 0 ▶sub reset_password {
Subroutine reset_password: not called CC=6 SCAR=37.4
Pod documented
535 0 my ($self) = @_;
536 0 ▶ my $user = $self->get_user
Condition0%0/6
$self->get_user || $self->query->param('user') && 'Rubric::User'->retrieve($self->query->param('user'))
l!l&&r!l&&!r
000
$self->query->param("user") and "Rubric::User"->retrieve($self->query->param("user"))
!ll&&!rl&&r
000
MC/DC0%0/3
$self->get_user || $self->query->param('user') && 'Rubric::User'->retrieve($self->query->param('user'))
$self->get_user $self->query->param("user") "Rubric::User"->retrieve($self->query->param("user"))
Truth table0%0/4
A || B and C
A= $self->get_userB= $self->query->param("user")C= "Rubric::User"->retrieve($self->query->param("user"))
ABCresult
1XX1
0111
00X0
0100
537 || $self->query->param('user')
538 && Rubric::User->retrieve($self->query->param('user'));
539 0 my $reset_code = $self->get_reset_code;
540  
541 0 ▶ return $self->template("reset_login") unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
542  
543 0 ▶ return $self->setup_reset_code($user) unless $reset_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $reset_code 0 0 0
544  
545 0 ▶ if (my $password = $user->reset_password($reset_code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $password = $user->reset_password($reset_code)) { } 0 0 0
546 0 $self->template("reset", { password => $password });
547 } else {
548 0 return $self->template("reset_error");
549 }
550  
551 }
552  
553 #pod =head2 setup_reset_code
554 #pod
555 #pod This routine gets a reset code for the user and emails it to him.
556 #pod
557 #pod =cut
558  
559 0 ▶sub setup_reset_code {
Subroutine setup_reset_code: not called CC=1 SCAR=6.9
Pod documented
560 0 my ($self, $user) = @_;
561  
562 0 my $reset_code = $user->randomize_reset_code;
563  
564 0 $self->send_reset_email_to($user, $reset_code);
565 0 $self->template("reset_sent");
566 }
567  
568 #pod =head2 preferences
569 #pod
570 #pod This method displays account information for the current user. Some account
571 #pod settings may be changed.
572 #pod
573 #pod =cut
574  
575 0 ▶sub preferences {
Subroutine preferences: not called CC=4 SCAR=30.0
Pod documented
576 0 my ($self) = @_;
577  
578 0 ▶ return $self->login unless $self->param('current_user');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param("current_user") 0 0 0
579  
580 0 ▶ return $self->template("preferences")
Branch: 1 branch
BranchTrueFalseTotal
unless my(%prefs) = $self->_get_prefs_form 0 0 0
581 unless my %prefs = $self->_get_prefs_form;
582  
583 0 ▶ if (my %errors = $self->validate_prefs(\%prefs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%errors) = $self->validate_prefs(\%prefs)) 0 0 0
584 0 return $self->template("preferences", { %prefs, %errors } );
585 }
586  
587 0 $self->update_user(\%prefs);
588 }
589  
590 #pod =head2 update_user(\%prefs)
591 #pod
592 #pod This method will update the current user object with the changes in C<%prefs>,
593 #pod which is passed by the C<preferences> method.
594 #pod
595 #pod =cut
596  
597 0 ▶sub update_user {
Subroutine update_user: not called CC=3 SCAR=24.8
Pod documented
598 0 my ($self, $prefs) = @_;
599 0 for ($self->param('current_user')) {
600 0 ▶ $_->password(md5_hex($prefs->{password_1})) if $prefs->{password_1};
Branch: 1 branch
BranchTrueFalseTotal
if $prefs->{"password_1"} 0 0 0
601 0 $_->email($prefs->{email});
602 0 $_->update;
603 }
604 0 $self->redirect_root('updated');
605 }
606  
607 0 ▶sub _get_prefs_form {
Subroutine _get_prefs_form: not called CC=3 SCAR=24.8
608 0 my ($self) = @_;
609  
610 0 my %form;
611 0 for (qw(password password_1 password_2 email)) {
612 0 ▶ $form{$_} = $self->query->param($_) if $self->query->param($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->query->param($_) 0 0 0
613 }
614 0 return %form;
615 }
616  
617 #pod =head2 validate_prefs(\%prefs)
618 #pod
619 #pod Given a set of preference updates from a form submission, this method validates
620 #pod them and returns a description of the validation results. This method will
621 #pod probably be redesigned (possibly with Data::FormValidator) in the future.
622 #pod Don't count on its interface.
623 #pod
624 #pod =cut
625  
626 #pod =begin future
627 #pod
628 #pod sub validate_prefs {
629 #pod my ($self, $prefs) = @_;
630 #pod require Data::FormValidator;
631 #pod
632 #pod my $profile = {
633 #pod required => [qw(password)],
634 #pod optional => [qw(password_1 password_2 email)],
635 #pod constraints => {
636 #pod email => 'email',
637 #pod password_1 => {
638 #pod params => [qw(password_1 password_2)],
639 #pod constraint => sub { $_[0] eq $_[1] },
640 #pod }
641 #pod },
642 #pod dependency_groups => { new_password => [qw(password_1 password_2)] }
643 #pod };
644 #pod
645 #pod my $results = Data::FormValidator->check($prefs, $profile);
646 #pod }
647 #pod
648 #pod =end future
649 #pod
650 #pod =cut
651  
652 0 ▶sub validate_prefs {
Subroutine validate_prefs: not called CC=9 SCAR=45.0
Pod documented
653 0 my ($self, $prefs) = @_;
654 0 my %errors;
655  
656 0 ▶ if (not $prefs->{email}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $prefs->{'email'}) { } 0 0 0
elsif ($prefs->{'email'} and not $prefs->{'email'} =~ /$Email::Address::addr_spec/) { } 0 0 0
657 0 $errors{email_missing} = 1;
658 ▶ } elsif ($prefs->{email} and $prefs->{email} !~ $Email::Address::addr_spec) {
Condition0%0/3
$prefs->{"email"} and not $prefs->{"email"} =~ /$Email::Address::addr_spec/
!ll&&!rl&&r
000
MC/DC0%0/2
$prefs->{"email"} and not $prefs->{"email"} =~ /$Email::Address::addr_spec/
$prefs->{"email"} not $prefs->{"email"} =~ /$Email::Address::addr_spec/
Truth table0%0/3
A and not(B)
A= $prefs->{"email"}B= not $prefs->{"email"} =~ /$Email::Address::addr_spec/
ABresult
0X0
100
111
659 0 undef $prefs->{email};
660 0 $errors{email_invalid} = 1;
661 }
662  
663 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($prefs->{"password_1"} and $prefs->{"password_2"} and $prefs->{"password_1"} ne $prefs->{"password_2"}) 0 0 0
Condition0%0/6
$prefs->{"password_1"} and $prefs->{"password_2"} and $prefs->{"password_1"} ne $prefs->{"password_2"}
!ll&&!rl&&r
000
$prefs->{"password_1"} and $prefs->{"password_2"}
!ll&&!rl&&r
000
MC/DC0%0/3
$prefs->{"password_1"} and $prefs->{"password_2"} and $prefs->{"password_1"} ne $prefs->{"password_2"}
$prefs->{"password_1"} $prefs->{"password_2"} $prefs->{"password_1"} ne $prefs->{"password_2"}
Truth table0%0/4
A and B and C
A= $prefs->{"password_1"}B= $prefs->{"password_2"}C= $prefs->{"password_1"} ne $prefs->{"password_2"}
ABCresult
0XX0
10X0
1100
1111
664 $prefs->{password_1} and $prefs->{password_2}
665 and $prefs->{password_1} ne $prefs->{password_2}
666 ) {
667 0 undef $prefs->{password_1};
668 0 undef $prefs->{password_2};
669 0 $errors{password_mismatch} = 1;
670 }
671  
672 0 ▶ unless ($prefs->{password}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefs->{'password'}) { } 0 0 0
if (do { md5_hex($prefs->{"password"}) ne $self->param("current_user")->password }) 0 0 0
673 0 $errors{password_missing} = 1;
674 0 } elsif (
675 md5_hex($prefs->{password}) ne $self->param('current_user')->password
676 ) {
677 0 $errors{password_wrong} = 1;
678 }
679  
680 0 return %errors;
681 }
682  
683 #pod =head2 newuser
684 #pod
685 #pod If the proper form information is present, this runmode creates a new user
686 #pod account. If not, it presents a form.
687 #pod
688 #pod If a user is already logged in, the user is redirected to the root of the
689 #pod Rubric.
690 #pod
691 #pod =cut
692  
693 1 ▶sub newuser {
Subroutine newuser: called CC=5 SCAR=17.0
Pod documented
694 1 my ($self) = @_;
695  
696 1 ▶ return $self->redirect_root("registration is closed...")
Branch: 1 branch
BranchTrueFalseTotal
if "Rubric::Config"->registration_closed 0 1 1
697 if Rubric::Config->registration_closed;
698  
699 1 ▶ return $self->redirect_root("Already logged in...")
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("current_user") 0 1 1
700 if $self->param('current_user');
701  
702 1 my %newuser;
703 $newuser{$_} = $self->query->param($_)
704 1 for qw(username password_1 password_2 email);
705  
706 1 my %errors = $self->validate_newuser_form(\%newuser);
707 1 ▶ if (%errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (%errors) { } 1 0 1
708 1 $self->template('newuser' => { %newuser, %errors });
709 } else {
710 0 $self->create_newuser(%newuser);
711 }
712 }
713  
714 #pod =head2 validate_newuser_form(\%newuser)
715 #pod
716 #pod Given a set of user data from a form submission, this method validates them and
717 #pod returns a description of the validation results. This method will probably be
718 #pod redesigned (possibly with Data::FormValidator) in the future. Don't count on
719 #pod its interface.
720 #pod
721 #pod =cut
722  
723 1 ▶sub validate_newuser_form {
Subroutine validate_newuser_form: called CC=10 SCAR=35.9
Pod documented
724 1 my ($self, $newuser) = @_;
725 1 my %errors;
726  
727 1 ▶ if ($newuser->{username} and $newuser->{username} !~ /^[\pL\d_.]+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($newuser->{'username'} and not $newuser->{'username'} =~ /^[\pL\d_.]+$/) { } 0 1 1
elsif ('Rubric::User'->retrieve($newuser->{'username'})) { } 0 1 1
Condition33%1/3
$newuser->{"username"} and not $newuser->{"username"} =~ /^[\pL\d_.]+$/
!ll&&!rl&&r
100
MC/DC0%0/2
$newuser->{"username"} and not $newuser->{"username"} =~ /^[\pL\d_.]+$/
$newuser->{"username"} not $newuser->{"username"} =~ /^[\pL\d_.]+$/
Truth table33%1/3
A and not(B)
A= $newuser->{"username"}B= not $newuser->{"username"} =~ /^[\pL\d_.]+$/
ABresult
0X0
100
111
728 0 undef $newuser->{username};
729 0 $errors{username_invalid} = 1;
730 } elsif (Rubric::User->retrieve($newuser->{username})) {
731 0 undef $newuser->{username};
732 0 $errors{username_taken} = 1;
733 }
734  
735 1 ▶ unless ($newuser->{email}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($newuser->{'email'}) { } 0 1 1
if (do { $newuser->{"email"} and not $newuser->{"email"} =~ /$Email::Address::addr_spec/ }) 0 0 0
736 1 $errors{email_missing} = 1;
737 0 ▶ } elsif ($newuser->{email} and $newuser->{email} !~ $Email::Address::addr_spec) {
Condition0%0/3
$newuser->{'email'} && !($newuser->{'email'} =~ /$Email::Address::addr_spec/)
!ll&&!rl&&r
000
MC/DC0%0/2
$newuser->{'email'} && !($newuser->{'email'} =~ /$Email::Address::addr_spec/)
$newuser->{'email'} !($newuser->{'email'} =~ /$Email::Address::addr_spec/)
Truth table0%0/3
A && not(B)
A= $newuser->{'email'}B= !($newuser->{'email'} =~ /$Email::Address::addr_spec/)
ABresult
0X0
100
111
738 0 undef $newuser->{email};
739 0 $errors{email_invalid} = 1;
740 }
741  
742 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($newuser->{"password_1"} and $newuser->{"password_2"} and $newuser->{"password_1"} ne $newuser->{"password_2"}) 0 1 1
Condition33%2/6
$newuser->{"password_1"} and $newuser->{"password_2"} and $newuser->{"password_1"} ne $newuser->{"password_2"}
!ll&&!rl&&r
100
$newuser->{"password_1"} and $newuser->{"password_2"}
!ll&&!rl&&r
100
MC/DC0%0/3
$newuser->{"password_1"} and $newuser->{"password_2"} and $newuser->{"password_1"} ne $newuser->{"password_2"}
$newuser->{"password_1"} $newuser->{"password_2"} $newuser->{"password_1"} ne $newuser->{"password_2"}
Truth table25%1/4
A and B and C
A= $newuser->{"password_1"}B= $newuser->{"password_2"}C= $newuser->{"password_1"} ne $newuser->{"password_2"}
ABCresult
0XX0
10X0
1100
1111
743 $newuser->{password_1} and $newuser->{password_2}
744 and $newuser->{password_1} ne $newuser->{password_2}
745 ) {
746 0 undef $newuser->{password_1};
747 0 undef $newuser->{password_2};
748 0 $errors{password_mismatch} = 1;
749 }
750 1 return %errors;
751 }
752  
753 #pod =head2 create_newuser(\%newuser)
754 #pod
755 #pod This method creates a new user account from the given description. It sends
756 #pod the user a validation email (if needed) and displays an account creation page.
757 #pod
758 #pod =cut
759  
760 0 ▶sub create_newuser {
Subroutine create_newuser: not called CC=2 SCAR=17.9
Pod documented
761 0 my ($self, %newuser) = @_;
762  
763 my %user = (
764 username => $newuser{username},
765 password => md5_hex($newuser{password_1}),
766 email => $newuser{email},
767 0 );
768  
769 0 my $user = Rubric::User->create(\%user);
770  
771 0 ▶ unless (Rubric::Config->skip_newuser_verification) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Rubric::Config"->skip_newuser_verification) 0 0 0
772 0 $user->randomize_verification_code;
773 0 $self->send_verification_email_to($user);
774 }
775  
776 0 $self->template("account_created");
777 }
778  
779 #pod =head2 send_reset_email_to($user)
780 #pod
781 #pod This method sends an email to the given user with a URI to reset his password.
782 #pod
783 #pod =cut
784  
785 0 ▶sub send_reset_email_to {
Subroutine send_reset_email_to: not called CC=1 SCAR=6.9
Pod documented
786 0 my ($self, $user) = @_;
787  
788 0 my $message = Rubric::Renderer->process(
789 'reset_mail',
790 'txt',
791 { user => $user, email_from => Rubric::Config->email_from }
792 );
793  
794 # XXX: This now ignores the smtp_server config.
795 0 sendmail($message);
796 }
797  
798 #pod =head2 send_verification_email_to($user)
799 #pod
800 #pod This method sends a verification email to the given user.
801 #pod
802 #pod =cut
803  
804 0 ▶sub send_verification_email_to {
Subroutine send_verification_email_to: not called CC=1 SCAR=6.9
Pod documented
805 0 my ($self, $user) = @_;
806  
807 0 my $message = Rubric::Renderer->process(
808 'newuser_mail',
809 'txt',
810 { user => $user, email_from => Rubric::Config->email_from }
811 );
812  
813 # XXX: This now ignores the smtp_server config.
814 0 sendmail($message);
815 }
816  
817 #pod =head2 verify
818 #pod
819 #pod This runmode attempts to verify a user account. It expects a request to be
820 #pod in the form: C< /verify/username/verification_code >
821 #pod
822 #pod =cut
823  
824 0 ▶sub verify {
Subroutine verify: not called CC=5 SCAR=34.0
Pod documented
825 0 my ($self) = @_;
826  
827 0 ▶ return $self->redirect_root("Already logged in...")
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("current_user") 0 0 0
828 if $self->param('current_user');
829  
830 0 my $user = $self->get_user;
831 0 my $code = $self->get_verification_code;
832  
833 0 ▶ return $self->redirect_root("no such user")
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and $user eq "" 0 0 0
Condition0%0/3
defined $user and $user eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and $user eq ""
defined $user $user eq ""
Truth table0%0/3
A and B
A= defined $userB= $user eq ""
ABresult
0X0
100
111
834 if defined $user and $user eq '';
835  
836 0 ▶ return $user->verify($code) ? $self->template('verified')
Branch: 1 branch
BranchTrueFalseTotal
$user->verify($code) ? : 0 0 0
837 : $self->redirect_root("BAD USER NO VALIDATION");
838 }
839  
840 #pod =head2 get_reset_code
841 #pod
842 #pod This gets the next part of the path and puts it in the C<reset_code>
843 #pod parameter.
844 #pod
845 #pod =cut
846  
847 0 ▶sub get_reset_code {
Subroutine get_reset_code: not called CC=1 SCAR=6.9
Pod documented
848 0 my ($self) = @_;
849  
850 0 $self->param(reset_code => $self->next_path_part);
851 }
852  
853 #pod =head2 get_verification_code
854 #pod
855 #pod This gets the next part of the path and puts it in the C<verification_code>
856 #pod parameter.
857 #pod
858 #pod =cut
859  
860 0 ▶sub get_verification_code {
Subroutine get_verification_code: not called CC=1 SCAR=6.9
Pod documented
861 0 my ($self) = @_;
862  
863 0 $self->param(verification_code => $self->next_path_part);
864 }
865  
866 #pod =head2 get_user
867 #pod
868 #pod This gets the next part of the path and puts it in the C<user> parameter.
869 #pod
870 #pod =cut
871  
872 0 ▶sub get_user {
Subroutine get_user: not called CC=2 SCAR=17.9
Pod documented
873 0 my ($self) = @_;
874  
875 0 ▶ $self->param(user => Rubric::User->retrieve($self->next_path_part) || '');
Condition0%0/2
'Rubric::User'->retrieve($self->next_path_part) || ''
l!l
00
MC/DC0%0/1
'Rubric::User'->retrieve($self->next_path_part) || ''
'Rubric::User'->retrieve($self->next_path_part)
Truth table0%0/2
A
A= 'Rubric::User'->retrieve($self->next_path_part)
Aresult
11
00
876 }
877  
878 #pod =head2 display_entries
879 #pod
880 #pod This method searches (with Rubric::Entry) for entries matching the requested
881 #pod user and tags. It pages the result (with C<page_entries>) and renders the
882 #pod resulting page with C<render_entries>.
883 #pod
884 #pod =cut
885  
886 0 ▶sub display_entries {
Subroutine display_entries: not called CC=3 SCAR=24.8
Pod documented
887 0 my ($self) = @_;
888  
889 0 ▶ return $self->redirect_root("no such user")
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->param("user") and $self->param("user") eq "" 0 0 0
Condition0%0/3
defined $self->param("user") and $self->param("user") eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->param("user") and $self->param("user") eq ""
defined $self->param("user") $self->param("user") eq ""
Truth table0%0/3
A and B
A= defined $self->param("user")B= $self->param("user") eq ""
ABresult
0X0
100
111
890 if defined $self->param('user') and $self->param('user') eq '';
891  
892 0 $self->param('has_body', scalar $self->query->param('has_body'));
893 0 $self->param('has_link', scalar $self->query->param('has_link'));
894  
895 0 my %search = (
896 user => $self->param('user'),
897 tags => $self->param('tags'),
898 link => $self->param('link'),
899 has_body => $self->param('has_body'),
900 has_link => $self->param('has_link'),
901 );
902  
903 0 my $entries = Rubric::Entry->by_tag(\%search);
904  
905 0 $self->page_entries($entries)->render_entries;
906 }
907  
908 #pod =head2 page_entries($iterator)
909 #pod
910 #pod Given a Class::DBI::Iterator, this method sets up parameters describing the
911 #pod current page. Most importantly, it retrieves an Iterator for the slice of
912 #pod entries representing the current page. The following parameters are set:
913 #pod
914 #pod entries - a Class::DBI::Iterator for the current page's entries
915 #pod count - the number of entries in the entire set
916 #pod pages - the number of pages the set spans
917 #pod
918 #pod =cut
919  
920 30 ▶sub page_entries {
Subroutine page_entries: called CC=2 SCAR=6.9
Pod documented
921 30 my ($self, $iterator) = @_;
922  
923 30 my $first = $self->param('per_page') * ($self->param('page') - 1);
924 30 my $last = ($self->param('per_page') * $self->param('page')) - 1;
925 30 my $slice = $iterator->slice($first, $last);
926 30 $self->param('entries', $slice);
927 30 $self->param('count', $iterator->count);
928  
929 30 my $pagecount = int($iterator->count / $self->param('per_page'));
930 30 ▶ $pagecount++ if $iterator->count % $self->param('per_page');
Branch: 1 branch
BranchTrueFalseTotal
if $iterator->count % $self->param("per_page") 29 1 30
931 30 $self->param('pages', $pagecount);
932  
933 30 return $self;
934 }
935  
936 #pod =head2 render_entries
937 #pod
938 #pod This method renders a template to display the set of entries set up by
939 #pod C<page_entries>.
940 #pod
941 #pod =cut
942  
943 30 ▶sub render_entries {
Subroutine render_entries: called CC=3 SCAR=11.0
Pod documented
944 30 my ($self, $options) = @_;
945 30 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
300
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
946  
947 $self->template('entries' => {
948 count => $self->param('count'),
949 entries => $self->param('entries'),
950 pages => $self->param('pages'),
951 %$options,
952 13 ▶ remove => sub { [ grep { $_ ne $_[0] } @{$_[1]} ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
953 self_url => $self->query->self_url(),
954 long_form => scalar $self->query->param('long_form'),
955 recent_tags => $self->param('recent_tags'),
956 related_tags => scalar (($options->{user} || 'Rubric::EntryTag')
957 30 ▶ ->related_tags_counted($options->{tags})),
Condition100%2/2
$options->{'user'} || 'Rubric::EntryTag'
l!l
822
MC/DC100%1/1
$options->{'user'} || 'Rubric::EntryTag'
$options->{'user'}
Truth table100%2/2
A
A= $options->{'user'}
Aresult
11
00
958 query_description => $self->param('query_description'),
959 });
960 }
961  
962 #pod =head2 edit
963 #pod
964 #pod If the user isn't logged in, it redirects to demand a login. If he is, it
965 #pod displays a post form, completed with the given entry's data.
966 #pod
967 #pod =cut
968  
969 1 ▶sub edit {
Subroutine edit: called CC=3 SCAR=11.2
Pod documented
970 1 my ($self) = @_;
971  
972 1 ▶ return $self->template('no_entry', { reason => 'missing' })
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_entry 0 1 1
973 unless $self->get_entry;
974  
975 1 ▶ return $self->template('no_entry', { reason => 'access' })
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param("entry")->user eq $self->param("current_user") 0 1 1
976 unless $self->param('entry')->user eq $self->param('current_user');
977  
978 1 $self->param('existing_entry', $self->param('entry'));
979 1 $self->param('existing_link', $self->param('entry')->link);
980 1 return $self->post_form();
981 }
982  
983 #pod =head2 post
984 #pod
985 #pod This method wants to be simplified.
986 #pod
987 #pod If the user isn't logged in, it redirects to demand a login. If he is, it
988 #pod checks whether it can create a new entry. If so, it tries to. If not, it
989 #pod displays a form for doing so. If the user already has an entry for the given
990 #pod URI, the existing entry is passed to the form renderer.
991 #pod
992 #pod If a new entry is created, the user is redirected to his entry listing.
993 #pod
994 #pod =cut
995  
996 2 ▶sub _post_form_contents {
Subroutine _post_form_contents: called CC=17 SCAR=34.8
997 2 my ($self) = @_;
998 2 my (%form, %error);
999  
1000 $form{$_} = $self->query->param($_)
1001 2 for qw(entryid uri title description tags body);
1002  
1003 2 for (qw(uri title description body tags)) {
1004 10 my $decoded;
1005 10 my $ok = eval {
1006 10 $decoded = decode_utf8($form{$_}, Encode::FB_CROAK | Encode::LEAVE_SRC);
1007 10 1;
1008 };
1009 10 ▶ $error{$_} = "Invalid characters in $_." unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 10 10
1010 10 ▶ $form{$_} = $decoded if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 10 0 10
1011 }
1012  
1013 2 eval { $form{uri} = URI->new($form{uri})->canonical; };
1014 2 ▶ $error{uri} = "Invalid URI" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1015  
1016 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($form{"uri"} and not $error{"uri"} and defined "Rubric::Config"->allowed_schemes and not grep({$_ eq $form{"uri"}->scheme;} @{"Rubric::Config"->allowed_schemes;})) 0 2 2
Condition55%5/9
$form{"uri"} and not $error{"uri"} and defined "Rubric::Config"->allowed_schemes and not grep({$_ eq $form{"uri"}->scheme;} @{"Rubric::Config"->allowed_schemes;})
!ll&&!rl&&r
200
$form{"uri"} and not $error{"uri"} and defined "Rubric::Config"->allowed_schemes
!ll&&!rl&&r
110
$form{"uri"} and not $error{"uri"}
!ll&&!rl&&r
101
MC/DC0%0/4
$form{"uri"} and not $error{"uri"} and defined "Rubric::Config"->allowed_schemes and not grep({$_ eq $form{"uri"}->scheme;} @{"Rubric::Config"->allowed_schemes;})
$form{"uri"} not $error{"uri"} defined "Rubric::Config"->allowed_schemes not grep({$_ eq $form{"uri"}->scheme;} @{"Rubric::Config"->allowed_schemes;})
Truth table40%2/5
A and not(B) and C and not(D)
A= $form{"uri"}B= not $error{"uri"}C= defined "Rubric::Config"->allowed_schemesD= not grep({$_ eq $form{"uri"}->scheme;} @{"Rubric::Config"->allowed_schemes;})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1017 $form{uri}
1018 and not $error{uri}
1019 and defined Rubric::Config->allowed_schemes
1020 0 and not grep { $_ eq $form{uri}->scheme } @{ Rubric::Config->allowed_schemes }
1021 ) {
1022 0 $error{uri} = "Invalid URI; valid schemes are: "
1023 0 . "@{ Rubric::Config->allowed_schemes }";
1024 }
1025  
1026 2 eval { Rubric::Entry->tags_from_string($form{tags}) };
1027 2 ▶ $error{tags} = "Tags may only contain letters, numbers, dot, colon, and asterisk." if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1028  
1029 $error{title} = "You must supply a title." if
1030 2 ▶ $self->query->param('submit') and not length $form{title};
Branch: 1 branch
BranchTrueFalseTotal
if $self->query->param("submit") and not length $form{"title"} 0 2 2
Condition33%1/3
$self->query->param("submit") and not length $form{"title"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->query->param("submit") and not length $form{"title"}
$self->query->param("submit") not length $form{"title"}
Truth table33%1/3
A and not(B)
A= $self->query->param("submit")B= not length $form{"title"}
ABresult
0X0
100
111
1031  
1032 2 ▶ if ($form{uri} and Rubric::Config->one_entry_per_link) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form{"uri"} and "Rubric::Config"->one_entry_per_link) 1 1 2
Condition66%2/3
$form{"uri"} and "Rubric::Config"->one_entry_per_link
!ll&&!rl&&r
101
MC/DC50%1/2
$form{"uri"} and "Rubric::Config"->one_entry_per_link
$form{"uri"} "Rubric::Config"->one_entry_per_link
Truth table66%2/3
A and B
A= $form{"uri"}B= "Rubric::Config"->one_entry_per_link
ABresult
0X0
100
111
1033 1 ▶ if (my ($link) = Rubric::Link->search({uri => $form{uri}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($link) = "Rubric::Link"->search({"uri", $form{"uri"}})) 0 1 1
1034 0 $self->param(existing_link => $link);
1035 0 ▶ if (my ($entry) = $self->param('current_user')->entries(link => $link)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($entry) = $self->param("current_user")->entries("link", $link)) 0 0 0
1036 0 $self->param(existing_entry => $entry);
1037 # why was this a desired error message?
1038 # $error{uri} = "This will replace your current entry for this URI."
1039 # if not $form{entryid};
1040 }
1041 }
1042 }
1043  
1044 2 return (\%form, \%error);
1045 }
1046  
1047 2 ▶sub post {
Subroutine post: called CC=9 SCAR=37.7
Pod documented
1048 2 my ($self) = @_;
1049  
1050 2 ▶ return $self->login unless my $user = $self->param('current_user');
Branch: 1 branch
BranchTrueFalseTotal
unless my $user = $self->param("current_user") 0 2 2
1051  
1052 2 my ($form, $error) = $self->_post_form_contents;
1053  
1054 2 ▶ return $self->post_form($form, $error)
Branch: 1 branch
BranchTrueFalseTotal
if not $self->query->param("submit") or %$error or not my $entry = $self->param("current_user")->quick_entry($form) 2 0 2
Condition33%2/6
not $self->query->param("submit") or %$error or not my $entry = $self->param("current_user")->quick_entry($form)
l!l&&r!l&&!r
200
not $self->query->param("submit") or %$error
l!l&&r!l&&!r
200
MC/DC0%0/3
not $self->query->param("submit") or %$error or not my $entry = $self->param("current_user")->quick_entry($form)
not $self->query->param("submit") %$error not my $entry = $self->param("current_user")->quick_entry($form)
Truth table25%1/4
not(A) or B or not(C)
A= not $self->query->param("submit")B= %$errorC= not my $entry = $self->param("current_user")->quick_entry($form)
ABCresult
1XX1
01X1
0011
0000
1055 if not $self->query->param('submit')
1056 or %$error
1057 or not my $entry = $self->param('current_user')->quick_entry($form);
1058  
1059 0 my $when_done = $self->query->param('when_done');
1060 0 my $goto;
1061  
1062 0 ▶ if ($when_done eq 'close') { return $self->template('close_window') }
Branch: 3 branches
BranchTrueFalseTotal
if ($when_done eq 'close') { } 0 0 0
elsif ($when_done eq 'entry') { } 0 0 0
elsif ($when_done eq 'go_back') { } 0 0 0
1063 0 elsif ($when_done eq 'entry') { $goto = Rubric::WebApp::URI->entry($entry) }
1064 0 elsif ($when_done eq 'go_back') { $goto = $form->{uri} }
1065 0 else { $goto = $self->query->param('then_goto') }
1066  
1067 0 ▶ $goto ||= Rubric::WebApp::URI->entries({user=> $self->param('current_user')});
Condition0%0/3
$goto ||= "Rubric::WebApp::URI"->entries({"user", $self->param("current_user")})
l!l&&r!l&&!r
000
MC/DC0%0/2
$goto ||= "Rubric::WebApp::URI"->entries({"user", $self->param("current_user")})
$goto "Rubric::WebApp::URI"->entries({"user", $self->param("current_user")})
Truth table0%0/3
A ||= B
A= $gotoB= "Rubric::WebApp::URI"->entries({"user", $self->param("current_user")})
ABresult
1X1
011
000
1068  
1069 0 $self->redirect( $goto, "Posted..." );
1070 }
1071  
1072 #pod =head2 post_form
1073 #pod
1074 #pod This method renders a form for the user to create a new entry.
1075 #pod
1076 #pod =cut
1077  
1078 3 ▶sub post_form {
Subroutine post_form: called CC=1 SCAR=0.0
Pod documented
1079 3 my ($self, $form, $error) = @_;
1080  
1081 3 $self->template( 'post' => {
1082 form => $form,
1083 error => $error,
1084 user => scalar $self->param('current_user'),
1085 existing_entry => scalar $self->param('existing_entry'),
1086 existing_link => scalar $self->param('existing_link'),
1087 then_goto => scalar $self->query->param('then_goto'),
1088 when_done => scalar $self->query->param('when_done'),
1089 });
1090 }
1091  
1092 #pod =head2 delete
1093 #pod
1094 #pod This method wants to be simplified. It's largely copied from C<post>.
1095 #pod
1096 #pod If the user isn't logged in, it redirects to demand a login. If he is, it
1097 #pod checks whether the user has an entry for the given URI. If so, it's deleted.
1098 #pod
1099 #pod Either way, the user is redirected to his entry listing.
1100 #pod
1101 #pod =cut
1102  
1103 1 ▶sub delete {
Subroutine delete: called CC=5 SCAR=17.5
Pod documented
1104 1 my ($self) = @_;
1105  
1106 1 ▶ return $self->login unless my $user = $self->param('current_user');
Branch: 1 branch
BranchTrueFalseTotal
unless my $user = $self->param("current_user") 0 1 1
1107  
1108 1 ▶ return $self->redirect_root("No such entry...")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_entry 0 1 1
1109 unless $self->get_entry;
1110  
1111 1 ▶ return $self->redirect_root("Not your entry...")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param("entry")->user eq $user 0 1 1
1112 unless $self->param('entry')->user eq $user;
1113  
1114 1 $self->param('entry')->delete;
1115  
1116 1 ▶ my $goto = $self->query->param('then_goto')
Condition33%1/3
$self->query->param('then_goto') || 'Rubric::WebApp::URI'->entries({'username', $user})
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->query->param('then_goto') || 'Rubric::WebApp::URI'->entries({'username', $user})
$self->query->param('then_goto') 'Rubric::WebApp::URI'->entries({'username', $user})
Truth table33%1/3
A || B
A= $self->query->param('then_goto')B= 'Rubric::WebApp::URI'->entries({'username', $user})
ABresult
1X1
011
000
1117 || Rubric::WebApp::URI->entries({ username => $user });
1118  
1119 1 return $self->redirect( $goto, "Deleted..." );
1120 }
1121  
1122 #pod =head2 doc
1123 #pod
1124 #pod This runmode returns a mostly-static document from the template path.
1125 #pod
1126 #pod =cut
1127  
1128 1 ▶sub doc {
Subroutine doc: called CC=2 SCAR=7.0
Pod documented
1129 1 my ($self) = @_;
1130  
1131 1 $self->get_doc;
1132 1 my $output = eval { $self->template("docs/" . $self->param('doc_page')); };
1133  
1134 # XXX: this should instead redirect to a 404-page
1135 1 ▶ return $output ? $output : $self->redirect_root("no such document");
Branch: 1 branch
BranchTrueFalseTotal
$output ? : 1 0 1
1136 }
1137  
1138 #pod =head2 get_doc
1139 #pod
1140 #pod This gets the next part of the path and puts it in the C<doc_page> parameter.
1141 #pod
1142 #pod =cut
1143  
1144 1 ▶sub get_doc {
Subroutine get_doc: called CC=2 SCAR=7.1
Pod documented
1145 1 my ($self) = @_;
1146  
1147 1 my $doc_page = $self->next_path_part;
1148 1 ▶ return $doc_page =~ /^[\pL\d_]+$/ ? $self->param(doc_page => $doc_page)
Branch: 1 branch
BranchTrueFalseTotal
$doc_page =~ /^[\pL\d_]+$/ ? : 1 0 1
1149 : ();
1150 }
1151  
1152 #pod =head2 style
1153 #pod
1154 #pod This runmode sends the named stylesheet from the CSS path.
1155 #pod
1156 #pod =cut
1157  
1158 1 ▶sub style {
Subroutine style: called CC=1 SCAR=0.0
Pod documented
1159 1 my ($self) = @_;
1160  
1161 1 my $sheet = $self->next_path_part;
1162  
1163 1 my $file = File::Spec->catfile('style', $sheet);
1164  
1165 1 $self->header_add(-type => 'text/css');
1166 1 my $tt = Template->new({
1167 INCLUDE_PATH => [
1168 Rubric::Config->template_path,
1169 File::Spec->catdir(File::ShareDir::dist_dir('Rubric'), 'templates'),
1170 ],
1171 });
1172  
1173 1 my $output;
1174 1 $tt->process($file, {}, \$output);
1175 1 return $output;
1176 }
1177  
1178 3 1;
1179  
1180 __END__
« Rubric/CLI/Command/user.pm ↑ Summary Rubric/DBI/Setup.pm »