Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jazzband/django-axes
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: cc-dev-github/django-axes
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 7 commits
  • 3 files changed
  • 3 contributors

Commits on Aug 8, 2023

  1. Copy the full SHA
    c89a392 View commit details
  2. Copy the full SHA
    d75afa2 View commit details
  3. Merge pull request #1 from cc-dev-github/molmos/26507-track-user-id

    Refs #26507: Track user_id
    cc-dev-github authored Aug 8, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9dd398d View commit details

Commits on Nov 16, 2023

  1. Add autologout column

    Kevin Yokley committed Nov 16, 2023
    Copy the full SHA
    4fe229a View commit details
  2. Allow existing records to be null

    Kevin Yokley committed Nov 16, 2023
    Copy the full SHA
    b06b393 View commit details
  3. Add django migration

    Kevin Yokley committed Nov 16, 2023
    Copy the full SHA
    270c114 View commit details

Commits on Nov 28, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    0e94606 View commit details
Showing with 62 additions and 2 deletions.
  1. +26 −2 axes/handlers/database.py
  2. +30 −0 axes/migrations/0009_accesslog_autologout_accesslog_user.py
  3. +6 −0 axes/models.py
28 changes: 26 additions & 2 deletions axes/handlers/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from logging import getLogger
from typing import Optional

from django.db import transaction
from django.db import transaction, connections
from django.db.models import F, Sum, Value, Q
from django.db.models.functions import Concat
from django.utils import timezone
@@ -24,6 +24,9 @@
from axes.models import AccessLog, AccessAttempt, AccessFailureLog
from axes.signals import user_locked_out

from django.db.utils import ProgrammingError, InternalError
from psycopg2.errors import InFailedSqlTransaction

log = getLogger(__name__)


@@ -277,7 +280,7 @@ def user_logged_in(self, sender, request, user, **kwargs):

if not settings.AXES_DISABLE_ACCESS_LOG:
# 2. database query: Insert new access logs with login time
AccessLog.objects.create(
accesslog_obj = AccessLog.objects.create(
username=username,
ip_address=request.axes_ip_address,
user_agent=request.axes_user_agent,
@@ -286,6 +289,27 @@ def user_logged_in(self, sender, request, user, **kwargs):
attempt_time=request.axes_attempt_time,
)

##### BEGIN CouponCabin minor override to track the user ID #####
usadmin = settings.DB_SCHEMA.get('usadmin', '')
tbl_name = AccessLog._meta.db_table
user_id = user.pk
accesslog_id = accesslog_obj.pk

query = f"""
UPDATE "{usadmin}{tbl_name}"
SET user_id = {user_id}
WHERE id = {accesslog_id}
"""

try:
with connections['default'].cursor() as cursor:
cursor.execute(query)
except (BaseException, ProgrammingError, InternalError, InFailedSqlTransaction) as e:
log.warning(f'Unable to set the user_id on the {tbl_name} for {username} ({user_id}): {e}')
except:
log.warning(f'Unable to set the user_id on the {tbl_name} for {username} ({user_id})')
##### END CouponCabin minor override to track the user ID #####

if settings.AXES_RESET_ON_SUCCESS:
# 3. database query: Reset failed attempts for the logging in user
count = reset_user_attempts(request, credentials)
30 changes: 30 additions & 0 deletions axes/migrations/0009_accesslog_autologout_accesslog_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.7 on 2023-11-16 11:42

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("axes", "0008_accessfailurelog"),
]

operations = [
migrations.AddField(
model_name="accesslog",
name="autologout",
field=models.BooleanField(blank=True, null=True),
),
migrations.AddField(
model_name="accesslog",
name="user",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
]
6 changes: 6 additions & 0 deletions axes/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _

@@ -54,6 +55,11 @@ class Meta:
class AccessLog(AccessBase):
logout_time = models.DateTimeField(_("Logout Time"), null=True, blank=True)

user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)

autologout = models.BooleanField(null=True,
blank=True)

def __str__(self):
return f"Access Log for {self.username} @ {self.attempt_time}"