diff --git a/app/YtManagerApp/views/auth.py b/app/YtManagerApp/views/auth.py index ec668fe..cbea096 100644 --- a/app/YtManagerApp/views/auth.py +++ b/app/YtManagerApp/views/auth.py @@ -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') diff --git a/app/YtManagerApp/views/first_time.py b/app/YtManagerApp/views/first_time.py index 6e3ce0c..645653f 100644 --- a/app/YtManagerApp/views/first_time.py +++ b/app/YtManagerApp/views/first_time.py @@ -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): diff --git a/app/YtManagerApp/views/forms/auth.py b/app/YtManagerApp/views/forms/auth.py index c90c10f..6fa9148 100644 --- a/app/YtManagerApp/views/forms/auth.py +++ b/app/YtManagerApp/views/forms/auth.py @@ -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): diff --git a/app/YtManagerApp/views/forms/first_time.py b/app/YtManagerApp/views/forms/first_time.py index 6f4dfae..e710a4e 100644 --- a/app/YtManagerApp/views/forms/first_time.py +++ b/app/YtManagerApp/views/forms/first_time.py @@ -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('Skip') + HTML('Skip') ) ) @@ -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('Register new admin account') + ) + ) + + class PickAdminUserForm(forms.Form): admin_user = forms.ModelChoiceField( User.objects.order_by('username'),