DBIC.pm

filter: Statementstmt 36.6% 26 / 71 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 40.0% 8 / 20 Podpod 100% 10 / 10 totaltotal 29.9% 44 / 147 CC 22 SCAR 30.7
CC
22
Cov
23%
session_store_delegate_key_to_accessor
56.0
setup_finished
42.0
delete_session_data
30.0
CRAP 21.5
↑ Summary DBIC/Delegate.pm »
1 package Catalyst::Plugin::Session::Store::DBIC;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw/Catalyst::Plugin::Session::Store::Delegate/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Catalyst::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Catalyst::Plugin::Session::Store::DBIC::Delegate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.15'; # VERSION
13  
14 =head1 NAME
15  
16 Catalyst::Plugin::Session::Store::DBIC - Store your sessions via DBIx::Class
17  
18 =head1 SYNOPSIS
19  
20 # Create a table in your database for sessions
21 CREATE TABLE sessions (
22 id CHAR(72) PRIMARY KEY,
23 session_data TEXT,
24 expires INTEGER
25 );
26  
27 # Create the corresponding table class
28 package MyApp::Schema::Session;
29  
30 use base qw/DBIx::Class/;
31  
32 __PACKAGE__->load_components(qw/Core/);
33 __PACKAGE__->table('sessions');
34 __PACKAGE__->add_columns(qw/id session_data expires/);
35 __PACKAGE__->set_primary_key('id');
36  
37 1;
38  
39 # In your application
40 use Catalyst qw/Session Session::Store::DBIC Session::State::Cookie/;
41  
42 __PACKAGE__->config(
43 # ... other items ...
44 'Plugin::Session' => {
45 dbic_class => 'DBIC::Session', # Assuming MyApp::Model::DBIC
46 expires => 3600,
47 },
48 );
49  
50 # Later, in a controller action
51 $c->session->{foo} = 'bar';
52  
53 =head1 DESCRIPTION
54  
55 This L<Catalyst::Plugin::Session> storage module saves session data in
56 your database via L<DBIx::Class>. It's actually just a wrapper around
57 L<Catalyst::Plugin::Session::Store::Delegate>; if you need complete
58 control over how your sessions are stored, you probably want to use
59 that instead.
60  
61 =head1 METHODS
62  
63 =head2 setup_finished
64  
65 Hook into the configured session class.
66  
67 =cut
68  
69 0 ▶sub setup_finished {
Subroutine setup_finished: not called CC=6 SCAR=37.4
Pod documented
70 0 my $c = shift;
71  
72 0 ▶ return $c->next::method unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
73  
74 # Try to determine id_field if it isn't set
75 0 ▶ unless ($c->_session_plugin_config->{id_field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->_session_plugin_config->{"id_field"}) 0 0 0
76 0 my $model = $c->session_store_model;
77 0 ▶ my $rs = ref $model ? $model
Branch: 2 branches
BranchTrueFalseTotal
ref $model ? : 0 0 0
$model->can('resultset_instance') ? : 0 0 0
78 : $model->can('resultset_instance') ? $model->resultset_instance
79 : $model;
80 0 my @primary_columns = $rs->result_source->primary_columns;
81  
82 0 ▶ Catalyst::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @primary_columns > 1 0 0 0
83 message => __PACKAGE__ . qq/: Primary key consists of more than one column; please set id_field manually/
84 ) if @primary_columns > 1;
85  
86 0 $c->_session_plugin_config->{id_field} = $primary_columns[0];
87 }
88  
89 0 $c->next::method(@_);
90 }
91  
92 =head2 session_store_dbic_class
93  
94 Return the L<DBIx::Class> class name to be passed to C<< $c->model >>.
95 Defaults to C<DBIC::Session>.
96  
97 =cut
98  
99 0 ▶sub session_store_dbic_class {
Subroutine session_store_dbic_class: not called CC=2 SCAR=17.9
Pod documented
100 0 ▶ shift->_session_plugin_config->{dbic_class} || 'DBIC::Session';
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_session_plugin_config->{"dbic_class"} or "DBIC::Session" 0 0 0
101 }
102  
103 =head2 session_store_dbic_id_field
104  
105 Return the configured ID field name. Defaults to C<id>.
106  
107 =cut
108  
109 0 ▶sub session_store_dbic_id_field {
Subroutine session_store_dbic_id_field: not called CC=2 SCAR=17.9
Pod documented
110 0 ▶ shift->_session_plugin_config->{id_field} || 'id';
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_session_plugin_config->{"id_field"} or "id" 0 0 0
111 }
112  
113 =head2 session_store_dbic_data_field
114  
115 Return the configured data field name. Defaults to C<session_data>.
116  
117 =cut
118  
119 0 ▶sub session_store_dbic_data_field {
Subroutine session_store_dbic_data_field: not called CC=2 SCAR=17.9
Pod documented
120 0 ▶ shift->_session_plugin_config->{data_field} || 'session_data';
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_session_plugin_config->{"data_field"} or "session_data" 0 0 0
121 }
122  
123 =head2 session_store_dbic_expires_field
124  
125 Return the configured expires field name. Defaults to C<expires>.
126  
127 =cut
128  
129 0 ▶sub session_store_dbic_expires_field {
Subroutine session_store_dbic_expires_field: not called CC=2 SCAR=17.9
Pod documented
130 0 ▶ shift->_session_plugin_config->{expires_field} || 'expires';
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_session_plugin_config->{"expires_field"} or "expires" 0 0 0
131 }
132  
133 =head2 session_store_model
134  
135 Return the model used to find a session.
136  
137 =cut
138  
139 0 ▶sub session_store_model {
Subroutine session_store_model: not called CC=2 SCAR=17.9
Pod documented
140 0 my ($c, $id) = @_;
141  
142 0 my $dbic_class = $c->session_store_dbic_class;
143 0 ▶ $c->model($dbic_class, $id) or die "Couldn't find a model named $dbic_class";
Branch: 1 branch
BranchTrueFalseTotal
$c->model($dbic_class, $id) or die "Couldn't find a model named $dbic_class" 0 0 0
144 }
145  
146 =head2 get_session_store_delegate
147  
148 Load the row corresponding to the specified session ID. If none is
149 found, one is automatically created.
150  
151 =cut
152  
153 0 ▶sub get_session_store_delegate {
Subroutine get_session_store_delegate: not called CC=1 SCAR=6.9
Pod documented
154 0 my ($c, $id) = @_;
155  
156 0 Catalyst::Plugin::Session::Store::DBIC::Delegate->new({
157 model => $c->session_store_model($id),
158 id_field => $c->session_store_dbic_id_field,
159 data_field => $c->session_store_dbic_data_field,
160 });
161 }
162  
163 =head2 session_store_delegate_key_to_accessor
164  
165 Match the specified key and operation to the session ID and field
166 name.
167  
168 =cut
169  
170 0 ▶sub session_store_delegate_key_to_accessor {
Subroutine session_store_delegate_key_to_accessor: not called CC=7 SCAR=40.3
Pod documented
171 0 my $c = shift;
172 0 my $key = $_[0];
173 0 my ($field, @args) = $c->next::method(@_);
174  
175 0 my ($type) = ($key =~ /^(\w+):/);
176  
177 0 ▶ $field = $c->session_store_dbic_id_field if $field eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "id" 0 0 0
178 0 ▶ $field = $c->session_store_dbic_expires_field if $field eq 'expires';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "expires" 0 0 0
179 0 ▶ $field = $c->session_store_dbic_data_field if $field eq 'session' or $field eq 'flash';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "session" or $field eq "flash" 0 0 0
Condition0%0/3
$field eq "session" or $field eq "flash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$field eq "session" or $field eq "flash"
$field eq "session" $field eq "flash"
Truth table0%0/3
A or B
A= $field eq "session"B= $field eq "flash"
ABresult
1X1
011
000
180  
181 0 ▶ my $accessor = sub { shift->$type($key)->$field(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
182  
183 0 ▶ if ($field eq $c->session_store_dbic_data_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq $c->session_store_dbic_data_field) 0 0 0
184 0 ▶ @args = map { MIME::Base64::encode(Storable::nfreeze($_ || '')) } @args;
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
185 0 ▶ $accessor = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
186 0 my $value = shift->$type($key)->$field(@_);
187 0 ▶ return unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
188 0 return Storable::thaw(MIME::Base64::decode($value));
189 0 };
190 }
191  
192 0 return ($accessor, @args);
193 }
194  
195 =head2 delete_session_data
196  
197 Delete the specified session from the backend store.
198  
199 =cut
200  
201 0 ▶sub delete_session_data {
Subroutine delete_session_data: not called CC=5 SCAR=34.0
Pod documented
202 0 my ($c, $key) = @_;
203  
204 # expires is stored on the session row for compatibility with Store::DBI
205 0 ▶ return if $key =~ /^expires/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^expires/ 0 0 0
206  
207 0 $c->session_store_model->search({
208 $c->session_store_dbic_id_field => $key,
209 })->delete;
210  
211 0 ▶ return if !$c->_session_store_delegate;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->_session_store_delegate 0 0 0
212  
213 0 my ($field) = split /:/, $key;
214 0 ▶ if ($field eq 'session') {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq 'session') { } 0 0 0
elsif ($field eq 'flash') { } 0 0 0
215 0 $c->_session_store_delegate->clear_session;
216 } elsif ($field eq 'flash') {
217 0 $c->_session_store_delegate->clear_flash;
218 }
219 }
220  
221 =head2 delete_expired_sessions
222  
223 Delete all expired sessions.
224  
225 =cut
226  
227 0 ▶sub delete_expired_sessions {
Subroutine delete_expired_sessions: not called CC=1 SCAR=6.9
Pod documented
228 0 my $c = shift;
229  
230 0 $c->session_store_model->search({
231 $c->session_store_dbic_expires_field => { '<', time() },
232 })->delete;
233 }
234  
235 =head1 CONFIGURATION
236  
237 The following parameters should be placed in your application
238 configuration under the C<Plugin::Session> key.
239  
240 =head2 dbic_class
241  
242 (Required) The name of the L<DBIx::Class> that represents a session in
243 the database. It is recommended that you provide only the part after
244 C<MyApp::Model>, e.g. C<DBIC::Session>.
245  
246 If you are using L<Catalyst::Model::DBIC::Schema>, the following
247 layout is recommended:
248  
249 =over 4
250  
251 =item * C<MyApp::Schema> - your L<DBIx::Class::Schema> class
252  
253 =item * C<MyApp::Schema::Session> - your session table class
254  
255 =item * C<MyApp::Model::DBIC> - your L<Catalyst::Model::DBIC::Schema> class
256  
257 =back
258  
259 This module will then use C<< $c->model >> to access the appropriate
260 result source from the composed schema matching the C<dbic_class>
261 name.
262  
263 For more information, please see L<Catalyst::Model::DBIC::Schema>.
264  
265 =head2 expires
266  
267 Number of seconds for which sessions are active.
268  
269 Note that no automatic cleanup is done on your session data. To
270 delete expired sessions, you can use the L</delete_expired_sessions>
271 method with L<Catalyst::Plugin::Scheduler>.
272  
273 =head2 id_field
274  
275 The name of the field on your sessions table which stores the session
276 ID. Defaults to C<id>.
277  
278 =head2 data_field
279  
280 The name of the field on your sessions table which stores session
281 data. Defaults to C<session_data> for compatibility with
282 L<Catalyst::Plugin::Session::Store::DBI>.
283  
284 =head2 expires_field
285  
286 The name of the field on your sessions table which stores the
287 expiration time of the session. Defaults to C<expires>.
288  
289 =head1 SCHEMA
290  
291 Your sessions table should contain the following columns:
292  
293 id CHAR(72) PRIMARY KEY
294 session_data TEXT
295 expires INTEGER
296  
297 The C<id> column should probably be 72 characters. It needs to handle
298 the longest string that can be returned by
299 L<Catalyst::Plugin::Session/generate_session_id>, plus another eight
300 characters for internal use. This is less than 72 characters when
301 SHA-1 or MD5 is used, but SHA-256 will need all 72 characters.
302  
303 The C<session_data> column should be a long text field. Session data
304 is encoded using L<MIME::Base64> before being stored in the database.
305  
306 Note that MySQL C<TEXT> fields only store 64 kB, so if your session
307 data will exceed that size you'll want to use C<MEDIUMTEXT>,
308 C<MEDIUMBLOB>, or larger. If you configure your
309 L<DBIx::Class::ResultSource> to include the size of the column, you
310 will receive warnings for this problem:
311  
312 This session requires 1180 bytes of storage, but your database
313 column 'session_data' can only store 200 bytes. Storing this
314 session may not be reliable; increase the size of your data field
315  
316 See L<DBIx::Class::ResultSource/add_columns> for more information.
317  
318 The C<expires> column stores the future expiration time of the
319 session. This may be null for per-user and flash sessions.
320  
321 Note that you can change the column names using the L</id_field>,
322 L</data_field>, and L</expires_field> configuration parameters.
323 However, the column types must match the above.
324  
325 =head1 AUTHORS
326  
327 Daniel Westermann-Clark E<lt>danieltwc@cpan.orgE<gt>
328  
329 Andrew Rodland E<lt>andrew@cleverdomain.orgE<gt>
330  
331 =head1 ACKNOWLEDGMENTS
332  
333 =over 4
334  
335 =item * Andy Grundman, for L<Catalyst::Plugin::Session::Store::DBI>
336  
337 =item * David Kamholz, for most of the testing code (from
338 L<Catalyst::Plugin::Authentication::Store::DBIC>)
339  
340 =item * Yuval Kogman, for assistance in converting to
341 L<Catalyst::Plugin::Session::Store::Delegate>
342  
343 =item * Jay Hannah, for tests and warning when session size
344 exceeds DBIx::Class storage size.
345  
346 =back
347  
348 =head1 COPYRIGHT
349  
350 Copyright (c) 2006 - 2026
351 the Catalyst::Plugin::Session::Store::DBIC L</AUTHORS>
352 as listed above.
353  
354 This program is free software; you can redistribute it and/or modify it
355 under the same terms as Perl itself.
356  
357 =cut
358  
359 1 1;
↑ Summary DBIC/Delegate.pm »