implement 'change_session_expires'
Gerda Shank [Mon, 23 Apr 2012 17:11:29 +0000 (13:11 -0400)]
lib/Catalyst/Plugin/Session.pm
t/cat_test.t
t/lib/SessionTestApp/Controller/Root.pm
t/live_app.t

index 8c102dc..59c94fb 100644 (file)
@@ -359,16 +359,36 @@ sub session_expires {
 
 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 );
@@ -832,6 +852,12 @@ you should call change_session_id in your login controller like this:
         ...
       }
 
+=item change_session_expires $expires
+
+You can change the session expiration time for this session;
+
+    $c->change_session_expires( 4000 );
+
 =back
 
 =head1 INTERNAL METHODS
@@ -960,6 +986,9 @@ dumped objects if session ID is defined.
 
 =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
index 19cec42..c30b8dc 100644 (file)
@@ -18,9 +18,9 @@ my $cookie = $res->header('Set-Cookie');
 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;
index 37c1931..203815b 100644 (file)
@@ -28,10 +28,10 @@ sub logout : Global {
     $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 {
@@ -114,9 +114,9 @@ sub dump_these_loads_session : 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);
 }
 
index 97f7102..6b9c3ad 100644 (file)
@@ -93,19 +93,17 @@ $ua4->content_contains( "please login", "ua4 not logged in" );
 $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");