-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Auto Logout functionality (#815)
* Add Auto Logout functionality * Update middleware to the latest & greatest * Upgrade settings for MIDDLEWARE * Update order of imports * Fix test check for tock session activity data * Move the fmt up
- Loading branch information
1 parent
10d471c
commit 68f9abb
Showing
5 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from datetime import datetime, timedelta | ||
from django.conf import settings | ||
from django.contrib import auth | ||
|
||
|
||
class AutoLogout(object): | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
fmt = '%Y%m%d%H%M%S' | ||
|
||
# Check if user exists and is logged in | ||
if request.user and request.user.is_authenticated(): | ||
|
||
logout_time_in_seconds = settings.AUTO_LOGOUT_DELAY_MINUTES * 60 | ||
|
||
# Compare the time of the last activity with the logout delay | ||
try: | ||
session_time = datetime.strptime( | ||
request.session['tock_last_activity'], | ||
fmt | ||
) | ||
if datetime.now() - session_time > \ | ||
timedelta(seconds=logout_time_in_seconds): | ||
auth.logout(request) | ||
del request.session['tock_last_activity'] | ||
return self.get_response(request) | ||
except KeyError: | ||
pass | ||
|
||
request.session['tock_last_activity'] = \ | ||
datetime.now().strftime(fmt) | ||
|
||
return self.get_response(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import time | ||
from django.test import TestCase, override_settings | ||
from django.core.urlresolvers import reverse | ||
|
||
from test_common import ProtectedViewTestCase | ||
|
||
|
||
@override_settings(AUTO_LOGOUT_DELAY_MINUTES=0.05) | ||
class MiddlewareAutoLogoutTests(ProtectedViewTestCase, TestCase): | ||
|
||
def test_user_auto_logged_out(self): | ||
self.login(username='regular.user') | ||
|
||
response_initial = self.client.get(reverse('ListReportingPeriods')) | ||
self.assertEqual(response_initial.status_code, 200) | ||
self.assertIn('tock_last_activity', response_initial.client.session) | ||
|
||
# Sleep for an arbirary five seconds | ||
time.sleep(5) | ||
|
||
response_after_expiry = self.client.get( | ||
reverse('ListReportingPeriods') | ||
) | ||
self.assertEqual(response_after_expiry.status_code, 302) | ||
self.assertIn( | ||
'tock_last_activity', | ||
response_after_expiry.client.session | ||
) |