From 9f45088c65ff9829371e0f4df23ab10b00a548d4 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 24 Nov 2026 15:16:10 -0800 Subject: [PATCH] users: Improve checks for creating initial user during firstboot Tests: - On a fresh development machine, first setup works. Admin account is created along with LDAP user the account. - After plinth.sqlite3 file is removed, first setup is triggered and it works. Admin account is created along with LDAP user the account. - When admin account is created without firstboot-wizard-secret, and the first wizard is waiting on the next step, visiting the user creation page does not work. Posting directly using the script also does not work. - When admin account is created without firstboot-wizard-secret, and the first wizard is waiting on the next step, remove the user_firstboot flag from the kvstore manually. Visiting the user creation page works. Posting to it does not work as admin user already exists. - When firstboot-wizard-secret file is present, first setup is triggered, skipping the secret is not possible via URL visit in browser and using a script that posts directly. Reviewed-by: James Valleroy --- freedombox/modules/first_boot/__init__.py | 6 ++++++ freedombox/modules/users/forms.py | 12 ++++++++++++ freedombox/modules/users/views.py | 10 ++++++++++ 3 files changed, 28 insertions(+) diff --git a/freedombox/modules/first_boot/__init__.py b/freedombox/modules/first_boot/__init__.py index ab072447a..4fa1b6e8a 100644 --- a/freedombox/modules/first_boot/__init__.py +++ b/freedombox/modules/first_boot/__init__.py @@ -146,6 +146,12 @@ def next_step_or_none(): return step.get('url') +def is_step_done(id: str) -> bool: + """Return whether a first boot step is done.""" + from freedombox import kvstore + return bool(kvstore.get_default(id, 0)) + + def mark_step_done(id): """Marks the status of a first boot step as done. diff --git a/freedombox/modules/users/forms.py b/freedombox/modules/users/forms.py index c7524daf2..317e0f6e6 100644 --- a/freedombox/modules/users/forms.py +++ b/freedombox/modules/users/forms.py @@ -445,6 +445,18 @@ class FirstBootForm(ValidNewUsernameCheckMixin, EmailFieldMixin, # field. self.fields['username'].widget.attrs.pop('autofocus', None) + def clean(self): + """Don't allow user account creation if an admin already exists.""" + cleaned_data = super().clean() + try: + admin_group = auth.models.Group.objects.get(name='admin') + if admin_group.user_set.exists(): + raise ValidationError('Administrator account already exists') + except auth.models.Group.DoesNotExist: + pass + + return cleaned_data + def save(self, commit=True): """Create and log the user in.""" user = super().save(commit=commit) diff --git a/freedombox/modules/users/views.py b/freedombox/modules/users/views.py index 09d5cafd7..847c9ef4c 100644 --- a/freedombox/modules/users/views.py +++ b/freedombox/modules/users/views.py @@ -261,6 +261,16 @@ class FirstBootView(django.views.generic.CreateView): def dispatch(self, request, *args, **kwargs): """Check if there is no possibility to create a new admin account.""" + # Don't allow calls if this step is already completed. + if first_boot.is_step_done('users_firstboot'): + return HttpResponseRedirect(reverse(first_boot.next_step())) + + # Don't allow calls unless the first wizard secret has been provided + # (but only if it exists in the first place.) + if (first_boot.firstboot_wizard_secret_exists() and + not request.session.get('firstboot_secret_provided', False)): + return HttpResponseRedirect(reverse('first_boot:welcome')) + if request.method == 'POST' and 'skip' in request.POST: first_boot.mark_step_done('users_firstboot') return HttpResponseRedirect(reverse(first_boot.next_step()))