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 <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2026-11-28 08:31:51 -05:00
1 parent 22124cd1db
commit 9f45088c65
3 files changed
+28

No files matched your search

@@ -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.
+12
View File
@@ -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)
+10
View File
@@ -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()))