OpenInteract2/SessionManager.pm

filter: Statementstmt 15.0% 18 / 120 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 3 totaltotal 10.2% 23 / 225 CC 37 SCAR 37.5
CC
37
Cov
9%
create
90.0
is_session_valid
42.0
save
42.0
CRAP 42.5
« OpenInteract2/Auth/Group.pm ↑ Summary OpenInteract2/Setup/RequireClasses.pm »
1 package OpenInteract2::SessionManager;
2  
3 # $Id: SessionManager.pm,v 1.9 2005/03/18 04:09:48 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use OpenInteract2::Constants qw( :log SESSION_COOKIE );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Context qw( CTX DEPLOY_URL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 87 $OpenInteract2::SessionManager::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/);
12  
13 87 my ( $log );
14  
15 0 ▶sub create {
Subroutine create: not called CC=9 SCAR=45.0
Pod undocumented
16 0 my ( $class, $session_id ) = @_;
17 0 ▶ $log ||= get_logger( LOG_SESSION );
Condition0%0/3
$log ||= get_logger("OI2.SESSION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.SESSION")
$log get_logger("OI2.SESSION")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.SESSION")
ABresult
1X1
011
000
18  
19 0 my $session_config = CTX->lookup_session_config;
20 0 my @validation_errors = $class->_validate_config( $session_config );
21 0 ▶ if ( scalar @validation_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @validation_errors) 0 0 0
22 0 $log->error( join( "\n", @validation_errors ) );
23 0 oi_error "Errors in session configuration: ",
24 join( "\n", @validation_errors );
25 }
26  
27 0 my $session = eval {
28 0 $class->_create_session( $session_config, $session_id )
29 };
30 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
31 0 $log->warn( "Error fetching session with ID '$session_id': $@\n",
32 "Continuing..." );
33 0 OpenInteract2::Cookie->expire( SESSION_COOKIE );
34 0 return {};
35 }
36 $log->is_debug &&
37 $log->debug( "Retrieved session w/keys ",
38 0 ▶ join( ', ', keys %{ $session } ) );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Retrieved session w/keys ", join(", ", keys %{$session;})) 0 0 0
39  
40 0 ▶ if ( $session_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session_id) { } 0 0 0
41  
42 # Check to see if we should expire the session
43  
44 0 ▶ unless ( $class->is_session_valid( $session ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->is_session_valid($session)) 0 0 0
45 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Session is expired; clearing out.") 0 0 0
46 $log->info( "Session is expired; clearing out." );
47 0 eval { tied( %{ $session } )->delete() };
48 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
49 0 $log->warn( "Caught error trying to remove expired session: $@\n",
50 "Continuing without problem since this just means",
51 "you'll have a stale session in your datastore" );
52 }
53 0 return {};
54 }
55 }
56 else {
57 0 $session->{is_new}++;
58 }
59 0 return $session;
60 }
61  
62  
63 0 ▶sub is_session_valid {
Subroutine is_session_valid: not called CC=6 SCAR=37.4
Pod undocumented
64 0 my ( $class, $session ) = @_;
65 0 ▶ $log ||= get_logger( LOG_SESSION );
Condition0%0/3
$log ||= get_logger("OI2.SESSION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.SESSION")
$log get_logger("OI2.SESSION")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.SESSION")
ABresult
1X1
011
000
66  
67 0 ▶ return 1 unless ( $session->{timestamp} );
Branch: 1 branch
BranchTrueFalseTotal
unless $session->{"timestamp"} 0 0 0
68  
69 0 my $session_config = CTX->lookup_session_config;
70 0 my $expires_in = $session_config->{expires_in};
71 0 ▶ return 1 unless ( $expires_in > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $expires_in > 0 0 0 0
72  
73 0 my $last_refresh = ( time - $session->{timestamp} ) / 60;
74 0 ▶ return 1 unless ( $last_refresh > $expires_in );
Branch: 1 branch
BranchTrueFalseTotal
unless $last_refresh > $expires_in 0 0 0
75  
76 $log->is_info &&
77 $log->info( "Session has expired. Last refresh was ",
78 "[", sprintf( '%5.2f', $last_refresh ) , "] minutes ago at ",
79 0 ▶ "[", scalar localtime( $session->{timestamp} ) , "]",
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Session has expired. Last refresh was ", "[", sprintf("%5.2f", $last_refresh), "] minutes ago at ", "[", scalar localtime $session->{"timestamp"}, "]", "and threshold is [", $expires_in, "] minutes") 0 0 0
80 "and threshold is [", $expires_in, "] minutes" );
81 0 return undef;
82 }
83  
84  
85 0 ▶sub save {
Subroutine save: not called CC=6 SCAR=37.4
Pod undocumented
86 0 my ( $class, $session ) = @_;
87 0 ▶ $log ||= get_logger( LOG_SESSION );
Condition0%0/3
$log ||= get_logger("OI2.SESSION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.SESSION")
$log get_logger("OI2.SESSION")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.SESSION")
ABresult
1X1
011
000
88  
89 0 ▶ unless ( ref $session ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $session) 0 0 0
90 0 ▶ $log->is_info && $log->info( "No session found, not storing" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("No session found, not storing") 0 0 0
91 0 return 1;
92 }
93  
94 0 ▶ if ( tied %{ $session } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (tied %{$session;}) { } 0 0 0
elsif (ref $session eq 'HASH') { } 0 0 0
95 0 $class->_store_tied_session( $session );
96 }
97 elsif ( ref $session eq 'HASH' ) {
98 0 $class->_store_hashref_session( $session );
99 }
100 else {
101 0 $log->warn( "No session reference returned from request. ",
102 "This should not happen...." );
103 }
104 0 return 1;
105 }
106  
107 0 ▶sub _store_tied_session {
Subroutine _store_tied_session: not called CC=6 SCAR=37.4
108 0 my ( $class, $session ) = @_;
109 0 my %useful_session_keys = map { $_ => 1 } keys %{ $session };
110 0 delete $useful_session_keys{_session_id};
111 0 delete $useful_session_keys{is_new};
112 0 ▶ if ( ref $session->{_oi_cache} and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $session->{"_oi_cache"} and scalar keys %{$$session{"_oi_cache"};} == 0) 0 0 0
Condition0%0/3
ref $session->{"_oi_cache"} and scalar keys %{$$session{"_oi_cache"};} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref $session->{"_oi_cache"} and scalar keys %{$$session{"_oi_cache"};} == 0
ref $session->{"_oi_cache"} scalar keys %{$$session{"_oi_cache"};} == 0
Truth table0%0/3
A and B
A= ref $session->{"_oi_cache"}B= scalar keys %{$$session{"_oi_cache"};} == 0
ABresult
0X0
100
111
113 0 scalar keys %{ $session->{_oi_cache} } == 0 ) {
114 0 delete $useful_session_keys{_oi_cache};
115 }
116 0 ▶ if ( scalar keys %useful_session_keys > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %useful_session_keys > 0) { } 0 0 0
117 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Storing tied session because of useful keys: ", join(", ", keys %useful_session_keys)) 0 0 0
118 $log->info( "Storing tied session because of useful keys: ",
119 join( ', ', keys %useful_session_keys ) );
120 0 my $is_new = $session->{is_new};
121 0 delete $session->{is_new};
122 0 $class->_save_session( $session, $is_new );
123 }
124 else {
125 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Tied session, no useful info; removing...") 0 0 0
126 $log->debug( "Tied session, no useful info; removing..." );
127 0 tied( %{ $session } )->delete();
128 }
129 }
130  
131 0 ▶sub _store_hashref_session {
Subroutine _store_hashref_session: not called CC=6 SCAR=37.4
132 0 my ( $class, $session ) = @_;
133 0 ▶ unless ( scalar keys %{ $session } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$session;}) 0 0 0
134 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Hashref session, no useful info; not storing...") 0 0 0
135 $log->debug( "Hashref session, no useful info; not storing..." );
136 0 return 1;
137 }
138 $log->is_info &&
139 $log->info( "Create new session with data from hashref using ",
140 0 ▶ "keys: ", keys %{ $session } );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Create new session with data from hashref using ", "keys: ", keys %{$session;}) 0 0 0
141 0 my $new_session = $class->_create_session;
142 0 ▶ if ( $new_session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_session) { } 0 0 0
143 0 foreach my $key ( keys %{ $session } ) {
144 0 $new_session->{ $key } = $session->{ $key };
145 }
146 0 $class->_save_session( $new_session, 1 );
147 }
148 else {
149 0 $log->error( "No value returned from _create_session; ",
150 "this shouldn't happen..." );
151 }
152 }
153  
154 0 ▶sub _save_session {
Subroutine _save_session: not called CC=5 SCAR=34.0
155 0 my ( $class, $session, $is_new ) = @_;
156 0 ▶ $log ||= get_logger( LOG_SESSION );
Condition0%0/3
$log ||= get_logger("OI2.SESSION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.SESSION")
$log get_logger("OI2.SESSION")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.SESSION")
ABresult
1X1
011
000
157  
158 0 $session->{timestamp} = time;
159 $log->is_debug &&
160 $log->debug( "Saving tied session [New? $is_new] with keys ",
161 0 ▶ join( ", ", keys %{ $session } ) );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Saving tied session [New? $is_new] with keys ", join(", ", keys %{$session;})) 0 0 0
162 0 my $session_id = $session->{_session_id};
163  
164 # Keep this here even though we may not need it since $session
165 # loses its magic (and possibly values) after it's untied
166  
167 0 my $expiration = $class->_get_expiration( $session );
168  
169 0 untie %{ $session };
170 0 ▶ if ( $is_new ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new) 0 0 0
171 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Sending cookie for new session; cookie should ", "expire in: ", $expiration) 0 0 0
172 $log->info( "Sending cookie for new session; cookie should ",
173 "expire in: ", $expiration );
174 0 OpenInteract2::Cookie->create(
175 { name => SESSION_COOKIE,
176 value => $session_id,
177 path => DEPLOY_URL,
178 expires => $expiration,
179 HEADER => 'yes' });
180 }
181 }
182  
183 # If the session key 'expiration' is defined then use that, even if
184 # the value in the session is blank or undef. This allows you to set
185 # sessions that expire when the browser is closed when a user requests
186 # (see OpenInteract2::Auth).
187  
188 0 ▶sub _get_expiration {
Subroutine _get_expiration: not called CC=5 SCAR=34.0
189 0 my ( $class, $session ) = @_;
190 0 ▶ $log ||= get_logger( LOG_SESSION );
Condition0%0/3
$log ||= get_logger("OI2.SESSION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.SESSION")
$log get_logger("OI2.SESSION")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.SESSION")
ABresult
1X1
011
000
191  
192 0 my ( $expiration );
193 0 ▶ if ( exists $session->{expiration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $session->{'expiration'}) { } 0 0 0
194 0 $expiration = $session->{expiration};
195 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Expiration for new session manually ", "set to [$expiration]") 0 0 0
196 $log->info( "Expiration for new session manually ",
197 "set to [$expiration]" );
198 0 delete $session->{expiration};
199 }
200 else {
201 0 $expiration = CTX->lookup_session_config->{expiration};
202 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Expiration for new session set to default ", "from config [$expiration]") 0 0 0
203 $log->info( "Expiration for new session set to default ",
204 "from config [$expiration]" );
205 }
206 0 return $expiration;
207 }
208  
209 ########################################
210 # OVERRIDE
211  
212 0 ▶sub _create_session { return undef }
Subroutine _create_session: not called CC=1 SCAR=6.9
213 0 ▶sub _validate_config { return () }
Subroutine _validate_config: not called CC=1 SCAR=6.9
214  
215 87 1;
216  
217 __END__
« OpenInteract2/Auth/Group.pm ↑ Summary OpenInteract2/Setup/RequireClasses.pm »