From e6cc3a71a0c41c40e95b47d7f001a7d948dba9fc Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 5 Nov 2015 15:31:56 +0530 Subject: Allow single user session active at any given time --- yaksh/middleware/one_session_per_user.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 yaksh/middleware/one_session_per_user.py (limited to 'yaksh/middleware/one_session_per_user.py') diff --git a/yaksh/middleware/one_session_per_user.py b/yaksh/middleware/one_session_per_user.py new file mode 100644 index 0000000..d9e97bb --- /dev/null +++ b/yaksh/middleware/one_session_per_user.py @@ -0,0 +1,22 @@ +from django.contrib.auth.models import User +from django.contrib.sessions.models import Session + +from yaksh.models import Visitor + + +class OneSessionPerUserMiddleware(object): + def process_request(self, request): + if isinstance(request.user, User): + current_key = request.session.session_key + if hasattr(request.user, 'visitor'): + active_key = request.user.visitor.session_key + print active_key, current_key + if active_key != current_key: + Session.objects.filter(session_key=active_key).delete() + request.user.visitor.session_key = current_key + request.user.visitor.save() + else: + Visitor.objects.create( + visitor_user=request.user, + session_key=current_key, + ) \ No newline at end of file -- cgit From 58bd6a8dbe0aa440ad8ec517fc85d388d02dc5e7 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 2 Dec 2015 18:30:12 +0530 Subject: Change model name associated with concurrent login --- yaksh/middleware/one_session_per_user.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'yaksh/middleware/one_session_per_user.py') diff --git a/yaksh/middleware/one_session_per_user.py b/yaksh/middleware/one_session_per_user.py index d9e97bb..f382652 100644 --- a/yaksh/middleware/one_session_per_user.py +++ b/yaksh/middleware/one_session_per_user.py @@ -1,22 +1,21 @@ from django.contrib.auth.models import User from django.contrib.sessions.models import Session -from yaksh.models import Visitor +from yaksh.models import ConcurrentUser class OneSessionPerUserMiddleware(object): def process_request(self, request): if isinstance(request.user, User): current_key = request.session.session_key - if hasattr(request.user, 'visitor'): - active_key = request.user.visitor.session_key - print active_key, current_key + if hasattr(request.user, 'concurrentuser'): + active_key = request.user.concurrentuser.session_key if active_key != current_key: Session.objects.filter(session_key=active_key).delete() - request.user.visitor.session_key = current_key - request.user.visitor.save() + request.user.concurrentuser.session_key = current_key + request.user.concurrentuser.save() else: - Visitor.objects.create( - visitor_user=request.user, + ConcurrentUser.objects.create( + concurrent_user=request.user, session_key=current_key, ) \ No newline at end of file -- cgit From 45163b0a4de5b685c5fd57dea6abd51d4c343a35 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Fri, 22 Jan 2016 17:49:23 +0530 Subject: Add docstring to middleware --- yaksh/middleware/one_session_per_user.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'yaksh/middleware/one_session_per_user.py') diff --git a/yaksh/middleware/one_session_per_user.py b/yaksh/middleware/one_session_per_user.py index f382652..92e888d 100644 --- a/yaksh/middleware/one_session_per_user.py +++ b/yaksh/middleware/one_session_per_user.py @@ -5,9 +5,23 @@ from yaksh.models import ConcurrentUser class OneSessionPerUserMiddleware(object): + """ + Middleware to handle multiple logins with same credentials + - Creates a Database entry to record the current user and active session key + - Checks if the current user has already been logged in. If True, the new session + key is stored with respect to the user and the old session key is deleted, + effectively terminating the older session for the same user. + - The concurrentuser attribute of the User model refers to the ConcurrentUser + model object and not the concurrent_user field due to behaviour described + in the Documentation + Link: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#extending-the-existing-user-model) + """ def process_request(self, request): if isinstance(request.user, User): current_key = request.session.session_key + # + # Documentation: + # https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#extending-the-existing-user-model if hasattr(request.user, 'concurrentuser'): active_key = request.user.concurrentuser.session_key if active_key != current_key: -- cgit