first_boot: Require password confirmation

- Use existing UserCreationForm from auth module.

- Use cleaned data for login.
This commit is contained in:
Sunil Mohan Adapa 2015-10-13 23:20:31 +05:30 committed by James Valleroy
parent 62185a5960
commit 23eb40fce8
2 changed files with 13 additions and 28 deletions

View File

@ -19,48 +19,30 @@
Forms for first boot module. Forms for first boot module.
""" """
from django import forms from django.contrib import auth
from django.contrib import auth, messages from django.contrib import messages
from gettext import gettext as _ from gettext import gettext as _
from plinth import actions from plinth import actions
from plinth.errors import ActionError from plinth.errors import ActionError
from plinth.modules.config import config
from plinth.modules.users.forms import GROUP_CHOICES from plinth.modules.users.forms import GROUP_CHOICES
class State0Form(forms.ModelForm): class State0Form(auth.forms.UserCreationForm):
"""Firstboot state 0: create a new user.""" """Firstboot state 0: create a new user."""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request') self.request = kwargs.pop('request')
super(State0Form, self).__init__(*args, **kwargs) super(State0Form, self).__init__(*args, **kwargs)
class Meta:
model = auth.models.User
fields = ('username', 'password')
widgets = {
'password': forms.PasswordInput,
}
help_texts = {
'username':
_('Choose a username and password to access this web interface. '
'The password can be changed and other users can be added '
'later. An LDAP user with administrative privileges (sudo) is '
'also created.'),
}
def save(self, commit=True): def save(self, commit=True):
"""Create and log the user in.""" """Create and log the user in."""
user = super(State0Form, self).save(commit=False) user = super(State0Form, self).save(commit=commit)
user.set_password(self.cleaned_data['password'])
if commit: if commit:
user.save()
try: try:
actions.superuser_run( actions.superuser_run(
'ldap', 'ldap',
['create-user', user.get_username()], ['create-user', user.get_username()],
input=self.cleaned_data['password'].encode()) input=self.cleaned_data['password1'].encode())
except ActionError: except ActionError:
messages.error(self.request, messages.error(self.request,
_('Creating LDAP user failed.')) _('Creating LDAP user failed.'))
@ -80,15 +62,15 @@ class State0Form(forms.ModelForm):
admin_group = auth.models.Group.objects.get(name='admin') admin_group = auth.models.Group.objects.get(name='admin')
admin_group.user_set.add(user) admin_group.user_set.add(user)
self.login_user() self.login_user(self.cleaned_data['username'],
self.cleaned_data['password1'])
return user return user
def login_user(self): def login_user(self, username, password):
"""Try to login the user with the credentials provided""" """Try to login the user with the credentials provided"""
try: try:
user = auth.authenticate(username=self.request.POST['username'], user = auth.authenticate(username=username, password=password)
password=self.request.POST['password'])
auth.login(self.request, user) auth.login(self.request, user)
except Exception: except Exception:
pass pass

View File

@ -27,7 +27,10 @@
<p>It looks like this {{ cfg.box_name }} isn't set up yet. Provide basic <p>It looks like this {{ cfg.box_name }} isn't set up yet. Provide basic
data to get started.</p> data to get started.</p>
<br>
<p>Choose a username and password to access this web interface. The
password can be changed and other users can be added later. An
LDAP user with administrative privileges (sudo) is also created.</p>
<form class="form" method="post"> <form class="form" method="post">
{% csrf_token %} {% csrf_token %}