Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setting to add a message after logout #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion session_security/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import django
from django.contrib.auth import logout
from django.contrib import messages
try: # Django 2.0
from django.urls import reverse, resolve, Resolver404
except: # Django < 2.0
Expand All @@ -25,7 +26,7 @@
MiddlewareMixin = object

from .utils import get_last_activity, set_last_activity
from .settings import EXPIRE_AFTER, PASSIVE_URLS, PASSIVE_URL_NAMES
from .settings import EXPIRE_AFTER, PASSIVE_URLS, PASSIVE_URL_NAMES, LOGOUT_MESSAGE


class SessionSecurityMiddleware(MiddlewareMixin):
Expand Down Expand Up @@ -72,6 +73,8 @@ def process_request(self, request):
delta = now - get_last_activity(request.session)
expire_seconds = self.get_expire_seconds(request)
if delta >= timedelta(seconds=expire_seconds):
if LOGOUT_MESSAGE:
messages.add_message(request, messages.WARNING, LOGOUT_MESSAGE)
logout(request)
elif (request.path == reverse('session_security_ping') and
'idleFor' in request.GET):
Expand Down
7 changes: 7 additions & 0 deletions session_security/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
thus cannot be described statically. NOTE: currently namespaces are not
handled. Overridable in ``settings.SESSION_SECURITY_PASSIVE_URL_NAMES``.

LOGOUT_MESSAGE
Message that will be shown after user has been logged out. Works using Django
messages framework. Default is ``None``, so no message will be added to the
request. Overridable in ``settings.SESSION_SECURITY_LOGOUT_MESSAGE``.

SESSION_SECURITY_INSECURE
Set this to True in your settings if you want the project to run without
having to set SESSION_EXPIRE_AT_BROWSER_CLOSE=True, which you should
Expand All @@ -40,6 +45,8 @@

PASSIVE_URL_NAMES = getattr(settings, 'SESSION_SECURITY_PASSIVE_URL_NAMES', [])

LOGOUT_MESSAGE = getattr(settings, 'SESSION_SECURITY_LOGOUT_MESSAGE', None)

expire_at_browser_close = getattr(
settings,
'SESSION_EXPIRE_AT_BROWSER_CLOSE',
Expand Down