sub extend_session_expires {
my ( $c, $expires ) = @_;
- $c->_extended_session_expires( my $updated = $c->calculate_extended_session_expires( $expires ) );
+ $c->_extended_session_expires( my $updated = $c->calculate_initial_session_expires( $expires ) );
$c->extend_session_id( $c->sessionid, $updated );
return $updated;
}
-sub calculate_initial_session_expires {
+sub change_session_expires {
+ my ( $c, $expires ) = @_;
+
+ $expires ||= 0;
+ my $sid = $c->sessionid;
+ my $time_exp = time() + $expires;
+ $c->store_session_data( "expires:$sid" => $time_exp );
+}
+
+sub initial_session_expires {
my $c = shift;
return ( time() + $c->_session_plugin_config->{expires} );
}
+sub calculate_initial_session_expires {
+ my $c = shift;
+
+ my $initial_expires = $c->initial_session_expires;
+ my $stored_session_expires = 0;
+ if ( my $sid = $c->sessionid ) {
+ $stored_session_expires = $c->get_session_data("expires:$sid") || 0;
+ }
+ return ( $initial_expires > $stored_session_expires ) ? $initial_expires : $stored_session_expires;
+}
+
sub calculate_extended_session_expires {
my ( $c, $prev ) = @_;
return ( time() + $prev );
...
}
+=item change_session_expires $expires
+
+You can change the session expiration time for this session;
+
+ $c->change_session_expires( 4000 );
+
=back
=head1 INTERNAL METHODS
=item extend_session_expires
+Note: this is *not* used to give an individual user a longer session. See
+'change_session_expires'.
+
=item extend_session_id
=item get_session_id
ok($cookie, 'Have a cookie');
# this checks that cookie persists across a redirect
-($res, $c) = ctx_request(GET 'http://localhost/logout_persist', Cookie => $cookie);
-is($res->code, 302, '/logout with cookie redirects');
-is($res->header('Location'), 'http://localhost/', 'Redirect to / after logout');
-ok($res->header('Set-Cookie'), 'Cookie is reset by /logout');
+($res, $c) = ctx_request(GET 'http://localhost/do_redirect', Cookie => $cookie);
+is($res->code, 302, 'redirected');
+is($res->header('Location'), 'http://localhost/page', 'Redirected after do_redirect');
+ok($res->header('Set-Cookie'), 'Cookie is still there after redirect');
done_testing;
$c->delete_session("logout");
}
-sub logout_persist : Global {
+sub do_redirect : Global {
my ( $self, $c ) = @_;
# session is not deleted
- $c->res->redirect( $c->uri_for('/') );
+ $c->res->redirect( $c->uri_for('page') );
}
sub set_session_variable : Global {
}
}
-sub extend_session_expires : Global {
+sub change_session_expires : Global {
my ($self, $c) = @_;
- $c->extend_session_expires(31536000);
+ $c->change_session_expires(31536000);
$c->res->output($c->session_expires);
}
$ua4->get_ok( "http://localhost/login", "log ua4 in" );
$ua4->content_contains( "logged in", "ua4 logged in" );
-$ua4->get_ok( "http://localhost/extend_session_expires", "ua4 extend expire session" );
-my ( $ua4_expires ) = ($ua4->content =~ /(\d+)$/);
-
-ok( ($ua4_expires - time() - 86400) >= 0, 'extend_session_expires with really long value' );
-
-sleep 1;
+$ua4->get( "http://localhost/page", "get page" );
+my ( $ua4_expires1 ) = ($ua4->content =~ /(\d+)$/);
+$ua4->get( "http://localhost/page", "get page" );
+my ( $ua4_expires2 ) = ($ua4->content =~ /(\d+)$/);
+is( $ua4_expires1, $ua4_expires2, 'expires has not changed' );
+$ua4->get( "http://localhost/change_session_expires", "get page" );
$ua4->get( "http://localhost/page", "get page" );
-my ( $ua4_expires_updated ) = ($ua4->content =~ /(\d+)$/);
-diag( "ua4_expires => $ua4_expires");
-diag( "ua4_expires_updated => $ua4_expires_updated");
-ok( $ua4_expires < $ua4_expires_updated, 'update extended session' );
+my ( $ua4_expires3 ) = ($ua4->content =~ /(\d+)$/);
+ok( $ua4_expires3 > ( $ua4_expires1 + 30000000), 'expires has been extended' );
diag("Testing against Catalyst $Catalyst::VERSION");
diag("Testing Catalyst::Plugin::Session $Catalyst::Plugin::Session::VERSION");