From d4e2572a268180fb68ae4acc01ec23b182e9f3ce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Sun, 7 Feb 2016 18:36:45 +0100 Subject: [PATCH] Add system check for non-unique case-insensitive usernames --- mygpo/users/__init__.py | 2 ++ mygpo/users/checks.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 mygpo/users/checks.py diff --git a/mygpo/users/__init__.py b/mygpo/users/__init__.py index c2c6f23e..7529e229 100644 --- a/mygpo/users/__init__.py +++ b/mygpo/users/__init__.py @@ -1 +1,3 @@ default_app_config = 'mygpo.users.apps.UsersConfig' + +from . import checks diff --git a/mygpo/users/checks.py b/mygpo/users/checks.py new file mode 100644 index 00000000..1634224d --- /dev/null +++ b/mygpo/users/checks.py @@ -0,0 +1,29 @@ +from django.core.checks import register, Warning +from django.db import connection + + +SQL = """ +SELECT count(*), lower(username) +FROM auth_user +GROUP BY lower(username) +HAVING count(*) > 1; +""" + +@register() +def check_case_insensitive_users(app_configs=None, **kwargs): + errors = [] + + cursor = connection.cursor() + cursor.execute(SQL) + non_unique = cursor.fetchall() + + usernames = [t[1] for t in non_unique] + + if len(non_unique) > 0: + txt = 'There are {0} non-unique usernames: {1}'.format( + len(non_unique), + ', '.join(usernames[:10] + ['...']) + ) + wid='users.W001' + errors.append(Warning(txt, id=wid)) + return errors -- 2.11.4.GIT