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

(-)a/C4/Auth.pm (-11 / +16 lines)
Lines 33-38 use C4::Search::History; Link Here
33
use Koha;
33
use Koha;
34
use Koha::Caches;
34
use Koha::Caches;
35
use Koha::AuthUtils qw(get_script_name hash_password);
35
use Koha::AuthUtils qw(get_script_name hash_password);
36
use Koha::DateUtils qw(dt_from_string);
36
use Koha::Library::Groups;
37
use Koha::Library::Groups;
37
use Koha::Libraries;
38
use Koha::Libraries;
38
use Koha::Patrons;
39
use Koha::Patrons;
Lines 1201-1207 sub checkauth { Link Here
1201
            );
1202
            );
1202
        }
1203
        }
1203
1204
1204
        track_login_for_session( $userid, $session );
1205
        track_login_for_session( $userid );
1205
1206
1206
        return ( $userid, $cookie, $sessionID, $flags );
1207
        return ( $userid, $cookie, $sessionID, $flags );
1207
    }
1208
    }
Lines 2078-2103 sub getborrowernumber { Link Here
2078
2079
2079
=head2 track_login_for_session
2080
=head2 track_login_for_session
2080
2081
2081
  track_login_for_session( $userid, $session );
2082
  track_login_for_session( $userid );
2082
2083
2083
C<$userid> the userid of the member
2084
C<$userid> the userid of the member
2084
C<$session> the CGI::Session object used to store the session's state.
2085
2085
2086
Wraps the call to $patron->track_login, the method used to update borrowers.lastseen.
2086
Wraps the call to $patron->track_login, the method used to update borrowers.lastseen.
2087
2087
2088
=cut
2088
=cut
2089
2089
2090
sub track_login_for_session {
2090
sub track_login_for_session {
2091
    my ( $userid, $session ) = @_;
2091
    my $userid = shift;
2092
    return unless $userid;
2092
2093
2093
    if ( $userid && $session && !$session->param('tracked_for_session') ) {
2094
    my $patron = Koha::Patrons->find( { userid => $userid } );
2094
        $session->param( 'tracked_for_session', 1 );
2095
    return unless $patron;
2095
        $session->flush();
2096
2096
2097
        # track_login also depends on pref TrackLastPatronActivity
2097
    my $cache     = Koha::Caches->get_instance();
2098
        my $patron = Koha::Patrons->find( { userid => $userid } );
2098
    my $cache_key = "seen-for-session-" . $patron->id;
2099
        $patron->track_login if $patron;
2099
    my $cached    = $cache->get_from_cache( $cache_key, { unsafe => 1 } );
2100
    }
2100
2101
    my $today = dt_from_string()->ymd;
2102
    return if $cached && $cached eq $today;
2103
2104
    $patron->track_login;
2105
    $cache->set_in_cache( $cache_key, $today );
2101
}
2106
}
2102
2107
2103
END { }    # module clean-up code here (global destructor)
2108
END { }    # module clean-up code here (global destructor)
(-)a/t/db_dependent/Auth.t (-15 / +9 lines)
Lines 77-114 subtest 'track_login_for_session() tests' => sub { Link Here
77
    $patron->lastseen( undef );
77
    $patron->lastseen( undef );
78
    $patron->store();
78
    $patron->store();
79
79
80
    # Mock a CGI object with real userid param
80
    my $cache     = Koha::Caches->get_instance();
81
    my $cgi = Test::MockObject->new();
81
    my $cache_key = "seen-for-session-" . $patron->id;
82
    $cgi->mock( 'param', sub { return $patron; } );
82
    $cache->clear_from_cache($cache_key);
83
    $cgi->mock( 'cookie', sub { return; } );
84
85
    my $session = new CGI::Session( undef, undef, { Directory => File::Spec->tmpdir } );
86
83
87
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
84
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
88
85
89
    C4::Auth::track_login_for_session( $userid );
86
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
90
    $patron->_result()->discard_changes();
91
    is( $patron->lastseen, undef, 'Patron last seen should be unchanged if no session is passed' );
92
87
93
    C4::Auth::track_login_for_session( $userid, $session );
88
    C4::Auth::track_login_for_session( $userid );
94
    $patron->_result()->discard_changes();
89
    $patron->_result()->discard_changes();
95
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
90
    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
96
91
97
    sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
92
    sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
98
    my $last_seen = $patron->lastseen;
93
    my $last_seen = $patron->lastseen;
99
    C4::Auth::track_login_for_session( $userid, $session );
94
    C4::Auth::track_login_for_session( $userid );
100
    $patron->_result()->discard_changes();
95
    $patron->_result()->discard_changes();
101
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged if passed the same session' );
96
    is( $patron->lastseen, $last_seen, 'Patron last seen should be unchanged if passed the same session' );
102
97
103
    $session = new CGI::Session( undef, undef, { Directory => File::Spec->tmpdir } );
98
    $cache->clear_from_cache($cache_key);
104
    C4::Auth::track_login_for_session( $userid, $session );
99
    C4::Auth::track_login_for_session( $userid );
105
    $patron->_result()->discard_changes();
100
    $patron->_result()->discard_changes();
106
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed if given a new session' );
101
    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed if given a new session' );
107
102
108
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
103
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
109
    sleep(1);
104
    sleep(1);
110
    $last_seen = $patron->lastseen;
105
    $last_seen = $patron->lastseen;
111
    C4::Auth::track_login_for_session( $userid, $session );
106
    C4::Auth::track_login_for_session( $userid );
112
    $patron->_result()->discard_changes();
107
    $patron->_result()->discard_changes();
113
    is( $patron->lastseen, $last_seen, 'Patron should have last seen unchanged when TrackLastPatronActivity = 0' );
108
    is( $patron->lastseen, $last_seen, 'Patron should have last seen unchanged when TrackLastPatronActivity = 0' );
114
109
115
- 

Return to bug 18821