We have multiple databases for authentication. Basically one database
per department. Before the obvious question is asked, I cannot merge
nor sync the various databases into one Django managed admin database.
The database schema cannot change and I am hoping I can work around
this in Django code.

For example purposes let's say our databases are: 'sales', 'support'
and 'engineering'. The users on each database are unique and we cannot
sync the databases.

I have setup settings.py to include a dictionary for all database
connections:

DATABASES = {
    'default': {
        STANDARD DB CONNECTION DETAILS
    },
    'sales_users': {
        STANDARD DB CONNECTION DETAILS
    },
    'support_users': {
        STANDARD DB CONNECTION DETAILS
    },
    'engineering_users': {
        STANDARD DB CONNECTION DETAILS
    },

I have a custom login form that includes an additional field over the
standard form username and password. The additional field is
'department'.

On the login form, I am capturing the department, username and
password.

I have also written a custom authentication backend to handle the
additional field for authentication. The custom authenticate method
takes the following arguments: self, email, password and db_to_use.

The custom authentication backend authenticate method does the
following:

def authenticate(self, username=None, password=None, db_to_use=None,
**kwargs):
    user = User.objects.using(db_to_use).get(username=username)
    if user.check_password(password):
        return user
    return None

Up to this point everything works as expected. If on the login form a
user submits the following:

Department: sales
Username: User1
Password: Password

My login view will collect the POST values and then pass to:

user = auth.authenticate(username=username, password=password,
db_to_use=dbalias)

I am able to confirm that this this successfully returns the user
object from the custom authentication backend. This would have queried
the appropriate database based on the department entered
('sales_users') and returned the user object.

The next part is where I'm having issue.

After successfully returning an authenticated user object (and making
sure that the user isn't None and isn't inactive), I then call the
auth.login(request, user) method.

However, I'm finding that auth.login() is failing because auth.login()
is then using the 'default' database rather than the 'sales_users'
database connection as per the example above. This results in the user
never seeming to get "logged in". I'm not sure how to "persist" the
use of the appropriate database after the authenticate() call and into
the login() call.

I've looked at the Django source and it seems that the
django.contrib.auth.login() method is supposed to save the
BACKEND_SESSION_KEY but I'm not sure how this relates to the login()
method using the database that was specified in my custom
authentication backend.

I've gone through the Multiple Database documentation and am not sure
what to try next. My thoughts are:

1. Implement a routing system, but I'm not sure how that applies to
the login issue I am experiencing.

2. Implement a database manager, but again I'm not sure how this
applies to the login issue I am experiencing.

3. Edit ModelAdmin to expose multiple databases to Django's admin
interface. I'm also unsure as to this option since I'm not using the
admin interface but just need a way to force the login() method to use
a difference database.

4. Add a custom login() method to my custom authentication backend?
This sounds like an option I could undertake but I'm unsure of the
syntax to select the appropriate database and perform all of the
required login features like saving the User ID to the session, etc.

If anyone has any hints, tips or insights into the problem I have I
would really appreciate it!

Thanks,

Lawrence.


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to