Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes here and there #13

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions useraudit/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?
The current version seems more flexible to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in retrospect, I can seem to remember why.

self.login_logger.log_failed_login(self.username, get_request())
self.login_attempt_logger.increment(self.username)
self.block_user_if_needed()
Expand Down
3 changes: 2 additions & 1 deletion useraudit/middleware.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import threading
from django.utils.deprecation import MiddlewareMixin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail on pre 1.10 Django.

you should do something along these lines:

https://github.com/muccg/django-iprestrict/blob/4db839c123ae90afb82438cb500b5f3d54477ea9/iprestrict/middleware.py#L10

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented this. Thanks


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
5 changes: 3 additions & 2 deletions useraudit/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils import datetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the change but shouldn't this be?

from django.utils import timezone

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes indeed, it should.

from django.contrib.auth.signals import user_logged_in
import datetime

Expand All @@ -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()


Expand Down