From e9ee3977f422d9b7c18a843bc8faaf392796ba75 Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Sat, 31 Dec 2016 14:56:14 +0100 Subject: [PATCH 1/8] Update middleware to support 1.10 style MIDDLEWARE Middleware has changed in django 1.10, see https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware This mixin is just "band aid" --- useraudit/middleware.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/useraudit/middleware.py b/useraudit/middleware.py index 83c8e2a..8588a1d 100644 --- a/useraudit/middleware.py +++ b/useraudit/middleware.py @@ -1,11 +1,12 @@ import threading +from django.utils.deprecation import MiddlewareMixin thread_data = threading.local() def get_request(): return getattr(thread_data, 'request', None) -class RequestToThreadLocalMiddleware(object): +class RequestToThreadLocalMiddleware(MiddlewareMixin): def process_request(self, request): thread_data.request = request From c3758545d27d52612a88c74b9c50718832fd0703 Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Tue, 3 Jan 2017 10:08:49 +0100 Subject: [PATCH 2/8] Fix bug in authenticate It happens that if USERNAME_FIELD is not 'username' like it usually isn't for custom user models, then self.username is None, leading to empty fields for username. --- useraudit/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/useraudit/backend.py b/useraudit/backend.py index 1ccd50a..07ccaeb 100644 --- a/useraudit/backend.py +++ b/useraudit/backend.py @@ -34,7 +34,7 @@ def __init__(self): def authenticate(self, **credentials): UserModel = get_user_model() - self.username = credentials.get(get_user_model().USERNAME_FIELD) + self.username = credentials.get('username') self.login_logger.log_failed_login(self.username, get_request()) self.login_attempt_logger.increment(self.username) self.block_user_if_needed() From 018852c8fb5805f2ef51ce9781891071fa59c86e Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Tue, 3 Jan 2017 10:22:19 +0100 Subject: [PATCH 3/8] Fixed AttributeError when using custom user Getting the "username" should be done with get_username() otherwise an AttributeError is raised on custom user models with custom USERNAME_FIELD --- useraudit/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/useraudit/backend.py b/useraudit/backend.py index 07ccaeb..40740f4 100644 --- a/useraudit/backend.py +++ b/useraudit/backend.py @@ -22,7 +22,7 @@ def user_pre_save(sender, instance=None, raw=False, **kwargs): # that the user isn't inactivated on next login by the AuthFailedLoggerBackend current_user = sender.objects.get(pk=user.pk) if not current_user.is_active and user.is_active: - LoginAttemptLogger().reset(user.username) + LoginAttemptLogger().reset(user.get_username()) class AuthFailedLoggerBackend(object): From 53ca688b5372d05fe12b8b67d4af76c6c6284f96 Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Tue, 3 Jan 2017 10:26:19 +0100 Subject: [PATCH 4/8] Note to add useraudit to project urls file Note to add useraudit to project urls file --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index e257a4d..cbac365 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,14 @@ You will run `migrate` to create/migrate the useraudit DB tables. Ex: ``` $ ./manage.py migrate useraudit ``` +Then add useraudit to your project url for the activate link to work in django admin. + +``` +... +url(r'^useraudit/', include('useraudit.urls')), +... +``` + ### Model changes for password expiration From 7656ededeb747fb9ac75e64fb7e4c87ede31d4ad Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Fri, 6 Jan 2017 15:04:18 +0100 Subject: [PATCH 5/8] Fixed issue #1 Django complains when a naive datetime is saved into a DateTime model field. Fix to issue https://github.com/dedayoa/django-useraudit/issues/1 --- useraudit/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/useraudit/models.py b/useraudit/models.py index b2b4440..39d0fd3 100644 --- a/useraudit/models.py +++ b/useraudit/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils import datetime from django.contrib.auth.signals import user_logged_in import datetime @@ -14,14 +15,14 @@ class LoginAttemptLogger(object): def reset(self, username): defaults = { 'count': 0, - 'timestamp': datetime.datetime.now() + 'timestamp': timezone.now() } LoginAttempt.objects.update_or_create(username=username, defaults=defaults) def increment(self, username): obj, created = LoginAttempt.objects.get_or_create(username=username) obj.count += 1 - obj.timestamp = datetime.datetime.now() + obj.timestamp = timezone.now() obj.save() From 6d018a6962600476b252503f023e0a7df2e972af Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Fri, 24 Feb 2017 09:27:58 +0100 Subject: [PATCH 6/8] Update models.py import from django.utils should have been timezone, not datetime --- useraudit/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/useraudit/models.py b/useraudit/models.py index 39d0fd3..58e0c10 100644 --- a/useraudit/models.py +++ b/useraudit/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.utils import datetime +from django.utils import timezone from django.contrib.auth.signals import user_logged_in import datetime From 73fdb6ae1123533e66172d934b24a97da0faa071 Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Sun, 26 Feb 2017 14:59:09 +0100 Subject: [PATCH 7/8] Update middleware.py Make 1.10 compatible without breaking pre 1.10 --- useraudit/middleware.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/useraudit/middleware.py b/useraudit/middleware.py index 8588a1d..3e7d81f 100644 --- a/useraudit/middleware.py +++ b/useraudit/middleware.py @@ -1,5 +1,10 @@ import threading -from django.utils.deprecation import MiddlewareMixin +try: + from django.utils.deprecation import MiddlewareMixin +except ImportError: + class MiddlewareMixin(object): + def __init__(self, *args, **kwargs): + pass thread_data = threading.local() From d37bed015f29d3307641bd770e74f8a7c6269205 Mon Sep 17 00:00:00 2001 From: Dayo Ayeni Date: Sun, 26 Feb 2017 15:09:34 +0100 Subject: [PATCH 8/8] Update backend.py reverted to get the USERNAME_FIELD off get_user_model() --- useraudit/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/useraudit/backend.py b/useraudit/backend.py index 40740f4..2fe6f9b 100644 --- a/useraudit/backend.py +++ b/useraudit/backend.py @@ -34,7 +34,7 @@ def __init__(self): def authenticate(self, **credentials): UserModel = get_user_model() - self.username = credentials.get('username') + self.username = credentials.get(UserModel.USERNAME_FIELD) self.login_logger.log_failed_login(self.username, get_request()) self.login_attempt_logger.increment(self.username) self.block_user_if_needed()