From 8ee7a9c8354a433b3a7096e88913b8889f29de4a Mon Sep 17 00:00:00 2001 From: vasileios Date: Fri, 24 Mar 2023 14:56:48 +0100 Subject: [PATCH] [#1027] Disabled applications dropdown in 2factor admin --- src/open_inwoner/conf/base.py | 3 +++ .../utils/django_two_factor_auth.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/open_inwoner/utils/django_two_factor_auth.py diff --git a/src/open_inwoner/conf/base.py b/src/open_inwoner/conf/base.py index a8fc580ff0..bb448075a9 100644 --- a/src/open_inwoner/conf/base.py +++ b/src/open_inwoner/conf/base.py @@ -1027,6 +1027,9 @@ # TWO_FACTOR_FORCE_OTP_ADMIN = config("TWO_FACTOR_FORCE_OTP_ADMIN", default=not DEBUG) TWO_FACTOR_PATCH_ADMIN = config("TWO_FACTOR_PATCH_ADMIN", default=True) +ADMIN_INDEX_DISPLAY_DROP_DOWN_MENU_CONDITION_FUNCTION = ( + "open_inwoner.utils.django_two_factor_auth.should_display_dropdown_menu" +) # file upload limits MIN_UPLOAD_SIZE = 1 # in bytes diff --git a/src/open_inwoner/utils/django_two_factor_auth.py b/src/open_inwoner/utils/django_two_factor_auth.py new file mode 100644 index 0000000000..1e3dc9688b --- /dev/null +++ b/src/open_inwoner/utils/django_two_factor_auth.py @@ -0,0 +1,19 @@ +from django.conf import settings + +from django_admin_index.utils import ( + should_display_dropdown_menu as default_should_display_dropdown_menu, +) + + +def should_display_dropdown_menu(request) -> bool: + default = default_should_display_dropdown_menu(request) + + two_factor_enabled = settings.TWO_FACTOR_PATCH_ADMIN + if not two_factor_enabled: + return default + + # never display the dropdown in two-factor admin views + if request.resolver_match.view_name.startswith("admin:two_factor:"): + return False + + return default and request.user.is_verified()