About a month ago, I built a centralized authentication backend using django-roa for my company, AUF. It’s actually a cascading authentication system, since I copy locally (in Django’s own authentication system) the account, once validated.
That was working just fine, until I tried adding some of my users’ local accounts in different groups. The distant usernames follow the format <first_name>.<last_name>, and since Django doesn’t allow that, everytime I would try to save the user edit form in the admin interface, I would get an error message saying that the username format was invalid.
After looking around a bit, I found this solution, which has been working great for me.
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
class UserCreationForm(UserCreationForm):
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r"^[\w'\.\-]+\s?[\w'\.\-]+$")
class UserChangeForm(UserChangeForm):
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r"^[\w'\.\-]+\s?[\w'\.\-]+$")
class UserProfileAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreationForm
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)
Subscribe
Leave a Reply