mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
users: Handle errors better during POSIX user operations
- If during an action, user does not exist ignore. - If during an action, return a non-zero exist status. - Catch an errors during actions as exceptions. - Display a message that corresponding POSIX operation failed.
This commit is contained in:
1 parent
d27cd02193
commit
a3de3ae277
9 files changed
+114
-35
No files matched your search
@@ -16,29 +16,42 @@
|
||||
#
|
||||
|
||||
from django import forms
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm, SetPasswordForm
|
||||
from gettext import gettext as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth.errors import ActionError
|
||||
|
||||
|
||||
class CreateUserForm(UserCreationForm):
|
||||
"""Custom user create form with option to also create POSIX user."""
|
||||
|
||||
add_posix_user = forms.BooleanField(
|
||||
label=_('Also create POSIX user'),
|
||||
label=_('Also create a POSIX system user'),
|
||||
required=False,
|
||||
help_text=_('This will allow the new user to log in to the system '
|
||||
'through SSH. The new user will also have administrative '
|
||||
'privileges (sudo).'))
|
||||
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
"""Initialize the form with extra request argument."""
|
||||
self.request = request
|
||||
super(CreateUserForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, commit=True):
|
||||
"""Save the user model and create POSIX user if required."""
|
||||
user = super(CreateUserForm, self).save(commit)
|
||||
if commit and self.cleaned_data['add_posix_user']:
|
||||
actions.superuser_run(
|
||||
'create-user',
|
||||
[user.username, self.cleaned_data['password1']])
|
||||
try:
|
||||
actions.superuser_run(
|
||||
'create-user',
|
||||
[user.get_username(), self.cleaned_data['password1']])
|
||||
except ActionError:
|
||||
messages.error(self.request,
|
||||
_('Creating POSIX system user failed.'))
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@@ -46,29 +59,57 @@ class UserUpdateForm(forms.ModelForm):
|
||||
"""When user is enabled/disabled, also enables/disables the POSIX user."""
|
||||
|
||||
class Meta:
|
||||
"""Metadata to control automatic form building."""
|
||||
fields = ('username', 'groups', 'is_active')
|
||||
model = User
|
||||
widgets = {
|
||||
'groups': forms.widgets.CheckboxSelectMultiple(),
|
||||
}
|
||||
|
||||
def __init__(self, request, username, *args, **kwargs):
|
||||
"""Initialize the form with extra request argument."""
|
||||
self.request = request
|
||||
self.username = username
|
||||
super(UserUpdateForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, commit=True):
|
||||
"""Enable/disable POSIX user after saving user model."""
|
||||
user = super(UserUpdateForm, self).save(commit)
|
||||
|
||||
if commit:
|
||||
if user.is_active:
|
||||
actions.superuser_run('enable-user', [user.username])
|
||||
else:
|
||||
actions.superuser_run('disable-user', [user.username])
|
||||
try:
|
||||
if user.is_active:
|
||||
actions.superuser_run('enable-user', [user.get_username()])
|
||||
else:
|
||||
actions.superuser_run('disable-user',
|
||||
[user.get_username()])
|
||||
except ActionError:
|
||||
messages.error(
|
||||
self.request,
|
||||
_('Setting active status for POSIX system user failed.'))
|
||||
|
||||
return user
|
||||
|
||||
|
||||
class UserChangePasswordForm(SetPasswordForm):
|
||||
"""Custom form that also updates password for POSIX users."""
|
||||
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
"""Initialize the form with extra request argument."""
|
||||
self.request = request
|
||||
super(UserChangePasswordForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, commit=True):
|
||||
"""Save the user model and change POSIX password as well."""
|
||||
user = super(UserChangePasswordForm, self).save(commit)
|
||||
if commit:
|
||||
actions.superuser_run(
|
||||
'change-user-password',
|
||||
[user.username, self.cleaned_data['new_password1']])
|
||||
try:
|
||||
actions.superuser_run(
|
||||
'change-user-password',
|
||||
[user.get_username(), self.cleaned_data['new_password1']])
|
||||
except ActionError:
|
||||
messages.error(
|
||||
self.request,
|
||||
_('Changing POSIX system user password failed.'))
|
||||
|
||||
return user
|
||||
Reference in new issue
Block a user