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

(-)a/C4/Auth.pm (-15 / +11 lines)
Lines 56-62 BEGIN { Link Here
56
    @ISA       = qw(Exporter);
56
    @ISA       = qw(Exporter);
57
    @EXPORT    = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
57
    @EXPORT    = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
58
    @EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
58
    @EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
59
      &get_all_subpermissions &get_user_subpermissions track_login_for_session
59
      &get_all_subpermissions &get_user_subpermissions track_login_daily
60
    );
60
    );
61
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
61
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
62
    $ldap      = C4::Context->config('useldapserver') || 0;
62
    $ldap      = C4::Context->config('useldapserver') || 0;
Lines 1202-1208 sub checkauth { Link Here
1202
            );
1202
            );
1203
        }
1203
        }
1204
1204
1205
        track_login_for_session( $userid );
1205
        track_login_daily( $userid );
1206
1206
1207
        return ( $userid, $cookie, $sessionID, $flags );
1207
        return ( $userid, $cookie, $sessionID, $flags );
1208
    }
1208
    }
Lines 2077-2106 sub getborrowernumber { Link Here
2077
    return 0;
2077
    return 0;
2078
}
2078
}
2079
2079
2080
=head2 track_login_for_session
2080
=head2 track_login_daily
2081
2081
2082
  track_login_for_session( $userid );
2082
    track_login_daily( $userid );
2083
2083
2084
C<$userid> the userid of the member
2084
Wraps the call to $patron->track_login, the method used to update borrowers.lastseen. We only call track_login once a day.
2085
2086
Wraps the call to $patron->track_login, the method used to update borrowers.lastseen.
2087
2085
2088
=cut
2086
=cut
2089
2087
2090
sub track_login_for_session {
2088
sub track_login_daily {
2091
    my $userid = shift;
2089
    my $userid = shift;
2092
    return unless $userid;
2090
    return if !$userid || !C4::Context->preference('TrackLastPatronActivity');
2093
2094
    my $patron = Koha::Patrons->find( { userid => $userid } );
2095
    return unless $patron;
2096
2091
2097
    my $cache     = Koha::Caches->get_instance();
2092
    my $cache     = Koha::Caches->get_instance();
2098
    my $cache_key = "seen-for-session-" . $patron->id;
2093
    my $cache_key = "track_login_" . $userid;
2099
    my $cached    = $cache->get_from_cache( $cache_key, { unsafe => 1 } );
2094
    my $cached    = $cache->get_from_cache($cache_key);
2100
2101
    my $today = dt_from_string()->ymd;
2095
    my $today = dt_from_string()->ymd;
2102
    return if $cached && $cached eq $today;
2096
    return if $cached && $cached eq $today;
2103
2097
2098
    my $patron = Koha::Patrons->find({ userid => $userid });
2099
    return unless $patron;
2104
    $patron->track_login;
2100
    $patron->track_login;
2105
    $cache->set_in_cache( $cache_key, $today );
2101
    $cache->set_in_cache( $cache_key, $today );
2106
}
2102
}
(-)a/t/db_dependent/Auth.t (-12 / +11 lines)
Lines 67-73 subtest 'checkauth() tests' => sub { Link Here
67
67
68
};
68
};
69
69
70
subtest 'track_login_for_session() tests' => sub {
70
subtest 'track_login_daily tests' => sub {
71
71
72
    plan tests => 5;
72
    plan tests => 5;
73
73
Lines 78-111 subtest 'track_login_for_session() tests' => sub { Link Here
78
    $patron->store();
78
    $patron->store();
79
79
80
    my $cache     = Koha::Caches->get_instance();
80
    my $cache     = Koha::Caches->get_instance();
81
    my $cache_key = "seen-for-session-" . $patron->id;
81
    my $cache_key = "track_login_" . $patron->userid;
82
    $cache->clear_from_cache($cache_key);
82
    $cache->clear_from_cache($cache_key);
83
83
84
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
84
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
85
85
86
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
86
    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
87
87
88
    C4::Auth::track_login_for_session( $userid );
88
    C4::Auth::track_login_daily( $userid );
89
    $patron->_result()->discard_changes();
89
    $patron->_result()->discard_changes();
90
    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' );
91
91
92
    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
93
    my $last_seen = $patron->lastseen;
93
    my $last_seen = $patron->lastseen;
94
    C4::Auth::track_login_for_session( $userid );
94
    C4::Auth::track_login_daily( $userid );
95
    $patron->_result()->discard_changes();
95
    $patron->_result()->discard_changes();
96
    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 still be unchanged' );
97
97
98
    $cache->clear_from_cache($cache_key);
98
    $cache->clear_from_cache($cache_key);
99
    C4::Auth::track_login_for_session( $userid );
99
    C4::Auth::track_login_daily( $userid );
100
    $patron->_result()->discard_changes();
100
    $patron->_result()->discard_changes();
101
    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 we cleared the cache' );
102
102
103
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
103
    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
104
    sleep(1);
104
    $patron->lastseen( undef )->store;
105
    $last_seen = $patron->lastseen;
105
    $cache->clear_from_cache($cache_key);
106
    C4::Auth::track_login_for_session( $userid );
106
    C4::Auth::track_login_daily( $userid );
107
    $patron->_result()->discard_changes();
107
    $patron->_result()->discard_changes();
108
    is( $patron->lastseen, $last_seen, 'Patron should have last seen unchanged when TrackLastPatronActivity = 0' );
108
    is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
109
109
110
};
110
};
111
111
112
- 

Return to bug 18821