Skip to content

Commit 9e70a4e

Browse files
committed
Track when an account last logged into a community auth site
This information can be useful when trying to debug issues with the community auth and the wonders of distributed data... No actual django model is created because django still doesn't support multi-column primary keys. Thus no tool to use the data yet other than psql.
1 parent a5108ad commit 9e70a4e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.27 on 2020-02-25 09:40
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('account', '0003_cauth_consent'),
12+
]
13+
14+
operations = [
15+
migrations.RunSQL("""CREATE TABLE account_communityauthlastlogin (
16+
user_id int NOT NULL REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED,
17+
site_id int NOT NULL REFERENCES account_communityauthsite (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
18+
lastlogin timestamptz NOT NULL,
19+
logincount bigint NOT NULL,
20+
CONSTRAINT account_communityauthlastlogin_pkey PRIMARY KEY (user_id, site_id)
21+
)"""),
22+
]

pgweb/account/views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.contrib.auth.tokens import default_token_generator
1010
from django.contrib.auth import logout as django_logout
1111
from django.conf import settings
12-
from django.db import transaction
12+
from django.db import transaction, connection
1313
from django.db.models import Q
1414

1515
import base64
@@ -528,6 +528,14 @@ def communityauth(request, siteid):
528528
return HttpResponseRedirect('/account/auth/{0}/consent/?{1}'.format(siteid,
529529
urllib.parse.urlencode({'next': '/account/auth/{0}/{1}'.format(siteid, urldata)})))
530530

531+
# Record the login as the last login to this site. Django doesn't support tables with
532+
# multi-column PK, so we have to do this in a raw query.
533+
with connection.cursor() as curs:
534+
curs.execute("INSERT INTO account_communityauthlastlogin (user_id, site_id, lastlogin, logincount) VALUES (%(userid)s, %(siteid)s, CURRENT_TIMESTAMP, 1) ON CONFLICT (user_id, site_id) DO UPDATE SET lastlogin=CURRENT_TIMESTAMP, logincount=EXCLUDED.logincount+1", {
535+
'userid': request.user.id,
536+
'siteid': site.id,
537+
})
538+
531539
info = {
532540
'u': request.user.username.encode('utf-8'),
533541
'f': request.user.first_name.encode('utf-8'),

0 commit comments

Comments
 (0)