View | Details | Raw Unified | Return to bug 18821
Collapse All | Expand All

(-)a/C4/Auth.pm (-7 / +17 lines)
Lines 55-61 BEGIN { Link Here
55
    @ISA       = qw(Exporter);
55
    @ISA       = qw(Exporter);
56
    @EXPORT    = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
56
    @EXPORT    = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
57
    @EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
57
    @EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
58
      &get_all_subpermissions &get_user_subpermissions
58
      &get_all_subpermissions &get_user_subpermissions track_login_for_session
59
    );
59
    );
60
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
60
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
61
    $ldap      = C4::Context->config('useldapserver') || 0;
61
    $ldap      = C4::Context->config('useldapserver') || 0;
Lines 802-807 sub checkauth { Link Here
802
    my $casparam = $query->param('cas');
802
    my $casparam = $query->param('cas');
803
    my $q_userid = $query->param('userid') // '';
803
    my $q_userid = $query->param('userid') // '';
804
804
805
    my $session;
806
805
    # Basic authentication is incompatible with the use of Shibboleth,
807
    # Basic authentication is incompatible with the use of Shibboleth,
806
    # as Shibboleth may return REMOTE_USER as a Shibboleth attribute,
808
    # as Shibboleth may return REMOTE_USER as a Shibboleth attribute,
807
    # and it may not be the attribute we want to use to match the koha login.
809
    # and it may not be the attribute we want to use to match the koha login.
Lines 827-833 sub checkauth { Link Here
827
    }
829
    }
828
    elsif ( $sessionID = $query->cookie("CGISESSID") )
830
    elsif ( $sessionID = $query->cookie("CGISESSID") )
829
    {    # assignment, not comparison
831
    {    # assignment, not comparison
830
        my $session = get_session($sessionID);
832
        $session = get_session($sessionID);
831
        C4::Context->_new_userenv($sessionID);
833
        C4::Context->_new_userenv($sessionID);
832
        my ( $ip, $lasttime, $sessiontype );
834
        my ( $ip, $lasttime, $sessiontype );
833
        my $s_userid = '';
835
        my $s_userid = '';
Lines 1199-1209 sub checkauth { Link Here
1199
            );
1201
            );
1200
        }
1202
        }
1201
1203
1202
        if ( $userid ) {
1204
        track_login_for_session( $userid, $session );
1203
            # track_login also depends on pref TrackLastPatronActivity
1204
            my $patron = Koha::Patrons->find({ userid => $userid });
1205
            $patron->track_login if $patron;
1206
        }
1207
1205
1208
        return ( $userid, $cookie, $sessionID, $flags );
1206
        return ( $userid, $cookie, $sessionID, $flags );
1209
    }
1207
    }
Lines 2078-2083 sub getborrowernumber { Link Here
2078
    return 0;
2076
    return 0;
2079
}
2077
}
2080
2078
2079
sub track_login_for_session {
2080
    my ( $userid, $session ) = @_;
2081
2082
    if ( $userid && $session && !$session->param('tracked_for_session') ) {
2083
        $session->param( 'tracked_for_session', 1 );
2084
2085
        # track_login also depends on pref TrackLastPatronActivity
2086
        my $patron = Koha::Patrons->find( { userid => $userid } );
2087
        $patron->track_login if $patron;
2088
    }
2089
}
2090
2081
END { }    # module clean-up code here (global destructor)
2091
END { }    # module clean-up code here (global destructor)
2082
1;
2092
1;
2083
__END__
2093
__END__
(-)a/t/db_dependent/Auth.t (-2 / +48 lines)
Lines 10-16 use CGI qw ( -utf8 ); Link Here
10
use Test::MockObject;
10
use Test::MockObject;
11
use Test::MockModule;
11
use Test::MockModule;
12
use List::MoreUtils qw/all any none/;
12
use List::MoreUtils qw/all any none/;
13
use Test::More tests => 21;
13
use Test::More tests => 22;
14
use Test::Warn;
14
use Test::Warn;
15
use t::lib::Mocks;
15
use t::lib::Mocks;
16
use t::lib::TestBuilder;
16
use t::lib::TestBuilder;
Lines 67-72 subtest 'checkauth() tests' => sub { Link Here
67
67
68
};
68
};
69
69
70
subtest 'track_login_for_session() tests' => sub {
71
72
    plan tests => 5;
73
74
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
75
    my $userid = $patron->userid;
76
77
    $patron->lastseen( undef );
78
    $patron->store();
79
80
    # Mock a CGI object with real userid param
81
    my $cgi = Test::MockObject->new();
82
    $cgi->mock( 'param', sub { return $patron; } );
83
    $cgi->mock( 'cookie', sub { return; } );
84
85
    my $session = new CGI::Session( undef, undef, { Directory => File::Spec->tmpdir } );
86
87
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
88
89
    C4::Auth::track_login_for_session( $userid );
90
    $patron->_result()->discard_changes();
91
    is( $patron->lastseen, undef, 'Patron last seen should be unchanged if no session is passed' );
92
93
    C4::Auth::track_login_for_session( $userid, $session );
94
    $patron->_result()->discard_changes();
95
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
96
97
	sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
98
    my $last_seen = $patron->lastseen;
99
    C4::Auth::track_login_for_session( $userid, $session );
100
    $patron->_result()->discard_changes();
101
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged if passed the same session' );
102
103
    $session = new CGI::Session( undef, undef, { Directory => File::Spec->tmpdir } );
104
    C4::Auth::track_login_for_session( $userid, $session );
105
    $patron->_result()->discard_changes();
106
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed if given a new session' );
107
108
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
109
    sleep(1);
110
    $last_seen = $patron->lastseen;
111
    C4::Auth::track_login_for_session( $userid, $session );
112
    $patron->_result()->discard_changes();
113
    is( $patron->lastseen, $last_seen, 'Patron should have last seen unchanged when TrackLastPatronActivity = 0' );
114
115
};
116
70
my $hash1 = hash_password('password');
117
my $hash1 = hash_password('password');
71
my $hash2 = hash_password('password');
118
my $hash2 = hash_password('password');
72
119
73
- 

Return to bug 18821