From a0503af5ef5c269f8360f753865e9b2f547d0537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20G=C3=B6tz?= Date: Wed, 24 Apr 2019 11:24:39 +0200 Subject: [PATCH] Fix password reset, Fix docker env --- src/account_manager/forms.py | 23 +++++++++++++++++++++++ src/account_manager/views/user_views.py | 2 ++ src/core/docker_settings.py | 6 +++--- src/core/urls.py | 6 ++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/account_manager/forms.py b/src/account_manager/forms.py index 74a9df1..960188a 100644 --- a/src/account_manager/forms.py +++ b/src/account_manager/forms.py @@ -1,7 +1,12 @@ from django import forms +from django.contrib.auth import get_user_model +from django.contrib.auth.forms import PasswordResetForm from .models import LdapUser, LdapGroup from django.forms import modelformset_factory +import logging + +logger = logging.getLogger(__name__) class AddLDAPUserForm(forms.Form): @@ -67,3 +72,21 @@ UserFormset = modelformset_factory( fields=('dn',), extra=1 ) + +UserModel = get_user_model() + + +class LdapPasswordResetForm(PasswordResetForm): + def get_users(self, email): + """Given an email, return matching user(s) who should receive a reset. + This allows subclasses to more easily customize the default policies + that prevent inactive users and users with unusable passwords from + resetting their password. + """ + logger.debug('Pasword reset get users') + active_users = UserModel._default_manager.filter(**{ + '%s__iexact' % UserModel.get_email_field_name(): email, + 'is_active': True, + }) + logger.debug((u for u in active_users)) + return (u for u in active_users) diff --git a/src/account_manager/views/user_views.py b/src/account_manager/views/user_views.py index 2519d26..7846802 100644 --- a/src/account_manager/views/user_views.py +++ b/src/account_manager/views/user_views.py @@ -411,3 +411,5 @@ class LdapPasswordChangeView(PasswordChangeView): LdapUser.base_dn = LdapUser.ROOT_DN LdapUser.password_reset(user, password) return super().form_valid(form) + + diff --git a/src/core/docker_settings.py b/src/core/docker_settings.py index 84acdfc..d0ec864 100644 --- a/src/core/docker_settings.py +++ b/src/core/docker_settings.py @@ -18,7 +18,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DOMAIN = os.environ['DOMAIN'] SITE_NAME = os.environ['SITE_NAME'] SECRET_KEY = os.environ['SECRET_KEY'] -DEBUG = os.environ.get('DEBUG', 'False') =='TRUE' +DEBUG = os.environ.get('DEBUG', 'False') =='True' ALLOWED_HOSTS = os.environ['ALLOWED_HOSTS'].split() # Application definition @@ -176,8 +176,8 @@ else: EMAIL_TIMEOUT = 15 EMAIL_HOST = os.environ['EMAIL_HOST'] EMAIL_PORT = int(os.environ['EMAIL_PORT']) - EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS', 'False') == 'TRUE' -# EMAIL_USE_SSL = os.environ.get('EMAIL_USE_SSL', 'False') == 'TRUE' + EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS', 'False') == 'True' + EMAIL_USE_SSL = os.environ.get('EMAIL_USE_SSL', 'False') == 'True' DEFAULT_FROM_EMAIL = os.environ['DEFAULT_FROM_EMAIL'] SERVER_EMAIL = os.environ['SERVER_EMAIL'] diff --git a/src/core/urls.py b/src/core/urls.py index e10bcaf..3c27ba8 100644 --- a/src/core/urls.py +++ b/src/core/urls.py @@ -17,6 +17,7 @@ from django.contrib import admin from django.urls import path, include from django.contrib.auth import views as auth_views from django.contrib.auth.decorators import user_passes_test +from account_manager.forms import LdapPasswordResetForm from .views import about login_forbidden = user_passes_test(lambda u: u.is_anonymous(), '/') @@ -24,10 +25,11 @@ login_forbidden = user_passes_test(lambda u: u.is_anonymous(), '/') urlpatterns = [ path('', include('account_manager.urls')), path('admin/', admin.site.urls), - path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'), path('about/', about, name='about'), + path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'), path('accounts/password_reset/', - auth_views.PasswordResetView.as_view(html_email_template_name='registration/password_reset_email.html'), + auth_views.PasswordResetView.as_view(html_email_template_name='registration/password_reset_email.html', + form_class=LdapPasswordResetForm), name='password_reset'), path('accounts/', include('django.contrib.auth.urls')), ]