mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
users: Avoid username conflicts with system users
When creating or renaming a user, check if the new username is in use by any system user.
This commit is contained in:
1 parent
ec68446eec
commit
3a69958165
2 files changed
+40
No files matched your search
@@ -22,6 +22,7 @@ Forms for first boot module.
|
||||
import json
|
||||
import logging
|
||||
import requests
|
||||
import subprocess
|
||||
|
||||
from django import forms
|
||||
from django.contrib import auth
|
||||
@@ -46,6 +47,18 @@ class State1Form(auth.forms.UserCreationForm):
|
||||
self.request = kwargs.pop('request')
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
"""Check for username collisions with system users."""
|
||||
username = self.cleaned_data['username']
|
||||
try:
|
||||
subprocess.run(['getent', 'passwd', username], check=True)
|
||||
# Exit code 0 means that the username is already in use.
|
||||
raise ValidationError(_('Username is reserved'))
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
|
||||
return super().clean()
|
||||
|
||||
def save(self, commit=True):
|
||||
"""Create and log the user in."""
|
||||
user = super().save(commit=commit)
|
||||
|
||||
Reference in new issue
Block a user