blib/lib/Catalyst/Plugin/AutoSession.pm

filter: Statementstmt 42.8% 15 / 35 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 37.7% 20 / 53 CC 7 SCAR 34.3
CC
7
Cov
33%
prepare
56.0
__ANON__
2.0
__ANON__
2.0
CRAP 30.8
↑ Summary
1 package Catalyst::Plugin::AutoSession;
2  
3 1 our $VERSION = 0.03;
4 1 $VERSION = eval $VERSION;
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub prepare {
Subroutine prepare: not called CC=7 SCAR=40.3
Pod documented
13 0 my $class = shift;
14  
15 0 my $c = $class->NEXT::prepare(@_);
16  
17 0 my $config = $c->config->{AutoSession};
18 #
19 # Get all params that start with the prefix and put them into session variables
20 #
21 0 my $prefixScalar = $config->{prefix_scalar};
22 0 my $prefixList = $config->{prefix_list};
23  
24 PARAM:
25 0 foreach my $param (keys %{$c->request->params()}) {
26 0 ▶ if (my ($key) = $param =~ /^$prefixScalar(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = $param =~ /^$prefixScalar(.*)/) 0 0 0
27 # Check for exclusions
28 0 ▶ next PARAM if (first {$key eq $_} @{$config->{exclude}});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $key eq $_; } , @{$$config{"exclude"};}) 0 0 0
29  
30 0 $c->session->{$key} = $c->request->param($param);
31 }
32  
33 0 ▶ if (my ($key) = $param =~ /^$prefixList(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = $param =~ /^$prefixList(.*)/) 0 0 0
34 # Check for exclusions
35 0 ▶ next PARAM if (first {$key eq $_} @{$config->{exclude}});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $key eq $_; } , @{$$config{"exclude"};}) 0 0 0
36  
37 0 ▶ if ($c->request->param($param)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->request->param($param)) { } 0 0 0
38 0 $c->session->{$key} = [$c->request->param($param)];
39 }
40 else {
41 0 undef $c->session->{$key};
42 }
43 }
44 }
45 0 return $c;
46 }
47 1 1;
48  
49 =pod
50  
51 =head1 NAME
52  
53 Catalyst::Plugin::AutoSession - Generate session variables directly from
54 request parameters
55  
56 =head1 SYNOPSIS
57  
58 # To set session variables directly from request parameters
59  
60 use Catalyst qw(AutoSession Session);
61  
62 # Configure the prefix and exclusions
63  
64 Admin->config(
65 AutoSession => {
66 prefix => 'sess_',
67 exclude => [qw(logged_in_user logged_in_username)],
68 },
69 );
70  
71 # Now any request parameter of the form 'sess_xxxx' will automatically
72 # create a session variable 'xxxx' e.g.
73 #
74 # http://mydomain.com/myapp/?sess_myname=icydee
75 #
76 # will create a session variable 'myname' equal to 'icydee'
77  
78  
79 =head1 DESCRIPTION
80  
81 It is frequently useful to have persistant forms, for example a search input
82 field which retains the value of the last search.
83  
84 To do this it is common to process the request parameters and save the entered
85 value into a session variable. When you re-display the form you set the value
86 from the session variable.
87  
88 This module automates this process by automatically setting session variables
89 from request parameters that start with a specific prefix.
90  
91 By default, all C<$c-E<gt>request-E<gt>parameters> that start with the prefix
92 C<sess_> are converted into session variables. The session variables are given
93 the same name as the C<$c-E<gt>request-E<gt>parameters> but with the prefix
94 removed.
95  
96 There may be a security issue in allowing some session variables to be set from
97 a C<$c-E<gt>request-E<gt>parameter>. As an example consider a session variable
98 that holds the logged in status of the user, 'loggedInUserId'. If this could be
99 set from the URL then it would bypass the authentication. Any such session
100 variables can be explicitely excluded in the C<exclude> configuration.
101  
102  
103 =head1 EXTENDED METHODS
104  
105 =head2 prepare
106  
107 Will automatically set session variables based on
108 C<$c-E<gt>request-E<gt>parameters> that start with a specified prefix.
109 C<prepare> is called automatically by the Catalyst Engine; the end user will
110 not have to call it directly. (In fact, it should never be called directly by
111 the end user.)
112  
113 =head1 CONFIGURATION
114  
115 The default prefix is C<sess_> but this can be changed in the configuration.
116  
117 By default, all C<$c-E<gt>request-E<gt>parameters> that start with this prefix
118 are used to create session variables. Exclude any that you do not want to
119 process by specifying an array of names in the configuration.
120  
121 __PACKAGE__->config(
122 AutoSession => {
123 prefix => 'sess_',
124 exclude => [qw(logged_in_user logged_in_username)],
125 },
126 );
127  
128 In a template
129  
130 <input name="sess_search" value="[% c.session.search %]">
131  
132 Each time a value is input into this search form it will be remembered in
133 a session variable and used to re-populate the form when it is displayed again.
134  
135 =head1 COPYRIGHT & LICENSE
136  
137 Copyright (c) 2005 the aforementioned authors. All rights
138 reserved. This program is free software; you can redistribute
139 it and/or modify it under the same terms as Perl itself.
140  
141 =cut
↑ Summary