]> git.parisson.com Git - django-unique-session.git/commitdiff
Added an optional whitelist
authorGael Le Mignot <gael@pilotsystems.net>
Fri, 15 Jun 2018 08:11:52 +0000 (10:11 +0200)
committerGael Le Mignot <gael@pilotsystems.net>
Fri, 15 Jun 2018 08:11:52 +0000 (10:11 +0200)
README.rst
unique_session/backends/session_backend.py

index 95581612e42b43e64cdbee0e4272b67170daa3e4..5535eb031f91841cf7992c575bd8288c66ff1b46 100644 (file)
@@ -20,7 +20,9 @@ How to use
 
 3. (Optional) Add ``"UNIQUE_SESSION_BLOCK_TIME = <seconds>"`` to enable the timeout.
 
-4. Run syncdb.
+4. (Optional) Add ``"UNIQUE_SESSION_WHITELIST = (<uid1>, <uid2>,...)"`` to whitelist some users (from their numeric id) so that they'll be exempt to the blocking mechanism.
+
+5. Run syncdb.
 
 License
 =======
index 08e9b1464b9df79e46520c9befa93c0aebe32ada..5fe2b00205104fbd7f60a3e872ed27012a0c4602 100644 (file)
@@ -14,6 +14,8 @@ import datetime
 TIME_DELTA = getattr(settings, 'UNIQUE_SESSION_BLOCK_TIME', None)
 TIME_DELTA = TIME_DELTA and datetime.timedelta(seconds = TIME_DELTA)
 
+WHITELIST = set(getattr(settings, 'UNIQUE_SESSION_WHITELIST', []))
+
 class SessionStore(SessionStoreBase):
     """
     Implements database session store.
@@ -56,7 +58,7 @@ class SessionStore(SessionStoreBase):
         sid = transaction.savepoint(using=using)
         try:
             # Also delete all other sessions of that user
-            if user_id:
+            if user_id and not user_id in WHITELIST:
                 exitsing = Session.objects.filter(user_id = user_id)
                 exitsing.exclude(session_key = obj.session_key).delete()
             obj.save(force_insert=must_create, using=using)
@@ -83,11 +85,12 @@ class SessionStore(SessionStoreBase):
         to log in as long as there is a session updated that number of
         seconds ago
         """
-        limit = timezone.now() + TIME_DELTA
-        s = Session.objects.filter(user_id = user.id,
-                                   updated_date__lt = limit)
-        if s.exists():
-            raise PermissionDenied
+        if not user.id in UNIQUE_SESSION_WHITELIST:
+            limit = timezone.now() + TIME_DELTA
+            s = Session.objects.filter(user_id = user.id,
+                                       updated_date__lt = limit)
+            if s.exists():
+                raise PermissionDenied
 
 if TIME_DELTA:
     user_logged_in.connect(SessionStore.ensure_unique_login)