| 1 | package Dancer::Session::DBIC; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | Dancer::Session::DBIC - DBIx::Class session engine for Dancer | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 VERSION | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 0.005 | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||
| 12 | |||||||||||||||
| 13 | This module implements a session engine for Dancer by serializing the session, | ||||||||||||||
| 14 | and storing it in a database via L<DBIx::Class>. The default serialization method is L<JSON>, | ||||||||||||||
| 15 | though one can specify any serialization format you want. L<YAML> and L<Storable> are | ||||||||||||||
| 16 | viable alternatives. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | JSON was chosen as the default serialization format, as it is fast, terse, and portable. | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||
| 21 | |||||||||||||||
| 22 | Example configuration: | ||||||||||||||
| 23 | |||||||||||||||
| 24 | session: "DBIC" | ||||||||||||||
| 25 | session_options: | ||||||||||||||
| 26 | dsn: "DBI:mysql:database=testing;host=127.0.0.1;port=3306" # DBI Data Source Name | ||||||||||||||
| 27 | schema_class: "Interchange6::Schema" # DBIx::Class schema | ||||||||||||||
| 28 | user: "user" # Username used to connect to the database | ||||||||||||||
| 29 | pass: "password" # Password to connect to the database | ||||||||||||||
| 30 | resultset: "MySession" # DBIx::Class resultset, defaults to Session | ||||||||||||||
| 31 | id_column: "my_session_id" # defaults to sessions_id | ||||||||||||||
| 32 | data_column: "my_session_data" # defaults to session_data | ||||||||||||||
| 33 | |||||||||||||||
| 34 | In conjunction with L<Dancer::Plugin::DBIC>, you can simply use the schema | ||||||||||||||
| 35 | object provided by this plugin in your application, either by | ||||||||||||||
| 36 | providing the name of the schema used by the plugin in the config: | ||||||||||||||
| 37 | |||||||||||||||
| 38 | session_options: | ||||||||||||||
| 39 | schema: default | ||||||||||||||
| 40 | |||||||||||||||
| 41 | Or by passing the schema object directly in the code: | ||||||||||||||
| 42 | |||||||||||||||
| 43 | set session_options => {schema => schema}; | ||||||||||||||
| 44 | |||||||||||||||
| 45 | Custom serializer / deserializer can be specified as follows: | ||||||||||||||
| 46 | |||||||||||||||
| 47 | set 'session_options' => { | ||||||||||||||
| 48 | schema => schema, | ||||||||||||||
| 49 | serializer => sub { YAML::Dump(@_); }, | ||||||||||||||
| 50 | deserializer => sub { YAML::Load(@_); }, | ||||||||||||||
| 51 | }; | ||||||||||||||
| 52 | |||||||||||||||
| 53 | =head1 SESSION EXPIRATION | ||||||||||||||
| 54 | |||||||||||||||
| 55 | A timestamp field that updates when a session is updated is recommended, so you can expire sessions server-side as well as client-side. | ||||||||||||||
| 56 | |||||||||||||||
| 57 | This session engine will not automagically remove expired sessions on the server, but with a timestamp field as above, you should be able to to do this manually. | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =head1 RESULT CLASS EXAMPLE | ||||||||||||||
| 60 | |||||||||||||||
| 61 | This result class would work as-is with the default values of C<session_options>. | ||||||||||||||
| 62 | It uses L<DBIx::Class::TimeStamp> to auto-set the C<created> | ||||||||||||||
| 63 | and C<last_modified> timestamps. | ||||||||||||||
| 64 | |||||||||||||||
| 65 | package MySchema::Result::Session; | ||||||||||||||
| 66 | |||||||||||||||
| 67 | use strict; | ||||||||||||||
| 68 | use warnings; | ||||||||||||||
| 69 | |||||||||||||||
| 70 | use base 'DBIx::Class::Core'; | ||||||||||||||
| 71 | |||||||||||||||
| 72 | __PACKAGE__->load_components(qw(TimeStamp)); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | __PACKAGE__->table('sessions'); | ||||||||||||||
| 75 | |||||||||||||||
| 76 | __PACKAGE__->add_columns( | ||||||||||||||
| 77 | sessions_id => { | ||||||||||||||
| 78 | data_type => 'varchar', size => 255 | ||||||||||||||
| 79 | }, | ||||||||||||||
| 80 | session_data => { | ||||||||||||||
| 81 | data_type => 'text' | ||||||||||||||
| 82 | }, | ||||||||||||||
| 83 | created => { | ||||||||||||||
| 84 | data_type => 'datetime', set_on_create => 1 | ||||||||||||||
| 85 | }, | ||||||||||||||
| 86 | last_modified => { | ||||||||||||||
| 87 | data_type => 'datetime', set_on_create => 1, set_on_update => 1 | ||||||||||||||
| 88 | }, | ||||||||||||||
| 89 | ); | ||||||||||||||
| 90 | |||||||||||||||
| 91 | __PACKAGE__->set_primary_key('sessions_id'); | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 1; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =cut | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 98 | 2 | ▶ | use parent 'Dancer::Session::Abstract'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | 2 | ▶ | use Dancer qw(:syntax !load); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | 2 | ▶ | use DBIx::Class; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | 2 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 103 | 2 | ▶ | use Module::Load; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 105 | |||||||||||||||
| 106 | 2 | our $VERSION = '0.005'; | |||||||||||||
| 107 | |||||||||||||||
| 108 | 2 | my %dbic_handles; | |||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 METHODS | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =head2 create() | ||||||||||||||
| 113 | |||||||||||||||
| 114 | Creates a new session. Returns the session object. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =cut | ||||||||||||||
| 117 | |||||||||||||||
| 118 | sub create { | ||||||||||||||
| 119 | 3 | ▶ | return Dancer::Session::DBIC->new->flush; | ||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | |||||||||||||||
| 123 | =head2 flush() | ||||||||||||||
| 124 | |||||||||||||||
| 125 | Write the session to the database. Returns the session object. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =cut | ||||||||||||||
| 128 | |||||||||||||||
| 129 | sub flush { | ||||||||||||||
| 130 | 9 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
flush: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 131 | 9 | my $handle = $self->_dbic; | |||||||||||||
| 132 | |||||||||||||||
| 133 | my %session_data = ($handle->{id_column} => $self->id, | ||||||||||||||
| 134 | 9 | $handle->{data_column} => $self->_serialize, | |||||||||||||
| 135 | ); | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 9 | my $session = $self->_rset->update_or_create(\%session_data); | |||||||||||||
| 138 | |||||||||||||||
| 139 | 9 | return $self; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head2 retrieve($id) | ||||||||||||||
| 143 | |||||||||||||||
| 144 | Look for a session with the given id. | ||||||||||||||
| 145 | |||||||||||||||
| 146 | Returns the session object if found, C<undef> if not. Logs a debug-level warning | ||||||||||||||
| 147 | if the session was found, but could not be deserialized. | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =cut | ||||||||||||||
| 150 | |||||||||||||||
| 151 | sub retrieve { | ||||||||||||||
| 152 | 0 | ▶ | my ($self, $session_id) = @_; | ||||||||||||
|
Subroutine
retrieve: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 153 | 0 | my $session_object; | |||||||||||||
| 154 | |||||||||||||||
| 155 | 0 | $session_object = $self->_rset->find($session_id); | |||||||||||||
| 156 | |||||||||||||||
| 157 | # Bail early if we know we have no session data at all | ||||||||||||||
| 158 | 0 | ▶ | if (!defined $session_object) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | debug "Could not retrieve session ID: $session_id"; | |||||||||||||
| 160 | 0 | return; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | my $session_data = $session_object->session_data; | |||||||||||||
| 164 | |||||||||||||||
| 165 | # No way to check that it's valid JSON other than trying to deserialize it | ||||||||||||||
| 166 | my $session = try { | ||||||||||||||
| 167 | 0 | ▶ | $self->_deserialize($session_data); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 168 | } catch { | ||||||||||||||
| 169 | 0 | ▶ | debug "Could not deserialize session ID: $session_id - $_"; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 170 | 0 | return; | |||||||||||||
| 171 | 0 | }; | |||||||||||||
| 172 | |||||||||||||||
| 173 | 0 | ▶ | bless $session, __PACKAGE__ if $session; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | |||||||||||||||
| 177 | =head2 destroy() | ||||||||||||||
| 178 | |||||||||||||||
| 179 | Remove the current session object from the database. | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =cut | ||||||||||||||
| 182 | |||||||||||||||
| 183 | sub destroy { | ||||||||||||||
| 184 | 2 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
destroy: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||
| 185 | |||||||||||||||
| 186 | 2 | ▶ | if (!defined $self->id) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 187 | 0 | debug "No session ID passed to destroy method"; | |||||||||||||
| 188 | 0 | return; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 2 | $self->_rset->find($self->id)->delete; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | # Creates and connects schema | ||||||||||||||
| 195 | |||||||||||||||
| 196 | sub _dbic { | ||||||||||||||
| 197 | 20 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_dbic: called CC=11 SCAR=29.8
| |||||||||||||||
| 198 | |||||||||||||||
| 199 | # To be fork safe and thread safe, use a combination of the PID and TID (if | ||||||||||||||
| 200 | # running with use threads) to make sure no two processes/threads share | ||||||||||||||
| 201 | # handles. Implementation based on DBIx::Connector by David E. Wheeler. | ||||||||||||||
| 202 | 20 | my $pid_tid = $$; | |||||||||||||
| 203 | 20 | ▶ | $pid_tid .= '_' . threads->tid if $INC{'threads.pm'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 204 | |||||||||||||||
| 205 | # OK, see if we have a matching handle | ||||||||||||||
| 206 | 20 | my $handle = $dbic_handles{$pid_tid}; | |||||||||||||
| 207 | |||||||||||||||
| 208 | 20 | ▶ | if ($handle->{schema}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 209 | 19 | return $handle; | |||||||||||||
| 210 | } | ||||||||||||||
| 211 | |||||||||||||||
| 212 | 1 | my $settings = setting('session_options'); | |||||||||||||
| 213 | |||||||||||||||
| 214 | # Prefer an active schema over a schema class. | ||||||||||||||
| 215 | 1 | ▶ | if ( my $schema = $settings->{schema}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 216 | 1 | ▶ | if (blessed $schema) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 217 | 0 | $handle->{schema} = $schema; | |||||||||||||
| 218 | } | ||||||||||||||
| 219 | elsif( ref $schema ) { | ||||||||||||||
| 220 | 1 | $handle->{schema} = $schema->(); | |||||||||||||
| 221 | } | ||||||||||||||
| 222 | else { | ||||||||||||||
| 223 | 0 | ▶ | die "can't use named schema: Dancer::Plugin::DBIC not loaded\n" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | unless $Dancer::Plugin::DBIC::VERSION; | ||||||||||||||
| 225 | 0 | $handle->{schema} = Dancer::Plugin::DBIC::schema($schema); | |||||||||||||
| 226 | } | ||||||||||||||
| 227 | } | ||||||||||||||
| 228 | elsif (! defined $settings->{schema_class}) { | ||||||||||||||
| 229 | 0 | die "No schema class defined."; | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | else { | ||||||||||||||
| 232 | 0 | my $schema_class = $settings->{schema_class}; | |||||||||||||
| 233 | |||||||||||||||
| 234 | $handle->{schema} = $self->_load_schema_class($schema_class, | ||||||||||||||
| 235 | $settings->{dsn}, | ||||||||||||||
| 236 | $settings->{user}, | ||||||||||||||
| 237 | 0 | $settings->{pass}); | |||||||||||||
| 238 | } | ||||||||||||||
| 239 | |||||||||||||||
| 240 | 1 | ▶ | $handle->{resultset} = $settings->{resultset} || 'Session'; | ||||||||||||
|
Condition50%1/2
$settings->{'resultset'} || 'Session'
MC/DC0%0/1
$settings->{'resultset'} || 'Session'
$settings->{'resultset'}
Truth table50%1/2
A
A= $settings->{'resultset'}
| |||||||||||||||
| 241 | 1 | ▶ | $handle->{id_column} = $settings->{id_column} || 'sessions_id'; | ||||||||||||
|
Condition50%1/2
$settings->{'id_column'} || 'sessions_id'
MC/DC0%0/1
$settings->{'id_column'} || 'sessions_id'
$settings->{'id_column'}
Truth table50%1/2
A
A= $settings->{'id_column'}
| |||||||||||||||
| 242 | 1 | ▶ | $handle->{data_column} = $settings->{data_column} || 'session_data'; | ||||||||||||
|
Condition50%1/2
$settings->{'data_column'} || 'session_data'
MC/DC0%0/1
$settings->{'data_column'} || 'session_data'
$settings->{'data_column'}
Truth table50%1/2
A
A= $settings->{'data_column'}
| |||||||||||||||
| 243 | |||||||||||||||
| 244 | 1 | $dbic_handles{$pid_tid} = $handle; | |||||||||||||
| 245 | |||||||||||||||
| 246 | 1 | return $handle; | |||||||||||||
| 247 | } | ||||||||||||||
| 248 | |||||||||||||||
| 249 | # Returns specific resultset | ||||||||||||||
| 250 | sub _rset { | ||||||||||||||
| 251 | 11 | ▶ | my ($self, $name) = @_; | ||||||||||||
|
Subroutine
_rset: called CC=1 SCAR=0.0
| |||||||||||||||
| 252 | |||||||||||||||
| 253 | 11 | my $handle = $self->_dbic; | |||||||||||||
| 254 | |||||||||||||||
| 255 | 11 | return $handle->{schema}->resultset($handle->{resultset}); | |||||||||||||
| 256 | } | ||||||||||||||
| 257 | |||||||||||||||
| 258 | # Loads schema class | ||||||||||||||
| 259 | sub _load_schema_class { | ||||||||||||||
| 260 | 0 | ▶ | my ($self, $schema_class, @conn_info) = @_; | ||||||||||||
|
Subroutine
_load_schema_class: not called CC=4 SCAR=30.0
| |||||||||||||||
| 261 | 0 | my ($schema_object); | |||||||||||||
| 262 | |||||||||||||||
| 263 | 0 | ▶ | if ($schema_class) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 264 | 0 | $schema_class =~ s/-/::/g; | |||||||||||||
| 265 | 0 | eval { load $schema_class }; | |||||||||||||
| 266 | 0 | ▶ | die "Could not load schema_class $schema_class: $@" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 267 | 0 | $schema_object = $schema_class->connect(@conn_info); | |||||||||||||
| 268 | } else { | ||||||||||||||
| 269 | 0 | my $dbic_loader = 'DBIx::Class::Schema::Loader'; | |||||||||||||
| 270 | 0 | eval { load $dbic_loader }; | |||||||||||||
| 271 | 0 | ▶ | die "You must provide a schema_class option or install $dbic_loader." | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 272 | if $@; | ||||||||||||||
| 273 | 0 | $dbic_loader->naming('v7'); | |||||||||||||
| 274 | 0 | $schema_object = DBIx::Class::Schema::Loader->connect(@conn_info); | |||||||||||||
| 275 | } | ||||||||||||||
| 276 | |||||||||||||||
| 277 | 0 | return $schema_object; | |||||||||||||
| 278 | } | ||||||||||||||
| 279 | |||||||||||||||
| 280 | # Default Serialize method | ||||||||||||||
| 281 | sub _serialize { | ||||||||||||||
| 282 | 9 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_serialize: called CC=2 SCAR=7.4
| |||||||||||||||
| 283 | 9 | my $settings = setting('session_options'); | |||||||||||||
| 284 | |||||||||||||||
| 285 | 9 | ▶ | if (defined $settings->{serializer}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 286 | 0 | return $settings->{serializer}->({%$self}); | |||||||||||||
| 287 | } | ||||||||||||||
| 288 | |||||||||||||||
| 289 | # A session is by definition ephemeral - Store it compactly | ||||||||||||||
| 290 | # This is the Dancer function, not from JSON.pm | ||||||||||||||
| 291 | 9 | return to_json({%$self}, { pretty => 0, convert_blessed => 1 }); | |||||||||||||
| 292 | } | ||||||||||||||
| 293 | |||||||||||||||
| 294 | |||||||||||||||
| 295 | # Default Deserialize method | ||||||||||||||
| 296 | sub _deserialize { | ||||||||||||||
| 297 | 0 | ▶ | my ($self, $json) = @_; | ||||||||||||
|
Subroutine
_deserialize: not called CC=2 SCAR=17.9
| |||||||||||||||
| 298 | 0 | my $settings = setting('session_options'); | |||||||||||||
| 299 | |||||||||||||||
| 300 | 0 | ▶ | if (defined $settings->{deserializer}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 301 | 0 | return $settings->{deserializer}->($json); | |||||||||||||
| 302 | } | ||||||||||||||
| 303 | |||||||||||||||
| 304 | # This is the Dancer function, not from JSON.pm | ||||||||||||||
| 305 | 0 | return from_json($json, { utf8 => 0}); | |||||||||||||
| 306 | } | ||||||||||||||
| 307 | |||||||||||||||
| 308 | =head1 SEE ALSO | ||||||||||||||
| 309 | |||||||||||||||
| 310 | L<Dancer>, L<Dancer::Session> | ||||||||||||||
| 311 | |||||||||||||||
| 312 | =head1 AUTHOR | ||||||||||||||
| 313 | |||||||||||||||
| 314 | Stefan Hornburg (Racke) <racke@linuxia.de> | ||||||||||||||
| 315 | |||||||||||||||
| 316 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 317 | |||||||||||||||
| 318 | Based on code from L<Dancer::Session::DBI> written by James Aitken | ||||||||||||||
| 319 | and code from L<Dancer::Plugin::DBIC> written by Naveed Massjouni. | ||||||||||||||
| 320 | |||||||||||||||
| 321 | Enhancements provided by: | ||||||||||||||
| 322 | |||||||||||||||
| 323 | Yanick Champoux (GH #6, #7). | ||||||||||||||
| 324 | Peter Mottram (GH #5). | ||||||||||||||
| 325 | |||||||||||||||
| 326 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 327 | |||||||||||||||
| 328 | This software is copyright (c) Stefan Hornburg. | ||||||||||||||
| 329 | |||||||||||||||
| 330 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 331 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 332 | |||||||||||||||
| 333 | =cut | ||||||||||||||
| 334 | |||||||||||||||
| 335 | |||||||||||||||
| 336 | 2 | 1; | |||||||||||||