Skip to content

Commit

Permalink
Merge pull request #332 from elixir-luxembourg/nicer-user-admin
Browse files Browse the repository at this point in the history
feat: small improvements for managing users in django-admin
  • Loading branch information
jLebioda authored Mar 8, 2022
2 parents 89fc6ed + ddebf43 commit e540968
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
3 changes: 3 additions & 0 deletions elixir_daisy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@
# ID service
IDSERVICE_FUNCTION = 'web.views.utils.generate_elu_accession'

# Should the superuser be able to change the passwords in django-admin
ENABLE_PASSWORD_CHANGE_IN_ADMIN = False

# Import local settings to override those values based on the deployment environment
try:
from .settings_local import *
Expand Down
47 changes: 31 additions & 16 deletions web/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
Expand Down Expand Up @@ -111,37 +112,51 @@ def save(self, commit=True):
return user


user_change_form_fields = (
'username', 'email', 'password', 'is_active',
'source', 'first_name', 'last_name', 'full_name',
'is_staff', 'is_superuser', 'groups',
'user_permissions', 'date_joined', 'last_login'
)
user_admin_fieldset_row = (None, {'fields': ('username', 'email', 'password', 'is_active', 'source')}, )

if getattr(settings, 'ENABLE_PASSWORD_CHANGE_IN_ADMIN', False):
user_change_form_fields = user_change_form_fields + ('change_password', )
user_admin_fieldset_row = (None, {'fields': ('username', 'email', 'password', 'change_password', 'is_active', 'source')}, )


class UserChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = ReadOnlyPasswordHashField(help_text='This field contains hashed and salted value')
#change_password = forms.CharField(label='Set new password:',
# help_text='Leave empty if no change is needed',
# widget=forms.PasswordInput)


if getattr(settings, 'ENABLE_PASSWORD_CHANGE_IN_ADMIN', False):
change_password = forms.CharField(
label='Set new password:',
help_text='Leave empty if no change is needed',
required=False,
widget=forms.PasswordInput
)

class Meta:
model = User
fields = (
'username', 'email', 'password', 'is_active',
'source', 'first_name', 'last_name', 'full_name',
'is_staff', 'is_superuser', 'groups',
'user_permissions', 'date_joined', 'last_login'
)
fields = user_change_form_fields

def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
print(self.initial["password"])
return self.initial["password"]

def save(self, commit=True):
user = super(UserChangeForm, self).save(commit=False)

# if len(self.cleaned_data["change_password"]):
# user.set_password(self.cleaned_data["change_password"])
if getattr(settings, 'ENABLE_PASSWORD_CHANGE_IN_ADMIN', False):
if len(self.cleaned_data["change_password"]):
user.set_password(self.cleaned_data["change_password"])

if commit:
user.save()
Expand All @@ -154,11 +169,11 @@ class UserAdmin(BaseUserAdmin):
add_form = UserCreationForm # Form to add new user

# The fields to be used in displaying the User model in `/admin/core/user/`
list_display = ('email', 'full_name', 'source', 'is_staff', 'is_superuser')
list_display = ('id', 'full_name', 'email', 'source', 'is_staff', 'is_superuser', 'oidc_id')

# Sections in the Edit page
fieldsets = (
(None, {'fields': ('username', 'email', 'password', 'is_active', 'source')}),
user_admin_fieldset_row,
('Personal info', {'fields': ('first_name', 'last_name', 'full_name', 'oidc_id')}),
('Permissions', {'fields': ('is_staff', 'is_superuser', 'groups', 'user_permissions')}),
('Additional metdata', {'fields': ('date_joined', 'last_login', 'api_key')}),
Expand All @@ -173,8 +188,8 @@ class UserAdmin(BaseUserAdmin):
),
)

search_fields = ('email',)
ordering = ('email',)
search_fields = ('full_name', 'email',)
ordering = ('full_name',)
filter_horizontal = ()

# User
Expand Down
1 change: 1 addition & 0 deletions web/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
{% endif %}
{% if request.user.is_superuser %}
<a class="dropdown-item" href="{% url 'users' %}">manage users</a>
<a class="dropdown-item" href="{% url 'admin:index' %}">django-admin</a>
{% endif %}
{% if request.user.source.name == 'MANUAL' %}
<a class="dropdown-item" href="{% url 'users_change_password'%}">change password</a>
Expand Down

0 comments on commit e540968

Please sign in to comment.