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:
James Valleroy authored and Sunil Mohan Adapa committed 2016-08-12 18:47:52 +05:30
1 parent ec68446eec
commit 3a69958165
2 files changed
+40

No files matched your search

+13
View File
@@ -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)