Fixed bug in first time setup wizard at login/user creation step. If an admin user already exists, the login page didn't work properly.

This commit is contained in:
Tiberiu Chibici 2019-01-02 23:11:39 +02:00
parent c7f740a4d1
commit 43583e0fca
4 changed files with 39 additions and 27 deletions

View File

@ -20,7 +20,7 @@ class RegisterView(FormView):
success_url = reverse_lazy('register_done')
def form_valid(self, form):
form.apply_session_expiry(self.request)
form.save()
username = form.cleaned_data.get('username')

View File

@ -11,8 +11,8 @@ from django.views.generic import FormView
from YtManagerApp.management.appconfig import appconfig
from YtManagerApp.management.jobs.synchronize import schedule_synchronize_global
from YtManagerApp.scheduler import initialize_scheduler
from YtManagerApp.views.forms.auth import ExtendedAuthenticationForm
from YtManagerApp.views.forms.first_time import WelcomeForm, ApiKeyForm, PickAdminUserForm, ServerConfigForm, DoneForm, UserCreationForm
from YtManagerApp.views.forms.first_time import WelcomeForm, ApiKeyForm, PickAdminUserForm, ServerConfigForm, DoneForm, \
UserCreationForm, LoginForm
logger = logging.getLogger("FirstTimeWizard")
@ -85,38 +85,35 @@ class Step2SetupAdminUserView(WizardStepMixin, FormView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__form_class = UserCreationForm
def get_form_class(self):
return self.__form_class
def get(self, request, *args, **kwargs):
have_users = User.objects.count() > 0
have_admin = User.objects.filter(is_superuser=True).count() > 0
# Check if an admin user already exists
if have_admin:
logger.debug("Admin user already exists and is not logged in!")
return LoginForm
elif have_users and 'register' not in self.kwargs:
logger.debug("There are users but no admin!")
return PickAdminUserForm
logger.debug("No admin user exists, will register a new account!")
return UserCreationForm
def get(self, request, *args, **kwargs):
# Skip if admin is already logged in
if request.user.is_authenticated and request.user.is_superuser:
logger.debug("Admin user already exists and is logged in!")
return redirect(self.success_url)
# Check if an admin user already exists
elif have_admin:
logger.debug("Admin user already exists and is not logged in!")
self.__form_class = ExtendedAuthenticationForm
elif have_users and 'register' not in kwargs:
logger.debug("There are users but no admin!")
self.__form_class = PickAdminUserForm
else:
logger.debug("No admin user exists, will register a new account!")
self.__form_class = UserCreationForm
return super().get(request, *args, **kwargs)
def form_valid(self, form):
if isinstance(form, ExtendedAuthenticationForm):
if isinstance(form, LoginForm):
form.apply_session_expiry(self.request)
login(self.request, form.get_user())
elif isinstance(form, UserCreationForm):

View File

@ -8,15 +8,14 @@ from django.urls import reverse_lazy
class ExtendedAuthenticationForm(AuthenticationForm):
remember_me = forms.BooleanField(label='Remember me', required=False, initial=False)
def clean(self):
def apply_session_expiry(self, request):
remember_me = self.cleaned_data.get('remember_me')
if remember_me:
expiry = 3600 * 24 * 30
else:
expiry = 0
self.request.session.set_expiry(expiry)
return super().clean()
request.session.set_expiry(expiry)
class ExtendedUserCreationForm(UserCreationForm):

View File

@ -6,7 +6,7 @@ from django import forms
from django.contrib.auth.models import User
from django.urls import reverse_lazy
from YtManagerApp.views.forms.auth import ExtendedUserCreationForm
from YtManagerApp.views.forms.auth import ExtendedUserCreationForm, ExtendedAuthenticationForm
logger = logging.getLogger("FirstTimeWizard")
@ -30,7 +30,7 @@ class ApiKeyForm(forms.Form):
'api_key',
Column(
Submit('submit', value='Continue'),
HTML('<a href="{% url \'first_time_2\' %}" class="btn">Skip</a>')
HTML('<a href="{% url \'first_time_2\' %}" class="btn btn-secondary">Skip</a>')
)
)
@ -39,6 +39,22 @@ class UserCreationForm(ExtendedUserCreationForm):
form_action = reverse_lazy('first_time_2')
class LoginForm(ExtendedAuthenticationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
'username',
'password',
'remember_me',
Column(
Submit('submit', value='Continue'),
HTML('<a href="{% url \'first_time_2\' %}?register=1" class="btn">Register new admin account</a>')
)
)
class PickAdminUserForm(forms.Form):
admin_user = forms.ModelChoiceField(
User.objects.order_by('username'),