I have a project that uses Python 3.6
and Django 1.11
where I use the built-in User
model.
The user objects are all inside the default database (which is postgres
), but the project uses a second authentication backend because some users need to be authenticated against a legacy Oracle
database.
# settings.py
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend', # new postgres DB
'project_config.auth_backends.OtherBackend', # legacy Oracle DB
]
This works fine so far, but now I have 3 groups of users:
- some users can only authenticate in
ModelBackend
because they are not in the legacy DB (because they are new users). - some users can only authenticate in the legacy DB; they have
usr.has_usable_password() == False
because they have not set their password in the newpostgres
DB yet. - some users can authenticate in both backends, maybe even with different passwords in each one; this is because they changed their password in the new system, but by design that change is not transmitted back to the legacy DB (don't shoot me, the only way to change the password in the legacy DB is to do it manually through the user interface).
For auditing purposes, I want to list all users and see which backends each one has available (ignoring the is_active
flag for now) to make auditing tasks easier.
My idea was to use a loop similar to this one:
for usr in User.objects.all():
backend_list = []
if usr.has_usable_password():
backend_list.append('ModelBackend')
if ... : # what should I check here ?
backend_list.append('OtherBackend')
print(usr, backend_list)
I don't have the passwords for each user for the legacy database, so is may idea even possible?
I have not found a way, but I am open to suggestions.