Restrict removal of last admin user

- Don't allow disabling the only available admin account.
- Don't allow deletion of the only available admin account.
- Don't allow removing admin privileges of the only available admin account.

Signed-off-by: Hemanth Kumar Veeranki <hems.india1997@gmail.com>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Hemanth Kumar Veeranki authored and Joseph Nuthalapati committed 2018-06-21 20:09:02 +05:30
1 parent b605c9da8a
commit b9963a45cd
6 files changed
+91 -7

No files matched your search

+12 -2
View File
@@ -22,6 +22,7 @@ from django.contrib import auth, messages
from django.contrib.auth.forms import SetPasswordForm, UserCreationForm
from django.contrib.auth.models import Group, User
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
@@ -33,6 +34,8 @@ from plinth.modules.security import set_restricted_access
from plinth.translation import set_language
from plinth.utils import is_user_admin
from . import get_last_admin_user
def get_group_choices():
"""Return localized group description and group name in one string."""
@@ -159,7 +162,7 @@ class UserUpdateForm(ValidNewUsernameCheckMixin,
fields = ('username', 'groups', 'ssh_keys', 'language', 'is_active')
model = User
widgets = {
'groups': forms.widgets.CheckboxSelectMultiple(),
'groups': plinth.forms.CheckboxSelectMultipleWithDisabled(),
}
def __init__(self, request, username, *args, **kwargs):
@@ -171,6 +174,7 @@ class UserUpdateForm(ValidNewUsernameCheckMixin,
self.request = request
self.username = username
super(UserUpdateForm, self).__init__(*args, **kwargs)
last_admin_user = get_last_admin_user()
choices = []
@@ -179,7 +183,10 @@ class UserUpdateForm(ValidNewUsernameCheckMixin,
# applications not installed yet.
if c[1] in group_choices:
# Replace group names with descriptions
choices.append((c[0], group_choices[c[1]]))
if c[1] == 'admin' and last_admin_user is not None:
choices.append((c[0], {'label': group_choices[c[1]], 'disabled': True}))
else:
choices.append((c[0], group_choices[c[1]]))
self.fields['groups'].label = _('Permissions')
self.fields['groups'].choices = choices
@@ -188,6 +195,9 @@ class UserUpdateForm(ValidNewUsernameCheckMixin,
self.fields['is_active'].widget = forms.HiddenInput()
self.fields['groups'].disabled = True
if last_admin_user and last_admin_user == self.username:
self.fields['is_active'].disabled = True
def save(self, commit=True):
"""Update LDAP user name and groups after saving user model."""
user = super(UserUpdateForm, self).save(commit=False)