From d24081e4323c78ea867a6f6439f5ec6e026a43dd Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 18 Nov 2015 11:58:19 +0000 Subject: [PATCH] Set WAGTAIL_PASSWORD_RESET_ENABLED to default to the value of WAGTAIL_PASSWORD_MANAGEMENT_ENABLED --- docs/advanced_topics/settings.rst | 4 ++-- wagtail/wagtailadmin/views/account.py | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/advanced_topics/settings.rst b/docs/advanced_topics/settings.rst index 148429a15..82097546d 100644 --- a/docs/advanced_topics/settings.rst +++ b/docs/advanced_topics/settings.rst @@ -215,13 +215,13 @@ Password Management WAGTAIL_PASSWORD_MANAGEMENT_ENABLED = True -This allows users to change their passwords. +This specifies whether users are allowed to change their passwords (enabled by default). .. code-block:: python WAGTAIL_PASSWORD_RESET_ENABLED = True -This allows users to reset their passwords. +This specifies whether users are allowed to reset their passwords. Defaults to the same as ``WAGTAIL_PASSWORD_MANAGEMENT_ENABLED``. diff --git a/wagtail/wagtailadmin/views/account.py b/wagtail/wagtailadmin/views/account.py index 020b4fe9f..34cad13b3 100644 --- a/wagtail/wagtailadmin/views/account.py +++ b/wagtail/wagtailadmin/views/account.py @@ -16,18 +16,32 @@ from wagtail.wagtailusers.models import UserProfile from wagtail.wagtailcore.models import UserPagePermissionsProxy +# Helper functions to check password management settings to enable/disable views as appropriate. +# These are functions rather than class-level constants so that they can be overridden in tests +# by override_settings + +def password_management_enabled(): + return getattr(settings, 'WAGTAIL_PASSWORD_MANAGEMENT_ENABLED', True) + + +def password_reset_enabled(): + return getattr(settings, 'WAGTAIL_PASSWORD_RESET_ENABLED', password_management_enabled()) + + +# Views + def account(request): user_perms = UserPagePermissionsProxy(request.user) show_notification_preferences = user_perms.can_edit_pages() or user_perms.can_publish_pages() return render(request, 'wagtailadmin/account/account.html', { - 'show_change_password': getattr(settings, 'WAGTAIL_PASSWORD_MANAGEMENT_ENABLED', True) and request.user.has_usable_password(), + 'show_change_password': password_management_enabled() and request.user.has_usable_password(), 'show_notification_preferences': show_notification_preferences }) def change_password(request): - if not getattr(settings, 'WAGTAIL_PASSWORD_MANAGEMENT_ENABLED', True): + if not password_management_enabled(): raise Http404 can_change_password = request.user.has_usable_password() @@ -56,7 +70,7 @@ def change_password(request): def _wrap_password_reset_view(view_func): @wraps(view_func) def wrapper(*args, **kwargs): - if not getattr(settings, 'WAGTAIL_PASSWORD_RESET_ENABLED', True): + if not password_reset_enabled(): raise Http404 return view_func(*args, **kwargs) return wrapper @@ -99,7 +113,7 @@ def login(request): template_name='wagtailadmin/login.html', authentication_form=forms.LoginForm, extra_context={ - 'show_password_reset': getattr(settings, 'WAGTAIL_PASSWORD_RESET_ENABLED', True), + 'show_password_reset': password_reset_enabled(), 'username_field': get_user_model().USERNAME_FIELD, }, )