diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-04-04 12:19:52 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-04-04 12:19:52 +0200 |
commit | ef2b079d276a2e485383ce0e1a187d882ae5c1dc (patch) | |
tree | 86520f0d7c926f256aab6504900c6256409f9f5f /ishtar_common/context_processors.py | |
parent | 0dac9c341be56b382fb6d5b918997aa2f41995ad (diff) | |
download | Ishtar-ef2b079d276a2e485383ce0e1a187d882ae5c1dc.tar.bz2 Ishtar-ef2b079d276a2e485383ce0e1a187d882ae5c1dc.zip |
Manage expiration of passwords
Diffstat (limited to 'ishtar_common/context_processors.py')
-rw-r--r-- | ishtar_common/context_processors.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/ishtar_common/context_processors.py b/ishtar_common/context_processors.py index e1754e935..c1d1224ea 100644 --- a/ishtar_common/context_processors.py +++ b/ishtar_common/context_processors.py @@ -17,8 +17,13 @@ # See the file COPYING for details. +import datetime + from django.conf import settings +from django.core.cache import cache from django.contrib.sites.models import Site +from django.urls import reverse +from django.utils.translation import ugettext_lazy as _ from ishtar_common.version import __version__ from ishtar_common.models import get_current_profile @@ -41,13 +46,6 @@ def get_base_context(request): except Site.DoesNotExist: dct["APP_NAME"] = settings.APP_NAME dct["COUNTRY"] = settings.COUNTRY - """ - if 'MENU' not in request.session or \ - request.session['MENU'].user != request.user: - menu = Menu(request.user) - menu.init() - request.session['MENU'] = menu - """ # menu is now in cache - put it back in session later? current_action = None if "CURRENT_ACTION" in request.session: @@ -71,6 +69,30 @@ def get_base_context(request): menu.init() if hasattr(request.user, "ishtaruser") and request.user.ishtaruser: + + # check password expiration date + if settings.ISHTAR_PASSWORD_EXPIRATION_DAYS and \ + isinstance(settings.ISHTAR_PASSWORD_EXPIRATION_DAYS, int): + key = f"{settings.PROJECT_SLUG}-password_expired-{request.user.pk}" + password_expired = cache.get(key) + if password_expired is None: + password_expired = True + d = datetime.date.today() - request.user.ishtaruser.password_last_update + if d.days < settings.ISHTAR_PASSWORD_EXPIRATION_DAYS: + password_expired = False + cache.set(key, password_expired, settings.CACHE_TIMEOUT) + if password_expired and not request.path.endswith("password_change/"): + msg = str(_("Your password has expired. Please update it using this " + "form.")) + msg = msg.replace( + str(_("form")), + f'<a href="{reverse("password_change")}">' + f'<i class="fa fa-external-link" aria-hidden="true"></i> ' + f'{_("form")}</a>' + ) + dct["MESSAGES"].append((msg, "warning")) + + # external sources if ( request.user.ishtaruser.current_profile and "EXTERNAL_SOURCES" not in request.session |