diff options
| author | Bobby <[email protected]> | 2023-02-15 01:47:01 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-02-15 01:47:01 -0500 |
| commit | 3fdb02de09122e18553634f9c01bbbcb63a1f90c (patch) | |
| tree | 84c0c78f6246b1c1136b9b82a8c07f30f43fe1fb | |
| parent | 7ddfd6181c738423d3e697498191288dc34fb49d (diff) | |
| download | thatcomputerscientist-3fdb02de09122e18553634f9c01bbbcb63a1f90c.tar.xz thatcomputerscientist-3fdb02de09122e18553634f9c01bbbcb63a1f90c.zip | |
case insensitive usernames
| -rw-r--r-- | blog/views.py | 2 | ||||
| -rw-r--r-- | thatcomputerscientist/backends.py | 21 | ||||
| -rw-r--r-- | thatcomputerscientist/settings.py | 1 |
3 files changed, 23 insertions, 1 deletions
diff --git a/blog/views.py b/blog/views.py index cbdd95d6..dfd47045 100644 --- a/blog/views.py +++ b/blog/views.py @@ -273,7 +273,7 @@ def articles(request, date=None, cg=None): def user_activity(request, username): try: - user = User.objects.get(username=username) + user = User.objects.get(username__iexact=username) user_profile = UserProfile.objects.get(user=user) if user_profile.is_public or user == request.user: recent_comments = Comment.objects.filter(user=user).order_by('-created_at')[:5] diff --git a/thatcomputerscientist/backends.py b/thatcomputerscientist/backends.py new file mode 100644 index 00000000..09867862 --- /dev/null +++ b/thatcomputerscientist/backends.py @@ -0,0 +1,21 @@ +from django.contrib.auth.backends import ModelBackend +from django.contrib.auth import get_user_model + +class CaseInsensitiveModelBackend(ModelBackend): + def authenticate(self, request, username=None, password=None, **kwargs): + UserModel = get_user_model() + try: + user = UserModel.objects.get(username__iexact=username) + except UserModel.DoesNotExist: + return None + else: + if user.check_password(password): + return user + return None + + def get_user(self, user_id): + UserModel = get_user_model() + try: + return UserModel.objects.get(pk=user_id) + except UserModel.DoesNotExist: + return None diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py index 1524a633..33c2186d 100644 --- a/thatcomputerscientist/settings.py +++ b/thatcomputerscientist/settings.py @@ -79,6 +79,7 @@ CONFIGURED_SUBDOMAINS = { ROOT_URLCONF = 'thatcomputerscientist.urls' +AUTHENTICATION_BACKENDS = ['thatcomputerscientist.backends.CaseInsensitiveModelBackend'] TEMPLATES = [ { |
