Jan Dittberner
caab322beb
- add osusers.admin.UserCreationForm - add dummy osusers.tasks implementation with create_ldap_group and create_ldap_user - fix UserManager.get_next_username - add proper transaction handling in UserManager.create_user - add calls to create_ldap_user and create_ldap_group to UserManager.create_user
95 lines
2.4 KiB
Python
95 lines
2.4 KiB
Python
from django import forms
|
|
from django.utils.translation import ugettext as _
|
|
from django.contrib import admin
|
|
|
|
from .models import (
|
|
AdditionalGroup,
|
|
Group,
|
|
Shadow,
|
|
User,
|
|
)
|
|
|
|
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
|
|
|
|
|
|
class AdditionalGroupInline(admin.TabularInline):
|
|
model = AdditionalGroup
|
|
|
|
|
|
class ShadowInline(admin.TabularInline):
|
|
model = Shadow
|
|
readonly_fields = ['passwd']
|
|
can_delete = False
|
|
|
|
|
|
class UserCreationForm(forms.ModelForm):
|
|
"""
|
|
A form for creating system users.
|
|
|
|
"""
|
|
password1 = forms.CharField(label=_('Password'),
|
|
widget=forms.PasswordInput)
|
|
password2 = forms.CharField(label=_('Password (again)'),
|
|
widget=forms.PasswordInput)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = []
|
|
|
|
def clean_password2(self):
|
|
"""
|
|
Check that the two password entries match.
|
|
|
|
"""
|
|
password1 = self.cleaned_data.get('password1')
|
|
password2 = self.cleaned_data.get('password2')
|
|
if password1 and password2 and password1 != password2:
|
|
raise forms.ValidationError(PASSWORD_MISMATCH_ERROR)
|
|
return password2
|
|
|
|
def save(self, commit=True):
|
|
"""
|
|
Save the provided password in hashed format.
|
|
|
|
"""
|
|
user = User.objects.create_user(
|
|
password=self.cleaned_data['password1'], commit=commit)
|
|
return user
|
|
|
|
def save_m2m(self):
|
|
pass
|
|
|
|
|
|
class UserAdmin(admin.ModelAdmin):
|
|
inlines = [AdditionalGroupInline, ShadowInline]
|
|
readonly_fields = ['uid']
|
|
add_form = UserCreationForm
|
|
|
|
add_fieldsets = (
|
|
(None, {
|
|
'classes': ('wide',),
|
|
'fields': ('password1', 'password2')}),
|
|
)
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
"""
|
|
Use special form during user creation.
|
|
|
|
"""
|
|
defaults = {}
|
|
if obj is None:
|
|
defaults.update({
|
|
'form': self.add_form,
|
|
'fields': admin.util.flatten_fieldsets(self.add_fieldsets),
|
|
})
|
|
defaults.update(kwargs)
|
|
return super(UserAdmin, self).get_form(request, obj, **defaults)
|
|
|
|
def get_inline_instances(self, request, obj=None):
|
|
if obj is None:
|
|
return []
|
|
return super(UserAdmin, self).get_inline_instances(request, obj)
|
|
|
|
|
|
admin.site.register(Group)
|
|
admin.site.register(User, UserAdmin)
|