OpenInteract2/Auth/User.pm

filter: Statementstmt 10.3% 15 / 145 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 28.5% 4 / 14 Podpod 0.0% 0 / 1 totaltotal 6.6% 19 / 285 CC 48 SCAR 47.5
CC
48
Cov
6%
get_user
272.0
_login_user_from_input
132.0
_remember_login
72.0
CRAP 115.3
« OpenInteract2/Manage/Website/TestDB.pm ↑ Summary OpenInteract2/CreateSecurity.pm »
1 package OpenInteract2::Auth::User;
2  
3 # $Id: User.pm,v 1.22 2005/03/17 14:57:59 sjn Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 $OpenInteract2::Auth::User::VERSION = sprintf("%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/);
11  
12 1 my ( $log );
13  
14 0 ▶sub get_user {
Subroutine get_user: not called CC=16 SCAR=56.1
Pod undocumented
15 0 my ( $class, $auth ) = @_;
16 0 my $server_config = CTX->server_config;
17 0 my ( $user, $user_id, $is_logged_in );
18 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
19  
20 # Check to see if the user is in the session
21  
22 0 ( $user, $user_id ) = $class->_get_cached_user;
23 0 ▶ if ( $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
24 0 $is_logged_in = 'yes';
25 0 ▶ $log->is_info && $log->info( "Found user from session cache" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Found user from session cache") 0 0 0
26 }
27 else {
28 0 ▶ $user_id ||= $class->_get_user_id;
Condition0%0/3
$user_id ||= $class->_get_user_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_id ||= $class->_get_user_id
$user_id $class->_get_user_id
Truth table0%0/3
A ||= B
A= $user_idB= $class->_get_user_id
ABresult
1X1
011
000
29 0 ▶ if ( $user_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_id) 0 0 0
30 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Found user ID '${user_id}'; fetching user") 0 0 0
31 $log->debug( "Found user ID '$user_id'; fetching user" );
32 0 $user = eval { $class->_fetch_user( $user_id ) };
33  
34 # If there's a failure fetching the user, we need to ensure that
35 # this user_id is not passed back to us again so we don't keep
36 # going through this process...
37  
38 0 ▶ if ( $@ or ! $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $user) { } 0 0 0
Condition0%0/3
$@ or not $user
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $user
$@ not $user
Truth table0%0/3
A or not(B)
A= $@B= not $user
ABresult
1X1
011
000
39 0 ▶ my $error = $@ || 'User not found';
Condition0%0/2
$@ || 'User not found'
l!l
00
MC/DC0%0/1
$@ || 'User not found'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
40 0 $class->_fetch_user_failed( $user_id, $error );
41 }
42 else {
43 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info(qq[User found '$user->{"login_name"}']) 0 0 0
44 $log->info( "User found '$user->{login_name}'" );
45 0 $class->_check_first_login( $user );
46 0 $class->_set_cached_user( $user );
47 0 $is_logged_in = 'yes';
48 }
49 }
50 }
51  
52 0 ▶ if ( $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
53 0 $auth->user( $user );
54 0 $auth->is_logged_in( $is_logged_in );
55 0 return ( $user, $is_logged_in );
56 }
57  
58 $log->is_info &&
59 0 ▶ $log->info( "No user ID found in session. Finding login..." );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("No user ID found in session. Finding login...") 0 0 0
60  
61 # If no user info found, check to see if the user logged in
62  
63 0 $user = $class->_login_user_from_input;
64  
65 # If so, see if it's the first one and if we should 'remember' the
66 # user (just changes the session expiration)
67  
68 0 ▶ if ( $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
69 0 $class->_check_first_login( $user );
70 0 $class->_remember_login( $user );
71 0 $class->_set_cached_user( $user );
72 0 $is_logged_in = 'yes'
73 }
74  
75 # If not, create a nonpersisted 'empty' user
76  
77 else {
78 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Creating the not-logged-in user.") 0 0 0
79 $log->info( "Creating the not-logged-in user." );
80 0 my $session = CTX->request->session;
81 0 ▶ if ( $session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
82 0 delete $session->{user_id};
83 }
84 0 $user = $class->_create_nologin_user;
85 0 $is_logged_in = 'no';
86 }
87 0 $auth->user( $user );
88 0 $auth->is_logged_in( $is_logged_in );
89 0 return ( $user, $is_logged_in );
90 }
91  
92  
93 # TODO: I don't like that this returns a user and user_id...
94  
95 0 ▶sub _get_cached_user {
Subroutine _get_cached_user: not called CC=7 SCAR=40.3
96 0 my ( $class ) = @_;
97 0 my $user_refresh = CTX->lookup_session_config->{cache_user};
98 0 ▶ return unless ( $user_refresh > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $user_refresh > 0 0 0 0
99  
100 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
101  
102 0 my ( $user, $user_id );
103 0 my $session = CTX->request->session;
104 0 ▶ if ( $user = $session->{_oi_cache}{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user = $session->{"_oi_cache"}{"user"}) 0 0 0
105 0 ▶ if ( time < $session->{_oi_cache}{user_refresh_on} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $session->{'_oi_cache'}{'user_refresh_on'}) { } 0 0 0
106 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Got user from session ok") 0 0 0
107 $log->info( "Got user from session ok" );
108 }
109  
110 # If we need to refresh the user object, pull the id out
111 # so we know what to refresh...
112  
113 else {
114 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("User session cache expired") 0 0 0
115 $log->info( "User session cache expired" );
116 0 $user_id = $user->id;
117 0 delete $session->{_oi_cache}{user};
118 0 delete $session->{_oi_cache}{user_refresh_on};
119 0 $user = undef;
120 }
121 }
122 0 return ( $user, $user_id );
123 }
124  
125  
126 0 ▶sub _set_cached_user {
Subroutine _set_cached_user: not called CC=4 SCAR=30.0
127 0 my ( $class, $user ) = @_;
128 0 my $user_refresh = CTX->lookup_session_config->{cache_user};
129 0 ▶ return unless ( $user_refresh > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $user_refresh > 0 0 0 0
130  
131 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
132  
133 0 my $session = CTX->request->session;
134 0 $session->{_oi_cache}{user} = $user;
135 0 $session->{_oi_cache}{user_refresh_on} = time + ( $user_refresh * 60 );
136 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Set user to session cache, expires in ", "'${user_refresh}' minutes") 0 0 0
137 $log->info( "Set user to session cache, expires in ",
138 "'$user_refresh' minutes" );
139 }
140  
141 # Just grab the user_id from somewhere
142  
143 0 ▶sub _get_user_id {
Subroutine _get_user_id: not called CC=2 SCAR=17.9
144 0 my ( $class ) = @_;
145 0 my $session = CTX->request->session;
146 0 ▶ return ( $session ) ? $session->{user_id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
147 }
148  
149  
150 # Use the user_id to create a user (don't use eval {} around the
151 # fetch(), this should die if it fails)
152  
153 0 ▶sub _fetch_user {
Subroutine _fetch_user: not called CC=1 SCAR=6.9
154 0 my ( $class, $user_id ) = @_;
155 0 return CTX->lookup_object( 'user' )
156 ->fetch( $user_id, { skip_security => 1 } );
157 }
158  
159  
160 # What to do if the user fetch fails
161  
162 0 ▶sub _fetch_user_failed {
Subroutine _fetch_user_failed: not called CC=2 SCAR=17.9
163 0 my ( $class, $user_id, $error ) = @_;
164 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
165 0 $log->error( "Failed to fetch user '$user_id': $error" );
166 0 CTX->request->session->{user_id} = undef;
167 0 $log->error( "Since user fetch failed, setting 'user_id' in ",
168 "session to undef to prevent this from recurring" );
169 }
170  
171  
172 # If no user found elsewhere, see if a login_name and password were
173 # passed in; if so, try and login the user and track the info
174  
175 0 ▶sub _login_user_from_input {
Subroutine _login_user_from_input: not called CC=11 SCAR=48.8
176 0 my ( $class ) = @_;
177 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
178 0 my $login_config = CTX->lookup_login_config;
179 0 my $login_field = $login_config->{login_field};
180 0 my $password_field = $login_config->{password_field};
181 0 ▶ unless ( $login_field and $password_field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login_field and $password_field) 0 0 0
Condition0%0/3
$login_field and $password_field
!ll&&!rl&&r
000
MC/DC0%0/2
$login_field and $password_field
$login_field $password_field
Truth table0%0/3
A and B
A= $login_fieldB= $password_field
ABresult
0X0
100
111
182 0 $log->error( "No login/password field configured; please set ",
183 "server configuration keys 'login.login_field' and ",
184 "'login.password_field'" );
185 0 return undef;
186 }
187  
188 0 my $request = CTX->request;
189 0 my $login_name = $request->param( $login_field );
190 0 ▶ unless ( $login_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login_name) 0 0 0
191 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("No login name found") 0 0 0
192 $log->info( "No login name found" );
193 0 return undef;
194 }
195 $log->is_info &&
196 0 ▶ $log->info( "Found login name [$login_name]" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Found login name [$login_name]") 0 0 0
197  
198 0 my $user = eval {
199 0 CTX->lookup_object( 'user' )
200 ->fetch_by_login_name( $login_name,
201 { skip_security => 1 } )
202 };
203 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
204 0 $log->error( "Error fetching user by login name: $@" );
205 }
206  
207 0 ▶ unless ( $user ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
208 0 $log->warn( "User with login '$login_name' not found." );
209 0 $request->add_action_message(
210 'login_box', 'login', 'Invalid login, please try again' );
211 0 return undef;
212 }
213  
214 # Check the password
215  
216 0 my $password = $request->param( $password_field );
217  
218 0 ▶ unless ( $user->check_password( $password ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user->check_password($password)) 0 0 0
219 0 $log->warn( "Password check for [$login_name] failed" );
220 0 $request->add_action_message(
221 'login_box', 'login', 'Invalid login, please try again' );
222 0 return undef;
223 }
224 $log->is_info &&
225 0 ▶ $log->info( "Passwords matched for UID ", $user->id );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Passwords matched for UID ", $user->id) 0 0 0
226  
227 0 return $user;
228 }
229  
230  
231 # If there's a removal date, then this is the user's first login
232  
233 # TODO: Check if this is working, if it's needed, ...
234  
235 0 ▶sub _check_first_login {
Subroutine _check_first_login: not called CC=5 SCAR=34.0
236 0 my ( $class, $user ) = @_;
237 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
238  
239 0 ▶ return unless ( $user->{removal_date} );
Branch: 1 branch
BranchTrueFalseTotal
unless $user->{"removal_date"} 0 0 0
240  
241 # blank out the removal date and put the user in the public group
242  
243 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("First login for user! Do some cleanup.") 0 0 0
244 $log->info( "First login for user! Do some cleanup." );
245 0 $user->{removal_date} = undef;
246  
247 0 eval {
248 0 $user->save({ skip_security => 1 });
249 0 $user->make_public;
250 };
251 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
252 0 $log->error( "Failed to save new user info at first login: $@" );
253 }
254 }
255  
256 # If we created a user, make the expiration transient unless told otherwise.
257  
258 0 ▶sub _remember_login {
Subroutine _remember_login: not called CC=8 SCAR=42.8
259 0 my ( $class, $user ) = @_;
260 0 ▶ $log ||= get_logger( LOG_AUTH );
Condition0%0/3
$log ||= get_logger("OI2.AUTH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.AUTH")
$log get_logger("OI2.AUTH")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.AUTH")
ABresult
1X1
011
000
261  
262 0 my $login_config = CTX->lookup_login_config;
263 0 ▶ if ( $login_config->{always_remember} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($login_config->{"always_remember"}) 0 0 0
264 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Configured to always remember users, session ", "should exist past browser shutdown") 0 0 0
265 $log->info( "Configured to always remember users, session ",
266 "should exist past browser shutdown" );
267 0 return;
268 }
269  
270 0 my $request = CTX->request;
271 0 my $remember_field = $login_config->{remember_field};
272 0 my ( $do_remember );
273 0 ▶ if ( $remember_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remember_field) 0 0 0
274 0 $do_remember = $request->param( $remember_field );
275 }
276 0 ▶ if ( $do_remember ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_remember) { } 0 0 0
277 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Remembering user, session should exist past ", "browser shutdown") 0 0 0
278 $log->info( "Remembering user, session should exist past ",
279 "browser shutdown" );
280 }
281 else {
282 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Not remembering user, session should expire ", "on browser shutdown") 0 0 0
283 $log->info( "Not remembering user, session should expire ",
284 "on browser shutdown" );
285 0 $request->session->{expiration} = undef;
286 }
287 }
288  
289 # Create a 'dummy' user
290  
291 0 ▶sub _create_nologin_user {
Subroutine _create_nologin_user: not called CC=1 SCAR=6.9
292 0 my ( $class ) = @_;
293 0 my $default_theme_id = CTX->lookup_default_object_id( 'theme' );
294 0 return CTX->lookup_object( 'user' )
295 ->new({ login_name => 'anonymous',
296 first_name => 'Anonymous',
297 last_name => 'User',
298 theme_id => $default_theme_id,
299 user_id => 99999 });
300 }
301  
302 1 1;
303  
304 __END__
« OpenInteract2/Manage/Website/TestDB.pm ↑ Summary OpenInteract2/CreateSecurity.pm »